Gatsby build-time 的 Graphql 请求未发送 headers

Graphql request at Gatsby build-time doesn't send headers

我在 front-end (Gatsby) 和 pollo 之间添加了身份验证。这适用于 run-time 的请求。但是在 build-time 没有 header 没有发送到 Apollo。我有以下代码:

// SERVER SIDE
// apollo server setup
const { ApolloServer, AuthenticationError } = require("apollo-server-lambda")
... //other imports

const server = new ApolloServer({
  context: ({ event, context }) => {
    if (!event.headers.authorization) {
        // No authentication header
        throw new AuthenticationError("must authenticate")
    }
    // Authenticate
  },
  resolvers,
  schema: schemaWithResolvers,
  introspection: true,
  playground: true,
  formatError: (err) => {
    console.log("Error", err)
    return err
  },
})
// CLIENT SIDE
// apollo-client setup
import { ApolloClient } from "apollo-client"

const client = new ApolloClient({
    link: ApolloLink.from([
        onError (.......),
        new HttpLink({
          uri: process.env.REACT_APP_GQL_SERVER,
          credentials: "same-origin",
          headers: {
            Authorization: 'Basic ' + '<login>:<password>', //works at run-time, not at build-time
          },
        }),
      ]),
  cache: new InMemoryCache(),
  fetch,
})
// CLIENT SIDE
// gatsby-node (during build-time)
// => This call gets rejected during buildtime because authorization header is not available.
exports.createPages = async function ({ actions, graphql }) {
  const { data } = await graphql(`
    query {
      apollo {
        allWathever {
          id
        }
      }
    }
  `)
}

我的 apollo-client 安装程序在运行时将授权 header 添加到我的 graphql 请求。但是在 build-time 我收到错误,因为没有 header 可用。 在 build-time 期间发送的 header 我错过了什么?或者我能否以某种方式检测 build-time 期间是否触发了呼叫,以便我可以忽略身份验证过程? 非常感谢。

感谢@xadm,我发现要在 build-time 使用源数据,您需要安装一个插件并将其添加到您的配置中。我使用 gatsby-source-graphql 并忘记在此处添加 headers。

//gatsby-config-js
    {
      resolve: "gatsby-source-graphql",
      options: {
        typeName: "Apollo",
        fieldName: "apollo",
        createLink: (pluginOptions) => {
          return createHttpLink({
            uri: "your uri",
            fetch,
            credentials: "same-origin",
            headers: {
              Authorization: "your authorization",
            },
          })
        },
      },
    },

关于此的文章:https://www.gatsbyjs.com/docs/data-fetching/#fetching-data-at-build-time