【发布时间】:2018-05-25 12:32:11
【问题描述】:
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 为“23”的用户。
我错过了什么?
【问题讨论】:
标签: graphql graphql-js