-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
duplicate fragments within queries #8103
Comments
I'm having the same issue, using the URQL codegen |
I'm also having the same issue. @bastiion have you found a solution? |
I'm also having this issue. |
Also having the same issue. Any workarounds would be appreciated! |
I was able to work around this using a small wrapper around graphql: import uniqBy from "lodash/uniqBy";
import type { DocumentNode } from "graphql";
/**
* Removes duplicate fragments.
* This is a workaround for this issue: @see https://github.com/dotansimha/graphql-code-generator/issues/8103
* */
export default function fixFragments<Query extends DocumentNode>(
query: Query
): Query {
return { ...query, definitions: uniqBy(query.definitions, "name.value") };
} (You can use other techniques for deduplicating the fragments by name – I used lodash because we are already using it in our project) Usage: await graphQLClient.request(
fixFragments(
graphql(/* GraphQL */`
query YourQuery {
yourType {
...InlineFragment1
...InlineFragment2
}
}
`)
)
) |
Hi! Could you try using the import { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
schema: "schema.graphql",
documents: "document.graphql",
generates: {
"types.ts": {
plugins: [/* your plugin */]
config: {
dedupeFragments: true
}
},
},
};
export default config; |
For the client preset, the dedupeFragments flag doesn't have an effect. I just upgraded to v1.1.1 of the client preset. My config: import type { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
overwrite: true,
schema: "http://localhost:3000/graphql",
documents: "app/**/*.tsx",
generates: {
"app/gql/": {
preset: "client",
plugins: [],
config: {
skipTypename: true,
dedupeFragments: true,
},
},
},
};
export default config; |
'./generated/gql/': {
// schema: './generated/schema.graphql',
schema: 'http://localhost:3000/api/graphql',
documents: ['lib/apollo/modules/**/*.{graphql,tsx,ts}', '!lib/apollo/modules/cms/**/*.{graphql,tsx,ts}'],
preset: 'client',
plugins: [],
presetConfig: {
fragmentMasking: false,
dedupeFragments: true
}
}, Adding it to |
@tojump, I've released your contribution; thanks! @AssisrMatheus @bernharduw, could you try with |
I can confirm it's working for me with v1.1.2! Good work @tojump + @charlypoly, thanks! 🎉 |
Should this be on by default? I would expect this to happen very often when combining fragments to form larger queries. |
@marco2216 I've open an issue to discuss enable options and default with |
@charlypoly i'm getting this issue with client 1.1.3 when i turn on 'typescript' plugin in the array.
|
Hi @ShravanSunder,
Also, |
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
Expected behavior
fragments must be deduplicated
a work-arround can be ssen here, which would also be an entry point for a bug fix bastiion/graphql-codegen-duplicate-fragments-issue@d08f51e#diff-ca580bb0b9ca66ec74fb97288ee12d571481819ed8e689ea99b2d6ff79dacc24
Screenshots or Videos
No response
Platform
Codegen Config File
Additional context
example query:
will produce:
The text was updated successfully, but these errors were encountered: