0
votes

New to GraphQL, I'm having problems sending a query through GraphiQL.

This is my schema.js

const graphql = require('graphql');
const _ = require('lodash');
const{
    GraphQLObjectType,
    GraphQLInt,
    GraphQLString,
    GraphQLSchema
} = graphql;

const users = [
    {id:'23', firstName:'Bill', age:20},
    {id:'47', firstName:'Samantha', age:21}
];

const UserType = new GraphQLObjectType({
    name: 'User',
    fields:{
        id: {type: GraphQLString},
        firstName: {type: GraphQLString},
        age:{type: GraphQLInt}
    }
});

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields:{
        user: {
            type: UserType,
            args:{
                id: {type: GraphQLString}
            },
            resolve(parentValue, args){ // move the resolve function to here
                return _.find(users, {id: args.id});
            }
        },

    }
});

module.exports = new GraphQLSchema({
    query: RootQuery
});

And when I run the following query on graphiql:

query {
  user {
    id: "23"
  }
}

I got "Syntax Error GraphQL request (3:9) Expected Name, found String", but I would expect to get the user with id "23".

What am I missing?

1

1 Answers

3
votes

You are putting your argument value on your selection it should instead look like

query {
  user(id: “23”) {
    id
    firstName
    age
  }
}

Please review the arguments section of the tutorial http://graphql.org/graphql-js/passing-arguments/