question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

duplicate fragments within queries

See original GitHub issue

Describe the bug

When using fragments in queries which use fragements in multiple positions, the fragments are beeing repeated in the string concatenation, so that most server will complain about it Error: There can be only one fragment named "addressFields".

Your Example Website or App

https://github.com/bastiion/graphql-codegen-duplicate-fragments-issue

Steps to Reproduce the Bug or Issue

  1. formulate a query that uses same fragments within two other fragments
  2. run codegen

Expected behavior

fragments must be deduplicated

a work-arround can be ssen here, which would also be an entry point for a bug fix https://github.com/bastiion/graphql-codegen-duplicate-fragments-issue/commit/d08f51ecdd7c3cb6b7af68c7bd24aae5b4a5808a#diff-ca580bb0b9ca66ec74fb97288ee12d571481819ed8e689ea99b2d6ff79dacc24

Screenshots or Videos

No response

Platform

Codegen Config File

overwrite: true
schema:
  - http://localhost:9002/graphql
documents: 'graphql/**/*.graphql'
generates:
  graphql/generated/index.ts:
    plugins:
      - 'typescript'
      - 'typescript-operations'
      - 'typescript-react-query'
    config:
      pureMagicComment: true
      fetcher:
        func: ../fetcher#useFetchData
        isReactHook: true

Additional context

example query:

fragment addressFields on Address {
  address
  city
}

fragment personFields on Person {
  name
  address {
    ...addressFields
  }
}

query company {
  company {
    name
    address {
      ...addressFields
    }
    employees {
      ...personFields
    }
  }
}

will produce:

export const AddressFieldsFragmentDoc = /*#__PURE__*/ `
    fragment addressFields on Address {
  address
  city
}
    `;
export const PersonFieldsFragmentDoc = /*#__PURE__*/ `
    fragment personFields on Person {
  name
  address {
    ...addressFields
  }
}
    ${AddressFieldsFragmentDoc}`;
export const CompanyDocument = /*#__PURE__*/ `
    query company {
  company {
    name
    address {
      ...addressFields
    }
    employees {
      ...personFields
    }
  }
}
    ${AddressFieldsFragmentDoc}
${PersonFieldsFragmentDoc}`;

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:6
  • Comments:14

github_iconTop GitHub Comments

1reaction
bernharduwcommented, Nov 1, 2022

I can confirm it’s working for me with v1.1.2!

Good work @tojump + @charlypoly, thanks! 🎉

1reaction
charlypolycommented, Nov 1, 2022

@tojump, I’ve released your contribution; thanks!

@AssisrMatheus @bernharduw, could you try with @graphql-codegen/client-preset@1.1.2? 📦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fragment duplication on Fragment Transaction - Stack Overflow
I have tried referencing the fragments parent view to replace it, and the fragment itself (by id) and still, it only adds the...
Read more >
Fragments - Apollo GraphQL Docs
A GraphQL fragment is a piece of logic that can be shared between multiple queries and mutations. ... Every fragment includes a subset...
Read more >
Duplicate fragments errors - Dgraph Cloud
I am building an app using Dgraph Cloud and found unexpected behaviour when using fragments. I am not able to use duplicate fragments...
Read more >
Code Inspection: Duplicated code fragment - DataGrip
The inspection options allow you to select the scope of the reported duplicated fragments and set the initial size for the duplicated language ......
Read more >
Fragments | Relay
Instead, they need to be included in a query, either directly or transitively. This means that all fragments must belong to a query...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found