我在 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
vsuser
等对各个部分进行分组,但是在尝试嵌套解析器时我遗漏了一些明显的东西。我究竟做错了什么?