试图让 GraphQL 与 JavaScript 一起工作。不知道我的错误在哪里。
我的代码
const graphql = require('graphql');
const _ = require('lodash');
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema
} = graphql;
const users = [
{ id: "23", firstName: "Bill", age: 20},
{ id: "47", firstName: "Sam", 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) {
return _.find(users, { id: args.id });
}
}
}
});
module.exports = new GraphQLSchema ({
query: RootQuery
});
我正进入(状态
{ "errors": [ { "message": "Type RootQueryType 必须定义一个或多个字段。" } ] }
为什么它不起作用?