Graphql-code-generator: Single fragment files are generated without a `gql` import (problematic when combined with `near-operations-file`)

Created on 6 Aug 2020  路  6Comments  路  Source: dotansimha/graphql-code-generator

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

  1. My GraphQL schema:

    https://codesandbox.io/s/sleepy-wilbur-x35uh?file=/schema.graphql

  2. My GraphQL operations:

    https://codesandbox.io/s/sleepy-wilbur-x35uh?file=/fragment.graphql

  3. 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

bug plugins waiting-for-release

Most helpful comment

Fixed in @graphql-codegen/[email protected]

All 6 comments

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]

Was this page helpful?
0 / 5 - 0 ratings