next.js codegen“typescript-react-apollo”插件在生成的输出中重新声明块作用域变量

isr3a4wc  于 2023-06-22  发布在  TypeScript
关注(0)|答案(1)|浏览(103)

我刚刚开始了一个新的项目,它使用NextJS和graphql-codegen来从我的API生成apollo-react类型。然而,马上它创建了重复的导出变量,我不能构建我的项目,由于typescript错误。关于如何防止codegen创建这些副本有什么想法?

codegen.ts

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

const config: CodegenConfig = {
  overwrite: true,
  documents: ["src/**/*.tsx"],
  schema: process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT,
  generates: {
    "./src/graphql/__generated__/": {
      preset: "client",
      plugins: ["typescript-react-apollo"],
    },
  },
};
export default config;
codegen error
// ./src/graphql/__generated__/
...
// creates two 'UserDocument' variables
export const UserDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"User"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"userId"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"ObjectId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"user"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"Variable","name":{"kind":"Name","value":"userId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"email"}},{"kind":"Field","name":{"kind":"Name","value":"image"}},{"kind":"Field","name":{"kind":"Name","value":"hashedPassword"}},{"kind":"Field","name":{"kind":"Name","value":"role"}}]}}]}}]} as unknown as DocumentNode<UserQuery, UserQueryVariables>;

export const UserDocument = gql`
    query User($userId: ObjectId) {
  user(id: $userId) {
    id
    name
    email
    image
    hashedPassword
    role
  }
}
    `;
...
eimct9ow

eimct9ow1#

我最终转储了generates: { preset: 'client' }字段,并传入了所有我自己的插件并更新了配置:

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

const config: CodegenConfig = {
  overwrite: true,
  documents: ["src/**/*.tsx"],
  schema: process.env.NEXT_PUBLIC_GRAPHQL_ENDPOINT,
  generates: {
    "./src/graphql/__generated__/graphql.ts": {
      plugins: [
        "typescript",
        "typescript-operations",
        "typescript-react-apollo",
      ],
    },
  },
};
export default config;

这似乎工作。

相关问题