如何在解析器中访问gatsby-source-graphql加载的数据?

时间:2019-12-08 18:10:11

标签: graphql 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)
        }
      }
    }
  });
};

1 个答案:

答案 0 :(得分: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`)
}