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

Client preset generates duplicate fragments definitions

Open banou26 opened this issue 3 years ago • 6 comments

Which packages are impacted by your issue?

@graphql-codegen/client-preset^1.2.6

Describe the bug

Using these definitions

export const COMMON_FRAGMENT = gql(`
  fragment CommonFragment on Common {
    common
    common2
  }
`)

export const FOO_FRAGMENT = gql(`
  fragment FooFragment on Foo {
    ...CommonFragment
    foo
    foo2
    bar {
      ...BarFragment
    }
  }
`)

export const BAR_FRAGMENT = gql(`
  fragment BarFragment on Bar {
    ...CommonFragment
    bar
    bar2
  }
`)

export const _GET_FOO = gql(`
  query GetFoo {
    foo {
      ...FooFragment
    }
  }
`)

the client preset generates a GetFoo document that contains the CommonFragment twice, which causes most servers to reject the request

Your Example Website or App

graphql-code-generator-issue-8842

Steps to Reproduce the Bug or Issue

  1. npm i && npm run dev
  2. Go to http://localhost:2222
  3. open devtools and check out the logs

Expected behavior

Document should not contain duplicate fragments

Screenshots or Videos

image

Platform

  • NodeJS: 19.0.1
    "graphql": "^16.6.0",
    "@graphql-codegen/cli": "^2.16.4",
    "@graphql-codegen/client-preset": "^1.2.6",
    "@graphql-codegen/typescript-react-apollo": "^3.3.7",
    "@graphql-codegen/typescript-resolvers": "^2.7.12",

Codegen Config File

const config: CodegenConfig = {
  schema: [typeDefs],
  documents: ['src/**/*.ts', 'src/**/*.tsx'],
  generates: {
    './src/generated/': {
      preset: 'client',
      plugins: ['typescript-resolvers'],
      presetConfig: {
        gqlTagName: 'gql',
        fragmentMasking: false
      }
    }
  },
  ignoreNoDocuments: true
}

banou26 avatar Jan 21 '23 22:01 banou26

I have the same Problem, resulting in nextjs outputing the generated Definition files in the Server Console, with a Error that my Fragment is defined twice in the generated Typescript file.

Julhol-droid avatar Jan 25 '23 21:01 Julhol-droid

Same for me, identical config file.

PiotrBryla avatar Jan 30 '23 14:01 PiotrBryla

I managed to make it work, by deleting the preset: "client" line in the Config File. However I have the following plugins: 'typescript', 'typescript-operations', 'typescript-react-apollo' Maybe it works for you as well.

import type { CodegenConfig } from '@graphql-codegen/cli'
import * as dotenv from 'dotenv-flow'

dotenv.config()

const config: CodegenConfig = {
  schema: process.env.NEXT_PUBLIC_WORDPRESS_URL + '/graphql',
  documents: 'src/graphql/**/*.graphql',
  generates: {
    'src/__generated__/graphql.tsx': {
      plugins: ['typescript', 'typescript-operations', 'typescript-react-apollo']
    }
  },
  verbose: true,
  config: {
    namingConvention: {
      enumValues: 'change-case-all#upperCase'
    }
  }
}

export default config

Julhol-droid avatar Jan 30 '23 14:01 Julhol-droid

Same for me. Tried a similar config to @Julhol-droid's but that no longer auto-includes fragments.

Looking at the generated gql.ts file, indeed there are two declarations for the duplicate fragment. One in the documents variable, one separate.

Any idea how to fix this short of just dropping fragment auto-import altogether?

c10b10 avatar Jan 31 '23 16:01 c10b10

Hey everyone, we are aware of this issue and we will tackle this issue next week!

n1ru4l avatar Feb 01 '23 10:02 n1ru4l

Hey everyone, I created https://github.com/dotansimha/graphql-code-generator/pull/8971 for resolving this once and for all. You can already start using the canary versions of the PR and provide feedback here.

n1ru4l avatar Feb 08 '23 15:02 n1ru4l