0

我在 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 中提取数据来填充我的图表,因此想根据sitevsuser等对各个部分进行分组,但是在尝试嵌套解析器时我遗漏了一些明显的东西。我究竟做错了什么?

4

1 回答 1

0

graphql 中没有“免费”嵌套...嵌套>下一级深度>下一个类型...站点解析器,site.page 解析器...但还有自己的站点ID(用于客户端缓存),重建/调整所有客户端代码等。 ...

命名空间应该更好:sitePage, siteOptions, userProfile, userSettings... 使用别名(或在文件中替换)在客户端重命名。

于 2020-08-21T05:24:21.563 回答