I've set a Schema to make a GraphQL Query, and now I run the server, go to GraphQL Client, and I get the following error:
"Syntax Error: Unexpected "
line: 32.
My code is the following:
const graphql = require('graphql') const _ = require('lodash') const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema
} = graphql;
const users = [{
id: '23',
firstName: 'Thiago',
age: 36
}, {
id: '47',
firstName: 'Caroline',
age: 29
}];
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
})
Thanks in advance for your help.
Related
I got this error:
"Mutation fields must be an object with field names as keys or a function which returns such an object."
I try to add user but I don't know how to make this work.
const mutation = new GraphQLObjectType({
name: "Mutation",
fileds: {
addUser: {
type: UserType,
args: {
firstName: { type: new GraphQLNonNull(GraphQLString) },
age: { type: new GraphQLNonNull(GraphQLInt) },
companyId: { type: GraphQLString }
},
resolve(parentValue, { firstName, age }) {
return axios.post("http://localhost:3000/users", { firstName, age }).then(r => r.data);
}
}
}
})
UserType is defined as follows:
const UserType = new GraphQLObjectType({
name: "User",
fields: {
id: { type: GraphQLString },
firstName: { type: GraphQLString },
age: { type: GraphQLInt },
company: {
type: CompanyType,
resolve(parentValue, args) {
return axios.get("http://localhost:3000/companies/" + parentValue.companyId).then(res => res.data)
}
}
}
})
You have a typo: "fileds" should be "fields".
I am learning GraphQL and I faced an error due while trying to start a server.
const graphql = require('graphql');
const _ = require('lodash');
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema
} = graphql;
const Users = [
{ id: '23', firstName: 'Bill', age: 20 },
{ id: '41', firstName: 'Jane', age: 18 }
];
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: {
type: UserType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args) {
return _.find(Users, { id: args.id });
}
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
});
and the server.js code is here:
const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const schema = require('./schema/schema');
const app = express();
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true
}));
app.listen(4000, () => {
console.log("Server running at port 4000.");
});
The error seems to be coming from the "module.exports" section as when I comment it out the server starts without a problem.
There is syntax confusion. In RootQuery the type part under user completely covers the whole field structure, error is here.
Just remove top level type: {}.
Try this out:
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: { id: { type: GraphQLString } },
resolve(parentValue, args) {
return _.find(Users, { id: args.id });
}
}
}
});
i am learning GraphQL with NodeJS but getting the error.
i have develop a mongoDB and NodeJS GraphQL REST API for Understanding and Learning purpose
Here is my Code :
const graphql = require('graphql');
const user = require('../models/User');
const post = require('../models/Post');
const { GraphQLObjectType, GraphQLID, GraphQLString } = graphql;
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
id: { type: GraphQLID },
fname: { type: GraphQLString },
lname: { type: GraphQLString },
email: { type: GraphQLString },
posts: {
type: PostType,
resolve(parent, args) {
return 1;
}
}
})
});
const PostType = new GraphQLObjectType({
name: 'Post',
fields: () => ({
id: { type: GraphQLID },
Userid: { type: GraphQLID },
title: { type: GraphQLString },
date: { type: GraphQLString },
})
});
const RootQueryType = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return user.findById({ id: args.id });
}
},
post: {
type: PostType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return post.findById({ id: args.id });
}
}
}
});
module.exports = new graphql.GraphQLSchema({
query: RootQueryType,
});
Please Help me to find the Solution.
Answer me to resolve this error.
I am looking for examples of writing nested mutations. I am making a mutation for a recipe object and the schema looks like this:
const RecipeType = new GraphQLObjectType({
name: "Recipe",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
dateCreated: { type: GraphQLString },
authorID: { type: GraphQLID },
prepTime: { type: PrepTimeType },
cookTime: { type: CookTimeType },
ingredients: { type: new GraphQLList(IngredientType) },
steps: { type: new GraphQLList(StepType) }
})
});
const PrepTimeType = new GraphQLObjectType({
name: "PrepTime",
fields: () => ({
quantity: { type: GraphQLFloat },
unit: { type: GraphQLString }
})
});
const CookTimeType = new GraphQLObjectType({
name: "CookTime",
fields: () => ({
quantity: { type: GraphQLFloat },
unit: { type: GraphQLString }
})
});
const IngredientType = new GraphQLObjectType({
name: "Ingredients",
fields: () => ({
name: { type: GraphQLString },
quantity: { type: GraphQLFloat },
unit: { type: GraphQLString }
})
});
const StepType = new GraphQLObjectType({
name: "Ingredients",
fields: () => ({
details: { type: GraphQLString },
estimatedTime: { type: GraphQLFloat },
unit: { type: GraphQLString }
})
});
I am looking to write a mutation that creates an entire object for this item. The mutation looks like the following:
createRecipe: {
type: RecipeType,
args: {
// Required Args
name: { type: new GraphQLNonNull(GraphQLString) },
authorID: { type: new GraphQLNonNull(GraphQLID) },
ingredients: { type: new GraphQLList(IngredientType) },
steps: { type: new GraphQLList(StepType) },
// Not required args
prepTime: { type: PrepTimeType },
cookTime: { type: CookTimeType },
},
resolve(parent, args) {
let recipe = new Recipe({
name: args.name,
dateCreated: new Date().getTime(),
authorID: args.authorID,
ingredients: args.ingredients,
steps: args.steps
});
// Check for optional args and set to recipe if they exist
args.prepTime ? recipe.prepTime = args.prepTime : recipe.prepTime = null;
args.cookTime ? recipe.cookTime = args.cookTime : recipe.cookTime = null;
return recipe.save();
}
}
I am not sure how to create one mutation that creates the entire object. and then updating will be a further challenge. Does anyone have any examples or links to the docs that support this? From what I can tell GraphQL hasn't covered this in a helpful manner.
I am currently getting the following errors:
{
"errors": [
{
"message": "The type of Mutation.createRecipe(ingredients:) must be Input Type but got: [Ingredients]."
},
{
"message": "The type of Mutation.createRecipe(steps:) must be Input Type but got: [Steps]."
},
{
"message": "The type of Mutation.createRecipe(prepTime:) must be Input Type but got: PrepTime."
},
{
"message": "The type of Mutation.createRecipe(cookTime:) must be Input Type but got: CookTime."
}
]
}
Any support would be greatly appreciated.
Cheers,
I figured this out. I needed to create input types for each subdocument. I already had the object types but for the mutations, I had to add new ones. From there I placed it into the mutation as such.
createRecipe: {
type: RecipeType,
args: {
// Required Args
name: { type: new GraphQLNonNull(GraphQLString) },
authorID: { type: new GraphQLNonNull(GraphQLID) },
ingredients: { type: new GraphQLList(IngredientInputType) },
steps: { type: new GraphQLList(StepInputType) },
// Not required args
prepTime: { type: PrepTimeInputType },
cookTime: { type: CookTimeInputType },
},
resolve(parent, args) {
let recipe = new Recipe({
name: args.name,
dateCreated: new Date().getTime(),
authorID: args.authorID,
ingredients: args.ingredients,
steps: args.steps
});
// Check for optional args and set to recipe if they exist
args.prepTime ? recipe.prepTime = args.prepTime : recipe.prepTime = null ;
args.cookTime ? recipe.cookTime = args.cookTime : recipe.cookTime = null ;
return recipe.save();
}
},
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?
you 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