From 8ed6781ebbc3f11eec51c7f65b8db2e494447f9c Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 28 May 2024 12:30:18 -0400 Subject: [PATCH] fix panic when node id is an int (#71) --- api_test.go | 11 ++++++++--- config.go | 16 +++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/api_test.go b/api_test.go index 2890f12..e40a968 100644 --- a/api_test.go +++ b/api_test.go @@ -163,14 +163,18 @@ func TestNodes(t *testing.T) { c: node(id: "c") { id } + one: node(id: 1) { + id + } }`) require.Equal(t, http.StatusOK, resp.StatusCode) var result struct { Data struct { - A *node - C *node + A *node + C *node + One *node } Errors []struct{} } @@ -179,11 +183,12 @@ func TestNodes(t *testing.T) { assert.NotNil(t, result.Data.A) assert.Nil(t, result.Data.C) + assert.Nil(t, result.Data.One) }) t.Run("Multiple", func(t *testing.T) { resp := executeGraphQL(t, api, `{ - nodes(ids: ["a", "b", "c", "d"]) { + nodes(ids: ["a", "b", "c", "d", 1]) { id } }`) diff --git a/config.go b/config.go index 7e1b45e..b066d28 100644 --- a/config.go +++ b/config.go @@ -92,11 +92,15 @@ func (cfg *Config) init() { Cost: graphql.FieldResolverCost(1), Resolve: func(ctx graphql.FieldContext) (interface{}, error) { // TODO: batching? - nodes, err := ctxAPI(ctx.Context).config.ResolveNodesByGlobalIds(ctx.Context, []string{ctx.Arguments["id"].(string)}) - if err != nil || len(nodes) == 0 { - return nil, err + if id, ok := ctx.Arguments["id"].(string); ok { + nodes, err := ctxAPI(ctx.Context).config.ResolveNodesByGlobalIds(ctx.Context, []string{id}) + if err != nil || len(nodes) == 0 { + return nil, err + } + return nodes[0], nil + } else { + return nil, nil } - return nodes[0], nil }, }, "nodes": { @@ -117,7 +121,9 @@ func (cfg *Config) init() { Resolve: func(ctx graphql.FieldContext) (interface{}, error) { var ids []string for _, id := range ctx.Arguments["ids"].([]interface{}) { - ids = append(ids, id.(string)) + if id, ok := id.(string); ok { + ids = append(ids, id) + } } return ctxAPI(ctx.Context).config.ResolveNodesByGlobalIds(ctx.Context, ids) },