Trying to get GraphQL to work with JavaScript. Not sure where my mistake is.
My code
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
});
I am getting
{ "errors": [ { "message": "Type RootQueryType must define one or more fields." } ] }
Why is it not working?
Trying to get GraphQL to work with JavaScript. Not sure where my mistake is.
My code
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
});
I am getting
{ "errors": [ { "message": "Type RootQueryType must define one or more fields." } ] }
Why is it not working?
Share Improve this question asked Apr 7, 2018 at 18:40 Nicole MarieNicole Marie 1312 silver badges8 bronze badges2 Answers
Reset to default 2you forgot to use an arrow function
const UserType = new GraphQLObjectType({
name: 'User',
fields:()=>( {
id: {type: GraphQLString},
firstName: {type: GraphQLString},
age:{type: GraphQLInt}
});
I believe your mistake is simply in your Query. You use the RootQueryType's fields
object to make your query endpoints. The fields
object in your case only contains one query: user
. However you are trying to make a query for User
, which is different.
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
// The items listed here are going to be your root query endpoints.
// Which in this case is only `user`.
user: {
type: UserType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args) {
return _.find(users, { id: args.id });
}
}
}
});
So you need to make your query using user
.
Additionally, you need to make sure you are making your query correctly. Basic query syntax for what you are trying to achieve looks like this:
{
user(id: "23") {
id
firstName
age
}
}
Let me know if this works for you.
Some documentation on queries:
GraphQL Queries
DevHints - GraphQL