Newbie GraphQL Question: Query Variables?


#1

I’m using the excellent Kadira LearnGraphQL site, and learning a lot. At the same time, in Lesson 9 there’s some sample code that I haven’t gotten to work yet. If I’m following the lesson correctly, this is the sample code:

import {
  // These are the basic GraphQL types
  GraphQLInt,
  GraphQLFloat,
  GraphQLString,
  GraphQLList,
  GraphQLObjectType,
  GraphQLEnumType,

  // This is used to create required fields and arguments
  GraphQLNonNull,

  // This is the class we need to create the schema
  GraphQLSchema,

  // This function is used execute GraphQL queries
  graphql
} from 'graphql';

const Query = new GraphQLObjectType({
  name: 'RootQueries',
  fields: () => ({
    echo: {
      type: GraphQLString,
      args: {
        message: {type: GraphQLString}
      },
      resolve(rootValue, args) {
        return `received: ${args.message}`;
      }
    }
  })
});

const Schema = new GraphQLSchema({
  query: Query
});

let query = `
  query getEcho($inputMessage: String) {
    receivedMessage: echo(message: $inputMessage)
  }
`;

graphql(Schema, query, null, {inputMessage: "Hello"}).then(function(result) {
  console.log(result);
});

When I run the code, I get:

{ data: { receivedMessage: ‘received: undefined’ } }

… instead of:

{ data: { receivedMessage: ‘received: Hello’ } }

What am I missing?


#2

GraphQL has been updated with a new parameter since the LearnGraphQL tutorial was posted. The correct call is:

graphql(Schema, query, null, null, {inputMessage: "Hello"}).then(function(result) {
  console.log(result);
});

Thanks to @helfer on the Apollo Slack for the info!