Skip to content

Instantly share code, notes, and snippets.

@dmaze
Created October 9, 2019 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmaze/c93ec7aa36ee0b17df2ded223e3b5ac8 to your computer and use it in GitHub Desktop.
Save dmaze/c93ec7aa36ee0b17df2ded223e3b5ac8 to your computer and use it in GitHub Desktop.
graphql-ruby 1.9.8 resolver arguments don't work when schema is built from_definition
#!/usr/bin/env ruby
# frozen_string_literal: true
# This illustrates a bug in graphql-ruby 1.9.8. Prior to this version,
# a resolver function passed in the map to GraphQL::Schema.from_definition
# could access its arguments as methods on the `args` object it gets passed;
# but starting in graphql-ruby 1.9.8 this no longer works.
require 'graphql'
# This is a sample very small GraphQL schema.
SDL = <<~GRAPHQL
input In { s: String! }
type Query {
echo(in: In!): String!
}
GRAPHQL
RESOLVERS = {
'Query' => {
# vvv THIS LINE is what breaks
'echo' => ->(_obj, args, _ctx) { args.in.s }
# ^^^
#
# In graphql-ruby 1.9.7, args.in.s resolves to the passed-in
# argument. In graphql-ruby 1.9.8, it produces a NoMethodError
# where args.in isn't defined.
}.freeze
}.freeze
# Create the GraphQL schema object
Schema = GraphQL::Schema.from_definition(SDL, default_resolve: RESOLVERS)
# Run a minimal query
QUERY = '{ echo(in: { s: "foo" }) }'
result = Schema.execute(QUERY)
p result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment