GraphQL validation error

GraphQL validation error


0

I am trying out relay-treasure hunt tutorial but getting the following error when I run npm start. I did update my schema by running npm run update-schema.

Uncaught Error: GraphQL validation error ``Cannot query field "game" on type "Query".`` in file `/home/tharaka/My Projects/relay-treasurehunt/js/routes/AppHomeRoute.js`. Try updating your GraphQL schema if an argument/field/type was recently added.

Here’s what’s inside my AppHomeRoute.js

import Relay from 'react-relay';

export default class extends Relay.Route {
    static path = '/';  
    static queries = {
        game: () => Relay.QL`query { game }`,
    };
    static routeName = 'AppHomeRoute';
}

2

  • What does your server side code look like? The Query GraphQLObjectType needs to have a property called fields with a func that returns an object with a game property.

    – Andy Carlson

    Jun 26, 2016 at 7:52

  • Here's the complete code

    – TA3

    Jun 26, 2016 at 8:52


2 Answers
2


0

It seems you have to query everything through either node or viewer, because those are the only fields on the root query type. So maybe try this:

query {
  viewer {
    game
  }
}


0

In your data/schema.js file…

var queryType = new GraphQLObjectType({
  name: 'Query',
  fields: () => ({
    node: nodeField,
    // Add your own root fields here
    viewer: {
      type: gameType,
      resolve: () => getGame(),
    },
  }),
});

Change the viewer property to game. Like this…

var queryType = new GraphQLObjectType({
  name: 'Query',
  fields: () => ({
    node: nodeField,
    // Add your own root fields here
    game: {
      type: gameType,
      resolve: () => getGame(),
    },
  }),
});

The name of that property needs to match the field you’re querying on in your fragment on your Home route. I suppose as an alternative you could change your route to request viewer instead of game. Do whichever feels better.



Leave a Reply

Your email address will not be published. Required fields are marked *