graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

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

Open Yagogc opened this issue 2 years ago • 4 comments

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',
      },
  1. 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 };
  1. after running codegen the updated gql.ts file will have an empty document variable:
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

  • OS: macOS
  • NodeJS: 20.9.0
  • graphql: 16.8.1,
  • @graphql-codegen/cli: 5.0.0,
  • @graphql-codegen/client-preset: 4.1.0,

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"
  }
],
...

Yagogc avatar Jan 18 '24 13:01 Yagogc

Bump! Facing same!

abhinaypandey02 avatar Jun 22 '24 19:06 abhinaypandey02

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

PelicanQ avatar Jul 31 '24 15:07 PelicanQ