Helpers
GraphQL-Ruby ships with a test helper method, run_graphql_field, that can execute a GraphQL field in isolation. To use it in your test suite, include the module with your schema class:
# Mix in `run_graphql_field(...)` to run on `MySchema` include GraphQL::Testing::Helpers.for(MySchema)
Then, you can run fields using Testing::Helpers#run_graphql_field:
post = Post.first graphql_post_title = run_graphql_field("Post.title", post) assert_equal "100 Great Ideas", graphql_post_title
run_graphql_field accepts two required arguments:
-
Field path, in
Type.fieldformat -
Runtime object: some non-
nilobject to resolve the field on.
Additionally, it accepts some keyword arguments:
-
arguments:,GraphQLarguments to the field, in Ruby-style (underscore, symbol) or GraphQL-style (camel-case, string) -
context:, theGraphQLcontext to use for this query
run_graphql_field performs several GraphQL-related steps:
-
Checks
.visible?on the named Object Type, raising an error if it isn’t visible -
Wraps the given runtime object in the
GraphQLObject Type -
Checks
.authorized?on the type, calling Schema.unauthorized_object if authorization fails -
Prepares arguments for field resolution
-
Checks
#visible?on the field, raising an error if the field isn’t visible -
Checks
#authorized?on the field, calling Schema.unauthorized_field if it fails -
Calls any field extensions
-
Runs Dataloader and/or GraphQL-Batch, as needed
Resolving fields on the same object
You can use Testing::Helpers#with_resolution_context to use the same type, runtime object, and GraphQL context for multiple field resolutions. For example:
# Assuming `include GraphQL::Testing::Helpers.for(MySchema)` # was used above ... with_resolution_context(type: "Post", object: example_post, context: { current_user: author }) do |rc| assert_equal "100 Great Ideas", rc.run_graphql_field("title") assert_equal true, rc.run_graphql_field("viewerIsAuthor") assert_equal 5, rc.run_graphql_field("commentsCount") # Optionally, pass `arguments:` for the field: assert_equal 9, rc.run_graphql_field("commentsCount", arguments: { include_unmoderated: true }) end
The method yields a resolution context (rc, above) which responds to run_graphql_field.