I'm new to graphQL and started from 'express-graphql'. I have problem: when I send a query from Postman it can not reach the resolvers. This is my schema:
import { buildSchema } from 'graphql';
export const schema = buildSchema(`
type Task {
id: ID!
title: String!
description: String!
tags: [String!]!
done: Boolean!
}
type Query {
getTodoTasks: [Task]!
getDoneTasks: [Task]!
getTask(id: ID!): Task
findTasks(tags: [String]!): [Task]!
}
input CreateTaskInput {
title: String!
description: String!
tags: [String]!
}
input UpdateTaskInput {
title: String!
description: String!
tags: [String]!
done: Boolean!
}
type Mutation {
createTask(task: CreateTaskInput!): Task!
updateTask(id:ID!, task: UpdateTaskInput!): Task!
deleteTask(id:ID!): Boolean!
}
`);
console.log('GraphQL Schema:', schema);
export default schema;
resolvers:
import { TaskService } from '../services/TaskService.js';
const taskService = new TaskService();
const resolvers = {
Query: {
getTodoTasks: async () => {
try {
const toDoTasks = await taskService.getTasks();
return toDoTasks.filter((task) => !task.done);
} catch (err) {
throw new Error(err.message);
}
},
getDoneTasks: async () => {
try {
const doneTasks = await taskService.getDoneTasks();
return doneTasks;
} catch (err) {
throw new Error(err.message);
}
},
getTask: async (_, { id }) => {
try {
const task = await taskService.getTask(id);
return task;
} catch (err) {
throw new Error(err.message);
}
},
findTasks: async (_, { tags }) => {
try {
const tasks = await taskService.findTasks(tags);
return tasks;
} catch (err) {
throw new Error(err.message);
}
},
},
Mutation: {
createTask: async ({ task }) => {
console.log('Request received in resolver');
try {
const { title, description, tags } = task;
const result = await taskService.addTask(title, description, tags);
console.log(result);
return result;
} catch (err) {
console.error('Error while creating task: ', err);
throw new Error(err.message || 'Internal server error');
}
},
updateTask: async (_, { id, task }) => {
console.log('Request received in resolver');
try {
const { title, description, tags, done } = task;
const result = await taskService.updateTask(
id,
title,
description,
tags,
done
);
return {
status: result.status,
message: result.message,
task: result.task,
};
} catch (err) {
console.error('Error while updating task: ', err);
throw new Error(err.message || 'Internal server error');
}
},
deleteTask: async (_, { id }) => {
console.log('Request received in resolver');
try {
const result = await taskService.deleteTask(id);
return result;
} catch (err) {
console.error('Error while deleting task: ', err);
throw new Error(err.message || 'Internal server error');
}
},
},
};
export default resolvers;
and index.js file:
import express from 'express';
import sequelize from './database.js';
import { graphqlHTTP } from 'express-graphql';
import schema from './graphql/schema.js';
import resolvers from './graphql/resolvers.js';
const app = express();
app.use(express.json());
app.use(
'/graphql',
graphqlHTTP({
schema,
rootValue: resolvers,
graphiql: true,
})
);
const PORT = process.env.PORT || 3000;
await sequelize.sync({ force: false });
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
When I send a query I can't see any changes in database and a receive a respond with error. Also, I can't see any logs from resolvers in console. My request:
mutation CreateTask {
createTask(task: { title: "test", description: "test", tags: ["test"] }) {
id
title
description
tags
done
}
}
Response:
{
"errors": [
{
"message": "Cannot return null for non-nullable field Mutation.createTask.",
"locations": [
{
"line": 2,
"column": 5
}
],
"path": [
"createTask"
]
}
],
"data": null
}
I searched for information in various sources, but nothing helped me. I hope someone here knows why the query doesn't trigger resolvers.
I'm new to graphQL and started from 'express-graphql'. I have problem: when I send a query from Postman it can not reach the resolvers. This is my schema:
import { buildSchema } from 'graphql';
export const schema = buildSchema(`
type Task {
id: ID!
title: String!
description: String!
tags: [String!]!
done: Boolean!
}
type Query {
getTodoTasks: [Task]!
getDoneTasks: [Task]!
getTask(id: ID!): Task
findTasks(tags: [String]!): [Task]!
}
input CreateTaskInput {
title: String!
description: String!
tags: [String]!
}
input UpdateTaskInput {
title: String!
description: String!
tags: [String]!
done: Boolean!
}
type Mutation {
createTask(task: CreateTaskInput!): Task!
updateTask(id:ID!, task: UpdateTaskInput!): Task!
deleteTask(id:ID!): Boolean!
}
`);
console.log('GraphQL Schema:', schema);
export default schema;
resolvers:
import { TaskService } from '../services/TaskService.js';
const taskService = new TaskService();
const resolvers = {
Query: {
getTodoTasks: async () => {
try {
const toDoTasks = await taskService.getTasks();
return toDoTasks.filter((task) => !task.done);
} catch (err) {
throw new Error(err.message);
}
},
getDoneTasks: async () => {
try {
const doneTasks = await taskService.getDoneTasks();
return doneTasks;
} catch (err) {
throw new Error(err.message);
}
},
getTask: async (_, { id }) => {
try {
const task = await taskService.getTask(id);
return task;
} catch (err) {
throw new Error(err.message);
}
},
findTasks: async (_, { tags }) => {
try {
const tasks = await taskService.findTasks(tags);
return tasks;
} catch (err) {
throw new Error(err.message);
}
},
},
Mutation: {
createTask: async ({ task }) => {
console.log('Request received in resolver');
try {
const { title, description, tags } = task;
const result = await taskService.addTask(title, description, tags);
console.log(result);
return result;
} catch (err) {
console.error('Error while creating task: ', err);
throw new Error(err.message || 'Internal server error');
}
},
updateTask: async (_, { id, task }) => {
console.log('Request received in resolver');
try {
const { title, description, tags, done } = task;
const result = await taskService.updateTask(
id,
title,
description,
tags,
done
);
return {
status: result.status,
message: result.message,
task: result.task,
};
} catch (err) {
console.error('Error while updating task: ', err);
throw new Error(err.message || 'Internal server error');
}
},
deleteTask: async (_, { id }) => {
console.log('Request received in resolver');
try {
const result = await taskService.deleteTask(id);
return result;
} catch (err) {
console.error('Error while deleting task: ', err);
throw new Error(err.message || 'Internal server error');
}
},
},
};
export default resolvers;
and index.js file:
import express from 'express';
import sequelize from './database.js';
import { graphqlHTTP } from 'express-graphql';
import schema from './graphql/schema.js';
import resolvers from './graphql/resolvers.js';
const app = express();
app.use(express.json());
app.use(
'/graphql',
graphqlHTTP({
schema,
rootValue: resolvers,
graphiql: true,
})
);
const PORT = process.env.PORT || 3000;
await sequelize.sync({ force: false });
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
When I send a query I can't see any changes in database and a receive a respond with error. Also, I can't see any logs from resolvers in console. My request:
mutation CreateTask {
createTask(task: { title: "test", description: "test", tags: ["test"] }) {
id
title
description
tags
done
}
}
Response:
{
"errors": [
{
"message": "Cannot return null for non-nullable field Mutation.createTask.",
"locations": [
{
"line": 2,
"column": 5
}
],
"path": [
"createTask"
]
}
],
"data": null
}
I searched for information in various sources, but nothing helped me. I hope someone here knows why the query doesn't trigger resolvers.
Share Improve this question edited Feb 15 at 7:31 Анна Некрасова asked Feb 14 at 17:29 Анна НекрасоваАнна Некрасова 11 bronze badge 1- Help us help you - share the request you're sending and the error you receive for it. – Mureinik Commented Feb 14 at 19:02
1 Answer
Reset to default 0There are a few issues here:
- Don't use
express-graphql
. That was deprecated in favor ofgraphql-http
- You are passing resolvers as a
rootValue
, and those are not the same thing.
I generally recommend using graphql-tools
and its makeExecutableSchema
if you're not going to use Apollo tooling.
If you really wanted to successfully use the deprecated express-graphql
, your code would look like this:
import express from 'express';
import sequelize from './database.js';
import { graphqlHTTP } from 'express-graphql';
import { makeExecutableSchema } from 'graphql-tools';
import typeDefs from './graphql/typeDefs.js';
import resolvers from './graphql/resolvers.js';
const app = express();
app.use(express.json());
app.use(
'/graphql',
graphqlHTTP({
schema: makeExecutableSchema({ typeDefs, resolvers }),
graphiql: true,
})
);
const PORT = process.env.PORT || 3000;
await sequelize.sync({ force: false });
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Some notes:
- Instead of
buildSchema
to export a schema, you would export the typeDefs as usually a string (or usegql
). - That string is generally referred to as
typeDefs
, so in my example code, I renamed your file to that too.