How do you write a test for GetPreloads from the docs, given it expects an OperationContext? #1793
Unanswered
AndrewRayCode
asked this question in
Q&A
Replies: 2 comments 1 reply
-
Did you ever manage to find a solution for this issue? |
Beta Was this translation helpful? Give feedback.
0 replies
-
I got this test to pass: func TestGetPreloads(t *testing.T) {
t.Run("Context with fields", func(t *testing.T) {
ctx := graphql.WithOperationContext(context.Background(), &graphql.OperationContext{})
ctx = graphql.WithFieldContext(ctx, &graphql.FieldContext{
Field: graphql.CollectedField{
Selections: ast.SelectionSet{
&ast.Field{
Name: "field_1",
SelectionSet: ast.SelectionSet{
&ast.Field{
Name: "field_a",
},
},
},
&ast.Field{
Name: "field_2",
},
},
},
})
result := preloads.GetPreloads(ctx)
expected := []string{"field_1", "field_1.field_a", "field_2"}
if !reflect.DeepEqual(result, expected) {
t.Errorf("expected %v, got %v", expected, result)
}
})
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What happened?
I'm writing a GraphQL service, that itself calls down to an underlying GraphQL service. To support this, I want to read the fields passed to my service, so I can dynamically build the query to send to the underlying service for only the fields needed.
The documentation has an example of how to do this with the
GetPreloads
function.This function is working when I perform a live query through my server. However, in my test, I'm calling the resolver more directly, something like:
When I run this as is, I get a panic from gqlgen:
So I set out trying to build a context that would satisfy the underlying requirements. I've tried this:
This feels like I need to re-create the query to make the test work. This code also raises a nil pointer:
I'm brand new to Go and this library. If this question is more appropriate on StackOverflow I'm happy to ask it there. Since the example function came from the docs, I figured I'd start here to see if there was some official way to make these functions testable for calling the resolvers themselves.
versions
gqlgen version
v0.13.0go version
1.15Beta Was this translation helpful? Give feedback.
All reactions