0

おはよう/午後のスタックオーバーフロー。@graphql-codegen/cliという npm パッケージを使用して、GraphQL スキーマの型定義/ユーティリティを生成しています。最近、graphql-codegen コマンド/スクリプトを実行しようとするたびにエラーが発生します。これは私が得るエラーです

"Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results."
...
"Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed."

この問題は私のプロジェクトではなく、私の環境にあると確信しています。新しいプロジェクトを最初から作成しようとしましたが、同じエラーが発生しました。これが私が試したことです

  • node_modules の再インストール
  • package.json で resolutions プロパティを使用する
  • 異なるバージョンの graphql/graphql-cli パッケージの使用
  • グローバル npm パッケージを完全に消去する

このエラーを解決するためにここ数日を費やしましたが、アイデアがありません。考えや推奨事項は大歓迎です。また、以下は関連ファイルを含むコードサンドボックスへのリンクです

https://codesandbox.io/s/graphql-codegen-cli-example-qq5cj

4

1 に答える 1

0

codegenでも同じ問題がありました。

src/generated/graphql.tsx
    Error: Cannot use GraphQLObjectType "FieldError" from another module or realm.

    Ensure that there is only one instance of "graphql" in the node_modules
    directory. If different versions of "graphql" are the dependencies of other
    relied on modules, use "resolutions" to ensure only one version is installed.

    https://yarnpkg.com/en/docs/selective-version-resolutions

    Duplicate "graphql" modules cannot be used at the same time since different
    versions may have different capabilities and behavior. The data from one
    version used in the function from another could produce confusing and
    spurious results.

codegen.yml の実行

overwrite: true
schema: "http://localhost:4001/graphql"
documents: "src/graphql/**/*.graphql"
generates:
  src/generated/graphql.tsx:
    plugins:
      - "typescript"
      - "typescript-operations"
      - "typescript-urql"

競合するパッケージがあったと思います。問題の原因と思われる「urql」を削除し、再インストールするとエラーが解決しました。

yarn run v1.22.17
$ graphql-codegen --config codegen.yml
  √ Parse configuration
  √ Generate outputs

package.json を使用

{
    "private": true,
    "scripts": {
        "dev": "next dev",
        "build": "next build",
        "start": "next start",
        "gen": "graphql-codegen --config codegen.yml"
    },
    "dependencies": {
        "@chakra-ui/icons": "^1.0.0",
        "@chakra-ui/react": "^1.8.5",
        "@emotion/react": "^11.0.0",
        "@emotion/styled": "^11.0.0",
        "formik": "^2.2.9",
        "framer-motion": "^4.0.3",
        "next": "latest",
        "react": "^17.0.2",
        "react-dom": "^17.0.2",
        "graphql": "^16.3.0",
        "urql": "^2.2.0"
    },
    "devDependencies": {
        "@graphql-codegen/cli": "^2.6.2",
        "@graphql-codegen/typescript": "2.4.5",
        "@graphql-codegen/typescript-operations": "2.3.2",
        "@graphql-codegen/typescript-urql": "^3.5.3",
        "@graphql-codegen/urql-introspection": "^2.1.1",
        "@types/node": "^17.0.21",
        "graphql-tag": "^2.12.6",
        "typescript": "^4.5.5"
    }
}

必要に応じて package.json を編集できます。ファイル「yarn.lock」、「package-lock.json」、およびフォルダー「node_modules」を削除して、依存関係をクリアします。そして、'npm install' / 'yarn install' を実行して、依存関係を再インストールします。

楽しむ!

于 2022-02-27T19:57:17.627 に答える