【问题标题】:How to access data loaded with gatsby-source-graphql in a resolver?如何在解析器中访问用 gatsby-source-graphql 加载的数据?
【发布时间】:2020-04-01 22:31:59
【问题描述】:

在 Gatsby 中,如何创建使用 gatsby-source-graphql 插件加载的数据的解析器。我无法弄清楚在解析器中查询数据。这可以做到吗?关于我所缺少的任何建议都会有所帮助。

gatsby-node.js 中的类似内容 ...

exports.createResolvers = ({ createResolvers }) => {
  createResolvers({
    Query: {
      getStructure: {
        type: `Structure`,
        async resolve(source, args, context, info) {

          // assume gatsby-config.js is configured with gatsby-source-graphql and this node exists
          const myGraphQlApiNode = await context.nodeModel.runQuery({
            query: {
              filter: {
                fieldName: { eq: "myGqlApi" }
              }
            },
            type: "GraphQLSource"
          });

          const someGqlApiData = // query all of type MyGqlApi_SomeTypeFromGqlApi loaded via gatsby-source-graphql

          return toStructure(someGqlApiData)
        }
      }
    }
  });
};

【问题讨论】:

    标签: graphql gatsby


    【解决方案1】:

    这可能是一个非官方的解决方案 因为 gatsby 没有在 createResolvers 中提供 graphql 方法,而是在 createPages 上提供

    你可以这样做

    在 gatsby-node.js 中

    let apiHelperGraphql = null
    
    exports.createPages = async ({ actions, graphql }) => {
    // steal it from create Pages
      apiHelperGraphql = graphql
    }
    exports.createResolvers = ({ createResolvers }) => {
    // and call it here, do what ever you want
    apiHelperGraphql(`same as grapql syntax`)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-08
      • 2021-07-02
      • 1970-01-01
      • 2020-08-11
      • 2021-07-27
      • 2019-04-02
      • 1970-01-01
      • 2020-07-23
      • 2019-06-22
      相关资源
      最近更新 更多