在不同文件中使用graphql-codegen查询时出现运行时错误。

huangapple go评论72阅读模式
英文:

Runtime error when using graphql-codegen query inside different file

问题

我有一个使用ReactJS、TypeScript和GraphQL编写的项目。我使用graphql-codegen npm包来生成GraphQL类型。

我有这段代码:

import { useQuery } from "@apollo/client";

import { gql } from './__generated__/gql';

export const POLL_SET = gql(`
query PollSet {
  sets {
    name
    polls {
      name
      questions {
        question_id
        intro_text
        outro_text
        options {
          option_id
          label
          valid
        }
      }
    }
  }
}
`);

function App() {
    const { loading, error, data } = useQuery(POLL_SET);
}

export default App;

问题是,当我将查询放入一个文件中,只需复制并粘贴然后添加命名导出,然后使用:

import { POLL_SET } from './queries';

我得到一个运行时错误:

Invariant Violation: Argument of [object Object] passed to parser was not a valid GraphQL DocumentNode. You may need to use 'graphql-tag' or another method to convert your operation into a document
    at new InvariantError2 (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:31:26)
    at invariant (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:42:11)
    at parser (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16327:13)
    at verifyDocumentType (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16371:19)
    at new InternalState2 (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16416:5)
    at useInternalState (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16388:24)
    at useQuery (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16383:10)
    at App (http://localhost:5173/src/App.tsx?t=1680805881602:29:7)
    at renderWithHooks (http://localhost:5173/node_modules/.vite/deps/chunk-QJV3R4PZ.js?v=1e4a2ee6:12171:26)
    at mountIndeterminateComponent (http://localhost:5173/node_modules/.vite/deps/chunk-QJV3R4PZ.js?v=1e4a2ee6:14921:21)

奇怪的是,当我将相同的gql调用添加到我的App.tsx中时,它起作用。甚至在我添加以下内容时也起作用:

const POLL_SET = false && gql(`
query PollSet {
  sets {
    name
    polls {
      name
      questions {
        question_id
        intro_text
        outro_text
        options {
          option_id
          label
          valid
        }
      }
    }
  }
}
`);

问题是如何将所有查询保存在一个单独的文件中?

英文:

I have a project written in ReactJS, TypeScript, and GraphQL. I use graphql-codegen npm package to generate the types for GraphQL.

I have this code:

import { useQuery } from "@apollo/client";

import { gql } from './__generated__/gql';

export const _POLL_SET = gql(`
query PollSet {
  sets {
    name
    polls {
      name
      questions {
        question_id
        intro_text
        outro_text
        options {
          option_id
          label
          valid
        }
      }
    }
  }
}
`);

function App() {
    const { loading, error, data } = useQuery(POLL_SET);

}

export default App;

The problem is that when I put the query into a file, just copy paste and adding named export and then using:

import { POLL_SET } from './queries';

I got a runtime error:

Invariant Violation: Argument of [object Object] passed to parser was not a valid GraphQL DocumentNode. You may need to use 'graphql-tag' or another method to convert your operation into a document
    at new InvariantError2 (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:31:26)
    at invariant (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:42:11)
    at parser (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16327:13)
    at verifyDocumentType (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16371:19)
    at new InternalState2 (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16416:5)
    at useInternalState (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16388:24)
    at useQuery (http://localhost:5173/node_modules/.vite/deps/@apollo_client.js?v=1e4a2ee6:16383:10)
    at App (http://localhost:5173/src/App.tsx?t=1680805881602:29:7)
    at renderWithHooks (http://localhost:5173/node_modules/.vite/deps/chunk-QJV3R4PZ.js?v=1e4a2ee6:12171:26)
    at mountIndeterminateComponent (http://localhost:5173/node_modules/.vite/deps/chunk-QJV3R4PZ.js?v=1e4a2ee6:14921:21)

what's weird is that when I add the same call to gql in my App.tsx it works. It even works when I add:

const _POLL_SET = false && gql(`
query PollSet {
  sets {
    name
    polls {
      name
      questions {
        question_id
        intro_text
        outro_text
        options {
          option_id
          label
          valid
        }
      }
    }
  }
}
`);

And the question is how can I keep all my queries in a separated file?

答案1

得分: 1

问题出在我的 codegen.ts 脚本上:

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

import { GRAPHQL_ENDPOINT } from './config.node';

const config: CodegenConfig = {
    schema: GRAPHQL_ENDPOINT,
    documents: ['src/**/*.tsx'],
    generates: {
        './src/__generated__/': {
            preset: 'client',
            plugins: [],
            presetConfig: {
                gqlTagName: 'gql',
            }
        }
    },
    ignoreNoDocuments: true,
};

export default config;

我创建了一个名为 queries.ts 的文件,生成器只查找 tsx 文件。

通过添加以下内容解决了问题:

    documents: ['src/**/*.tsx', 'src/**/*.ts'],

这解决了问题。我保留这个问题,以防有人犯类似的错误。我本来以为问题出在我的代码中,而不是生成器中。

英文:

The problem was my codegen.ts script:

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

import { GRAPHQL_ENDPOINT } from './config.node';

const config: CodegenConfig = {
    schema: GRAPHQL_ENDPOINT,
    documents: ['src/**/*.tsx'],
    generates: {
        './src/__generated__/': {
            preset: 'client',
            plugins: [],
            presetConfig: {
                gqlTagName: 'gql',
            }
        }
    },
    ignoreNoDocuments: true,
};

export default config;

I've created a file queries.ts and the generator was only looking into tsx files.

Adding:

    documents: ['src/**/*.tsx', 'src/**/*.ts'],

solved the issue. I keep this question just in case someone had a similar mistake. I was expecting a problem with my code, not with a generator.

huangapple
  • 本文由 发表于 2023年4月7日 02:43:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定