【问题标题】:Gatsby graphql resolver query not returning all fieldsGatsby graphql 解析器查询未返回所有字段
【发布时间】:2022-01-10 18:03:46
【问题描述】:

我有这个解析器查询,我在其中过滤所有降价条目以构建一个集合:

export const createResolvers = ({createResolvers}) => {
    const resolvers = {
        Query : {
            allPosts     : {
                type   : ["Post"],
                args   : { limit: `Int`, skip: `Int` },
                resolve: async (source, args, context, info) => {
                    const { entries } = await context.nodeModel.findAll({
                        type : "MarkdownRemark",
                        query: {
                            limit : args.limit,
                            skip  : args.skip,
                            filter: {
                                fileAbsolutePath: {
                                    regex: "//collections/posts//"
                                }
                            }
                        }
                    });

                    return entries;
                }
            },
        }
    }
}

它返回一个Post类型的集合:

    type Post implements Node @dontInfer {
        id: ID!
        html: String
        rawMarkdownBody: String
        fileAbsolutePath: String
        ...
    }

我选择的MarkdownRemark 条目有一个html 字段,其中包含markdown 文件的html 输出。我可以在 Graphiql 游乐场/控制台中看到它。

当我使用allPosts 查询时,html 字段为空。我得到rawMarkdownBody 和其他字段,但没有得到html

有什么想法吗?

【问题讨论】:

    标签: reactjs graphql gatsby


    【解决方案1】:

    这里的问题是 createResolvers 转换器之后运行,因此在解析器运行时,MarkdownRemark 类型的其他节点已经添加了 html 字段。 p>

    您可以改为在 createSchemaCustomization 中进行更改。例如,这样的事情(可能无法逐字使用,但想让您了解要点):

    exports.createSchemaCustomization = async ({
      actions: { createTypes },
      schema,
    }) => {
      createTypes([
        schema.buildObjectType({
          name: "Query"
          fields: {
            allPosts: {
              type: ["Post"],
              args: { limit: `Int`, skip: `Int` },
              resolve: async (source, args, context, info) => {
                // …       
              },
            },
          },
        })
      ])
    }
    

    【讨论】:

    • 我有时间对此进行了更长的调查,似乎 html 字段在第一次查询之前不会呈现,这就是为什么在我创建解析器时它返回 null 的原因。就我而言,问题是我构建了allPosts,然后对其进行查询,而不是在allMarkdownRemark 上进行查询,这会阻止markdownremark 生成html,因为没有针对它运行任何查询。一些见解:github.com/gatsbyjs/gatsby/issues/17045
    • @RomeoMihalcea 我不确定你的情况是否重要。如果您不需要查询 html 字段,则不必担心解析器在查询时动态创建它,您只需在备注插件运行之前注册架构更改。
    • 它似乎不起作用或者我做错了。我创建了一个演示(来自一个玩具博客),您可以查看 node.js 文件。尝试使用buildObjectType 或使用createResolvers 进一步创建它。无论在哪里,html 字段都返回 null。感谢您抽出宝贵的时间
    • 忘记添加 repo 文件:github.com/ciokan/surron-parts/blob/resolvers/…(这是在 resolvers 分支上)
    猜你喜欢
    • 2019-07-23
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    • 2019-07-08
    • 2021-06-20
    相关资源
    最近更新 更多