为什么我的GraphQl查询会丢弃错误而不是响应?
目前,我正在研究GraphQl-Yoga/Node
。
我有一个index.js文件,如下所示:
import { createServer } from "@graphql-yoga/node";
import { readFileSync } from "node:fs";
import Comment from "./resolvers/Comment";
import Query from "./resolvers/Query";
import Post from "./resolvers/Post";
import User from "./resolvers/User";
import Mutation from "./resolvers/Mutation";
import db from "./db";
const typeDefs = readFileSync("./src/schema.graphql", "utf-8");
const server = createServer({
schema: {
typeDefs,
resolvers: { Query, Mutation, Post, User, Comment },
context: { db },
},
});
server.start();
我有一个用于架构和解析器的单独文件。
模式文件如下:
type Query {
posts(query: String): [Post!]!
users(query: String): [User!]!
comments(query: String): [Comment!]!
}
type Post {
id: ID!
title: String!
body: String!
isPublished: Boolean!
author: User!
comments: [Comment!]!
}
解析器如下:
const Post = {
author(parent, args, { db }, info) {
return db.users.find((user) => {
return user.id === parent.author;
});
},
comments(parent, args, { db }, info) {
return db.comments.filter((comment) => {
return comment.post === parent.id;
});
},
};
export { Post as default}
我有一个单独的查询文件
const Query = {
posts(parent, args, { db }, info) {
if (!args.query) {
return db.posts;
}
return db.posts.filter((post) => {
const isTitleMatch = post.title.toLowerCase().includes(args.query.toLowerCase());
const isBodyMatch = post.body.toLowerCase().includes(args.query.toLowerCase());
return isTitleMatch || isBodyMatch;
});
}
}
export { Query as default };
db是保存静态数据的文件。
const posts = [
{
id: "4",
title: "The alchemist",
body: "There is only one thing that makes a dream impossible to achieve: the fear of failure.",
isPublished: true,
author: "1",
},
{
id: "5",
title: "Dear stranger",
body: "the truth is you are at war with yourself thats why you find yourself at war with others.",
isPublished: false,
author: "3",
},
{
id: "6",
title: "Warren buffet",
body: "The most important thing to do if you find yourself in a hole is to stop digging.",
isPublished: true,
author: "3",
},
];
控制台中没有错误。一切似乎都处于良好的工作状态。但是,当我尝试按照以下所述运行后查询时,我会收到一个错误而不是响应。
query {
posts {
id
title
body
}
}
错误:
{
"errors": [
{
"message": "Unexpected error.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"posts"
]
}
],
"data": null
}
我不确定我在做什么错。 谁能在这里帮我吗?
I'm working on graphql-yoga/node
at the moment.
I've got an index.js file like below:
import { createServer } from "@graphql-yoga/node";
import { readFileSync } from "node:fs";
import Comment from "./resolvers/Comment";
import Query from "./resolvers/Query";
import Post from "./resolvers/Post";
import User from "./resolvers/User";
import Mutation from "./resolvers/Mutation";
import db from "./db";
const typeDefs = readFileSync("./src/schema.graphql", "utf-8");
const server = createServer({
schema: {
typeDefs,
resolvers: { Query, Mutation, Post, User, Comment },
context: { db },
},
});
server.start();
I have a separate files for schema and resolvers.
Schema file is as follows:
type Query {
posts(query: String): [Post!]!
users(query: String): [User!]!
comments(query: String): [Comment!]!
}
type Post {
id: ID!
title: String!
body: String!
isPublished: Boolean!
author: User!
comments: [Comment!]!
}
Resolver is as follow:
const Post = {
author(parent, args, { db }, info) {
return db.users.find((user) => {
return user.id === parent.author;
});
},
comments(parent, args, { db }, info) {
return db.comments.filter((comment) => {
return comment.post === parent.id;
});
},
};
export { Post as default}
I have a separate Query file
const Query = {
posts(parent, args, { db }, info) {
if (!args.query) {
return db.posts;
}
return db.posts.filter((post) => {
const isTitleMatch = post.title.toLowerCase().includes(args.query.toLowerCase());
const isBodyMatch = post.body.toLowerCase().includes(args.query.toLowerCase());
return isTitleMatch || isBodyMatch;
});
}
}
export { Query as default };
db is the file where static data is saved.
const posts = [
{
id: "4",
title: "The alchemist",
body: "There is only one thing that makes a dream impossible to achieve: the fear of failure.",
isPublished: true,
author: "1",
},
{
id: "5",
title: "Dear stranger",
body: "the truth is you are at war with yourself thats why you find yourself at war with others.",
isPublished: false,
author: "3",
},
{
id: "6",
title: "Warren buffet",
body: "The most important thing to do if you find yourself in a hole is to stop digging.",
isPublished: true,
author: "3",
},
];
There are no errors in the console. Everything appears to be in good working order. However, when I try to run the post query as described below, I get an error rather than a response.
query {
posts {
id
title
body
}
}
the Error:
{
"errors": [
{
"message": "Unexpected error.",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"posts"
]
}
],
"data": null
}
I'm not sure what I'm doing wrong.
Can anyone help me here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您在服务器端遇到错误。您可以共享终端/日志输出吗?
另外,
上下文
选项应在根级别,而不是架构。
您可以尝试以下内容吗?
It seems like you're getting an error on the server side. Could you share your terminal/logs output?
Also, the
context
option should be at the root level, not underschema.
Could you try the following?