【发布时间】:2020-12-10 10:13:12
【问题描述】:
我在 GraphQL 中封装了一系列 REST API,但无法让解析器正常工作。
我的 typeDefs
const typeDefs = `
type Page {
id: String
path: String!
title: String!
heading: String
type: String
html: String
summary: String
}
type Site {
page(path: String!): Page
}
type Query {
site: Site
}`
和我的解析器
const resolvers = {
Query: {
site: {
page: async (_root, { path }) => getPage(path)
}
}
}
如果我尝试这个查询
{
site {
page(path: "/services/research-request") {
id
path
title
html
type
summary
}
}
}
我回来了
{
"data": {
"site": null
}
}
但是,如果我不在网站内嵌套页面
const typeDefs = `
type Page {
id: String
path: String!
title: String!
heading: String
type: String
html: String
summary: String
}
type Query {
page(path: String!): Page
}`
并使用解析器
const resolvers = {
Query: {
page: async (_root, { path }) => getPage(path)
}
}
那我试试这个查询
{
page(path: "/services/research-request") {
id
path
title
html
type
summary
}
}
我回来了,正确的,
{
"data": {
"page": {
"id": "d290f1ee-6c54-4b01-90e6-d701748f0851",
"path": "/services/research-request",
"title": "Research | Requests",
"html": "<p>This is a really well put together Research Requests page<p>\n",
"type": "page",
"summary": "A short piece of summary text"
}
}
}
我将从多个 API 中提取数据来填充我的图表,因此我想根据 site 与 user 等对各个部分进行分组,但在尝试嵌套时我遗漏了一些明显的东西解析器。我做错了什么?
【问题讨论】:
标签: graphql graphql-js graphql-tools