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:

Additionally, it accepts some keyword arguments:

run_graphql_field performs several GraphQL-related steps:

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.