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:
Type.field formatnil object to resolve the field on.Additionally, it accepts some keyword arguments:
arguments:, GraphQL arguments to the field, in Ruby-style (underscore, symbol) or GraphQL-style (camel-case, string)context:, the GraphQL context to use for this queryrun_graphql_field performs several GraphQL-related steps:
.visible? on the named Object Type, raising an error if it isn’t visible.authorized? on the type, calling Schema.unauthorized_object if authorization fails#visible? on the field, raising an error if the field isn’t visible#authorized? on the field, calling Schema.unauthorized_field if it failsYou 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.