-
Notifications
You must be signed in to change notification settings - Fork 152
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
Cannot read/write nested fragments #140
Comments
@johannesbraeunig Can you please provide a reproduction or even better pr with a failing test for this? |
@n1ru4l I was able to narrow it down to the option Here is an example: With
|
I believe this changed in v2, as we got bit by this during the upgrade. Previously reading nested fragments worked fine, even with |
I'm hitting this as well. Is there a fix in sight? |
Experiencing this as well |
With Here is a really annoying workaround: cache.writeFragment({
fragment: {
...FooBarFragmentDoc,
definitions: [
...FooBarFragmentDoc.definitions,
...NestedFragmentDoc.definitions,
...AnotherNestedFragmentDoc.definitions,
// ... any other fragment that's needed, add more of them as you get errors
],
},
fragmentName: 'FooBar',
data: {
// ...
}
}); fragment FooBar on FooBar {
# some fields
nested {
...Nested
}
}
fragment Nested on SomeType {
# some fields
something {
...AnotherNested
}
}
fragment AnotherNested on Something {
# some fields
} |
Adding search terms for people bumping into this: With With There is no fix for either at the moment on the latest versions, only the workaround mentioned by @yusufkandemir (which I can't apply in my codebase as it is too cumbersome). |
Someone from my team (@Titozzz) found a workaround: In the config:
With a node_modules patcher (yarn 3 patch or patch-package for @graphql-codegen/visitor-plugin-common, - return `{"kind":"${graphql_1.Kind.DOCUMENT}","definitions":[${definitions}]}`;
+ return `{"kind":"${graphql_1.Kind.DOCUMENT}","definitions":[${definitions}].filter(x => x.kind !== "FragmentDefinition" || !usedFragments.includes(x.name.value) && usedFragments.push(x.name.value))}`; It might not be safe so use with caution :) Until a real fix can be worked on! |
I spent a while wondering why these issues occur. I have the same issues as you @yusufkandemir, but I also stand with @adrienharnay that this is too cumbersome. For now, I will try to avoid reading fragments, which is a shame. |
I have the same problem. In my case, it's a fragment nested in another type. When the fragment is used somewhere, it generates a 2-level deeper structure to the type with So, instead of: which is wrong, because the actual data (in JavaScript) is at So far, I haven't found a workaround. I tried changing the settings for import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
overwrite: true,
schema: './schema.graphql',
documents: 'lib/graphql/*.ts',
generates: {
'src/__generated__/': {
preset: 'client',
plugins: [],
presetConfig: {
gqlTagName: 'gql',
},
},
},
ignoreNoDocuments: true,
};
export default config; |
Have just run into this as well. Are there any updates on a forthcoming fix or better workarounds? |
I believe the issue with However, it is mentioned in the main repo, that Does that mean, that in the future everyone will run into this problem, or is there now another way around |
@erikwrede See dotansimha/graphql-code-generator#8971 The |
@n1ru4l thanks for the quick reply! Please excuse the confusion and using this thread to ask, but which version of codegen do I need to use to try this out? I'm on |
@erikwrede |
Can confirm that this is fixed now, thanks for the help! |
Cool, I am going to close this issue then. 🥳 |
Hello @n1ru4l, thank you for the fix that seems to work on my side too (I need to test more). However, it seems fragments are still imported even if they are not used, which results in TS errors. Is this an oversight in the refactoring or something I'm doing wrong? |
Interesting, I don't observe this behavior. Have you got an example @adrienharnay? |
Yes @erikwrede , here you go: https://codesandbox.io/s/gql-code-generator-without-dedup-fragments-forked-35lp34?file=/src/user.fragment.generated.tsx Run |
@adrienharnay Please open a new issue for this, can you help fixing it? |
I don't see how this is fixed. I am still getting the |
Hi @n1ru4l , I opened a PR: dotansimha/graphql-code-generator#9136 |
Running into the same issue, seems to be purely typescript related e.g.
Note: the item This is using A workaround is using an explicit cast e.g. |
I was able to work through this a different way to @dwknippers by using the codegen generated Where I was casting as they recommended above with...
I'm now doing
Your linter might then complain about what appears to be a After making the recommended change to the @thexpand tagging you because my problem was exactly the same as yours. |
Versions
Versions
Describe the bug
In our project, we create a new GraphQL file for each query, mutation, and fragment. Out of that, a types file is generated, including the GraphQL document.
We're making use of the Apollo write/read fragment feature, in order to store some state in the Apollo cache or simply overwrite some data. For this, we use the document of the fragment from the generated types file.
With the latest update, we get an error, that no fragment was found for a specific name. We looked a bit deeper into that and found out, for fragments that import another fragment, the fragment is not included in the respective document.
This causes the following error:
Invariant Violation: No fragment named Zip.
Looking into the changes of our current upgrade PR, we can see that for all fragments that all
fragment.definitions
assignments to the document got removed, while they been added to the respective query and mutations.Example:
export const AddressFragmentDoc = { kind: 'Document', defintions: [ { // the current fragment document defintion } - ...ZipFragmentDoc.definitions ] }
It seems, by removing the definitions of the imported fragment we lose the information about the fragment.
Questions
The text was updated successfully, but these errors were encountered: