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

时间:2017-12-11 22:00:36

标签: graphql graphql-js

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"
  }
}

我得到"语法错误GraphQL请求(3:9)预期名称,找到字符串",但我希望得到ID为#34; 23"的用户。

我错过了什么?

1 个答案:

答案 0 :(得分:2)

你将你的论点值放在你的选择上,而不应该是

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

请查看教程http://graphql.org/graphql-js/passing-arguments/

的参数部分