0

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 Answer 1

3

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.