Describe the bug
Generating something based on a simple fragment file, for example:
fragment Foo on User {
id
}
... produces an invalid fragment file without a gql import:
import * as Types from './types';
export type FooFragment = (
{ __typename?: 'User' }
& Pick<Types.User, 'id'>
);
export const FooFragmentDoc = gql`
fragment Foo on User {
id
}
`;
This is not normally a problem, since everything is output into one final TS file. However, with the near-operations-file preset this becomes problematic as you tend to have a FooFragment.graphql file that gets converted to its own TypeScript file.
To Reproduce
Steps to reproduce the behavior:
Everything is in the sandbox at https://codesandbox.io/s/sleepy-wilbur-x35uh
My GraphQL schema:
https://codesandbox.io/s/sleepy-wilbur-x35uh?file=/schema.graphql
My GraphQL operations:
https://codesandbox.io/s/sleepy-wilbur-x35uh?file=/fragment.graphql
My codegen.yml config file:
https://codesandbox.io/s/sleepy-wilbur-x35uh?file=/codegen.yml
Expected behavior
The gql import is added for fragment-only files
Environment:
https://codesandbox.io/s/sleepy-wilbur-x35uh?file=/package.json
Additional context
Faced with the same problem, as a workaround I suggest moving your fragment to another .graphql file with query or mutation, it helped me.
Thank, I'm on it now :)
Fixed in: https://github.com/dotansimha/graphql-code-generator/pull/4554
@adam1658 can you please try the alpha versions from that PR?
@dotansimha tried it in the sandbox and it seems to work with @graphql-codegen/[email protected]:
https://codesandbox.io/s/sleepy-wilbur-x35uh?file=/fragment.gen.ts
@dotansimha I've had a bit of a look and tried @graphql-codegen/[email protected] - it doesn't appear to break anything for me, though I use documentMode: documentNode.
Fixed in @graphql-codegen/[email protected]
Most helpful comment
Fixed in
@graphql-codegen/[email protected]