【问题标题】:How to nest qraphql resolvers so as to group them如何嵌套 qraphql 解析器以便对它们进行分组
【发布时间】: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 中提取数据来填充我的图表,因此我想根据 siteuser 等对各个部分进行分组,但在尝试嵌套时我遗漏了一些明显的东西解析器。我做错了什么?

【问题讨论】:

    标签: graphql graphql-js graphql-tools


    【解决方案1】:

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

    命名空间应该更好:sitePagesiteOptionsuserProfileuserSettings ... 在客户端重命名时使用别名(或在文件中替换)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-27
      • 2012-05-06
      相关资源
      最近更新 更多