dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.88k stars 1.34k forks source link

[client-preset] if gqlTagName is changed doesn't generate any documents #9820

Open Yagogc opened 10 months ago

Yagogc commented 10 months ago

Which packages are impacted by your issue?

@graphql-codegen/client-preset

Describe the bug

I need to generate types for 2 differents schemas, so to avoid clashing with the ts pluging @0no-co/graphqlsp I'm separating them by the use of the gql tag.

The problem is that when I change the presetConfig.gqlTagName to something else that's not graphql or gql when tries to generate the documents it's not able to parse the new tagName creating an empty document array in gql.ts

Your Example Website or App

none

Steps to Reproduce the Bug or Issue

  1. In codegen.ts for the client preset change the gqlTagName to something else. i.e:

      presetConfig: {
        gqlTagName: 'gqlX',
      },
  2. Modify one of the queries w/ the new function name:

    
    import { gqlX } from '../generated/X';

const pageCollectionQuery = gqlX( query PageCollection { pageCollection { items { sys { id } } } } );

export { pageCollectionQuery };


3. after running `codegen` the updated `gql.ts` file  will have an empty `document` variable:

```ts
const documents = [];

Expected behavior

I expect that the gql.ts document variable is populated as expected:

const documents = {
    "\n  query something {\n    pageCollection {\n      items {\n        sys {\n          id\n        }\n        title\n        slug\n      }\n    }\n  }\n": types.SomethingDocument,
};

Screenshots or Videos

No response

Platform

Codegen Config File

No response

Additional context

// codegen.ts

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

import env from './env';

const schema1Endpoint = env.SCHEMA_1;
const schema2Endpoint = env.SCHEMA_2;

const config: CodegenConfig = {
  overwrite: true,
  ignoreNoDocuments: true,
  generates: {
    './src/graphql/generated/schema1/': {
      schema: schema1Endpoint,
      documents: './src/graphql/schema1/**/*.{ts,tsx}',
      preset: 'client',
      presetConfig: {
        gqlTagName: 'gqlSchema1',
      },
      config: {
        useTypeImports: true,
      },
    },
    './src/graphql/generated/schema1/schema.graphql': {
      schema: schema1Endpoint,
      plugins: ['schema-ast'],
    },
    './src/graphql/generated/schema2/': {
      documents: './src/graphql/schema2/**/*.{ts,tsx}',
      schema: schema2Endpoint,
      preset: 'client',
      presetConfig: {
        gqlTagName: 'gqlSchema2',
      },
      config: {
        useTypeImports: true,
      },
    },
    './src/graphql/generated/schema2/schema.graphql': {
      schema: schema2Endpoint,
      plugins: ['schema-ast'],
    },
  },
};

export default config;
tsconfig.json

...
"plugins": [
  {
    "name": "@0no-co/graphqlsp",
    "schema": "./src/graphql/generated/schema1/schema.graphql",
    "disableTypegen": true,
    "templateIsCallExpression": true,
    "template": "gqlSchema1"
  }
  {
    "name": "@0no-co/graphqlsp",
    "schema": "./src/graphql/generated/schema2/schema.graphql",
    "disableTypegen": true,
    "templateIsCallExpression": true,
    "template": "gqlSchema2"
  }
],
...
abhinaypandey02 commented 5 months ago

Bump! Facing same!

PelicanQ commented 4 months ago

I have the same problem! Surprised to not see others with the same since Apollo teaches you to use gqlTagName here https://www.apollographql.com/tutorials/lift-off-part1/09-codegen