Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client preset generates duplicate fragments definitions #8842

Closed
Banou26 opened this issue Jan 21, 2023 · 6 comments · Fixed by #8971
Closed

Client preset generates duplicate fragments definitions #8842

Banou26 opened this issue Jan 21, 2023 · 6 comments · Fixed by #8971
Assignees
Labels
kind/bug Bug :-(

Comments

@Banou26
Copy link

Banou26 commented Jan 21, 2023

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
}
@Julhol-droid
Copy link

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.

@PiotrBryla
Copy link

Same for me, identical config file.

@Julhol-droid
Copy link

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

@n1ru4l n1ru4l added the kind/bug Bug :-( label Jan 31, 2023
@c10b10
Copy link

c10b10 commented Jan 31, 2023

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?

@n1ru4l
Copy link
Collaborator

n1ru4l commented Feb 1, 2023

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

@n1ru4l
Copy link
Collaborator

n1ru4l commented Feb 8, 2023

Hey everyone, I created #8971 for resolving this once and for all. You can already start using the canary versions of the PR and provide feedback here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Bug :-(
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants