Skip to content

Commit

Permalink
fix panic when node id is an int (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccbrown authored May 28, 2024
1 parent 3478735 commit 8ed6781
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 8 additions & 3 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand All @@ -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
}
}`)
Expand Down
16 changes: 11 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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)
},
Expand Down

0 comments on commit 8ed6781

Please sign in to comment.