GraphQL,使用 GraphiQL 的查询语法不正确

GraphQL, incorrect syntax for query using GraphiQL

GraphQL 新手,我在通过 GraphiQL 发送查询时遇到问题。

这是我的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
});

当我 运行 在 graphiql 上进行以下查询时:

query {
  user {
    id: "23"
  }
}

我得到了 "Syntax Error GraphQL request (3:9) Expected Name, found String",但我希望得到 ID 为“23”的用户。

我错过了什么?

您将参数值放在您的选择上,它应该看起来像

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

请查看教程的参数部分http://graphql.org/graphql-js/passing-arguments/