我在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
等对各个部分进行分组,但是我缺少明显的东西尝试嵌套解析器时。我在做什么错了?
答案 0 :(得分:0)
graphql中没有'free'嵌套...嵌套>下一级深度>下一个TYPE ...网站解析器,site.page解析器...但也拥有自己的网站ID(用于客户端缓存),重建/调整所有客户端代码等。
命名空间应该更好:sitePage
,siteOptions
,userProfile
,userSettings
...使用别名(或替换文件)在客户端中重命名。