Vercel 应用程序出现 graphql-codegen 端点错误无法找到以下指针的任何 GraphQL 类型定义

nee*_*eko 8 graphql next.js vercel graphql-codegen

我加载 GraphQL 架构,如下所示:

const schema = loadSchemaSync('./src/graphql/server/schema/*.gql', {
  loaders: [new GraphQLFileLoader()],
})
Run Code Online (Sandbox Code Playgroud)

这在本地工作正常,但是,当部署到 vercel 时,我收到错误:

Unable to find any GraphQL type definitions for the following pointers:
          - ./src/graphql/server/schema/*.gql
Run Code Online (Sandbox Code Playgroud)

我认为这是因为 vercel 在构建后删除了相关文件?

小智 0

问题是您无法在 Vercel Serverless Functions 中使用动态加载器。

此问题的解决方法是使用内联 GraphQL 架构。

// src/graphql/schema.ts

import { gql } from "apollo-server-core";

export default gql`
  type Query {
    greet: String!
  }
`;

Run Code Online (Sandbox Code Playgroud)
// src/pages/api/graphql.ts

import { ApolloServerPluginLandingPageGraphQLPlayground } from "apollo-server-core";

import Schema from "../../graphql/schema";

const apolloServer = new ApolloServer({
  typeDefs: Schema,
  resolvers,
  plugins: [ApolloServerPluginLandingPageGraphQLPlayground],
  introspection: true,
});
Run Code Online (Sandbox Code Playgroud)

如果您使用像 codegen 这样的工具:

// codegen.ts

import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
  schema: "src/graphql/schema.ts",
  documents: ["./src/**/*.{ts,tsx}"],
  ignoreNoDocuments: true,
  generates: {
    "src/graphql/types/server.ts": {
      plugins: [
        "@graphql-codegen/typescript",
        "@graphql-codegen/typescript-resolvers",
      ],
    },
    "src/graphql/types/client/": {
      preset: "client",
      plugins: [],
    },
  },
};

export default config;
Run Code Online (Sandbox Code Playgroud)