Graphql-code-generator: Fragment Doc get lost after upgrading above the 1.9.1

Created on 9 Jan 2020  路  15Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
On 1.9.1 there is an import from Graphql file to use SomeFragmentDoc string.
After upgrading above 1.9.1 it doesn't work properly, I didn't tested alpha version, there are too much of them.
I don't know maybe it's intended behaviour and I need to use some kinda import feature, but I didn't need before.

To Reproduce
Steps to reproduce the behavior:
There is a repo
https://github.com/ivansky/graphql-codegen-test-repo/tree/babc5efd6e757bcfb1fe3dcfeac2851f00f6866b

And commit that describes the issue
https://github.com/ivansky/graphql-codegen-test-repo/commit/babc5efd6e757bcfb1fe3dcfeac2851f00f6866b

  1. My GraphQL schema:
schema {
    query: Query
}

type Query {
    check(
        product: ProductCheckInput!
    ): ProductCheckResult!
}

interface Service {
    id: ID!
    productId: String!
}

type AService implements Service {
    id: ID!
    productId: String!
    dooms: [[ADoom!]!]!
}

type ALoad {
    code: String!
}

type ADoom {
    loads: [ALoad!]!
    code: String!
    description: String
    searchCount: Int
}

type Product {
    rules: [String!]!
    id: ID!
    notes: [String!]!
    services: [Service!]!
}

type ProductCheckResult {
    product: Product
    success: Boolean!
}

input ProductCheckInput {
    searchId: String!
    searchProductId: String!
}

scalar Date

scalar DateTime

scalar Time

type Auth {
    accessToken: String!
}

type Client {
    auth: Auth
}

extend type Query {
    client: Client!
}

  1. My GraphQL operations:

AServiceFragment.graphql

fragment AServiceFragment on AService {
    id
    productId
}

CheckProduct.graphql

query CheckProduct($product: ProductCheckInput!) {
    check(product: $product) {
        success
        product {
            notes
            rules
            services {
                ...AServiceFragment
                ...on AService {
                    dooms {
                        code
                        loads {
                            code
                        }
                    }
                }
            }
        }
    }
}
  1. My codegen.yml config file:
overwrite: true
config:
  namingConvention:
    typeNames: change-case#pascalCase
    transformUnderscore: true
schema:
  - ./schema.api.graphql
  - ./schema.client.graphql
generates:
  types/types.ts:
    documents:
      - './graphql/operations/**/*.graphql'
    schema:
      - ./schema.api.graphql
      - ./schema.client.graphql
    plugins:
      - typescript
  types/:
    documents:
      - './graphql/operations/**/*.graphql'
    schema:
      - ./schema.api.graphql
      - ./schema.client.graphql
    config:
      noNamespaces: true
      withHooks: true
      withComponent: true
      withHOC: false
    preset: near-operation-file
    presetConfig:
      extension: .generated.tsx
      baseTypesPath: types.ts
    plugins:
      - typescript-operations
      - typescript-react-apollo
  types/resolversTypes.ts:
    config:
      contextType: ./graphql/contexts/ClientContext#ClientContext
    schema:
      - ./schema.client.graphql
    plugins:
      - typescript
      - typescript-resolvers
  graphql/introspectionResult.generated.ts:
    schema:
      - ./schema.api.graphql
    plugins:
      - fragment-matcher

Expected behavior
Everything that is used within the module should be imported automatically.

Environment:

  • OS: MacOS
  • @graphql-codegen/...: Any above 1.9.1
  • NodeJS: 13.5.0
bug core waiting-for-release

Most helpful comment

I can confirm version 1.11.2 have the same issue.

All 15 comments

I can confirm version 1.11.2 have the same issue.

@ardatan can you please take a look?

The problem still persists in 1.12.1.

I did a bit of digging, for me it looks like a bit of incomplete refactor https://github.com/dotansimha/graphql-code-generator/issues/3426

Bountysource

Could you try the following canary version?
1.12.2-alpha-ea7264f9.15
It works with this canary version on the given reproduction repo

this seems to work for me.
though this issue (https://github.com/dotansimha/graphql-code-generator/issues/3066) which seems related is not working yet.

@lkleuver Let's track your problem in that issue #3066 seperately. That PR only covers this issue for now.

@ardatan Yes, it works for me! Thank you very much!

Was a bit too early to celebrate :)

import { PublicPropertyFieldsDoc,PublicPropertyFieldsFragment } from '../../../../../../core/graphql/fragments/public-property-fields.generated';

this is the generated import.

These are exports

export type PublicPropertyFieldsFragment = (
  { readonly __typename?: 'property_api_PropertyEnrichedOutput' }

and

export const PublicPropertyFieldsFragmentDoc = gql`
    fragment PublicPropertyFields on property_api_PropertyEnrichedOutput {

and it gives me an error

Module '"../../../../../../core/graphql/fragments/public-property-fields.generated"' has no exported member 'PublicPropertyFieldsDoc'.

Looks like it should be

-import { PublicPropertyFieldsDoc,PublicPropertyFieldsFragment } 
+import { PublicPropertyFieldsFragmentDoc,PublicPropertyFieldsFragment } 

My bad, I gave you the older version. Please try that one
1.12.2-alpha-ea7264f9.15

Yep! This version works! Thank you!!

My bad, I gave you the older version. Please try that one
1.12.2-alpha-ea7264f9.15

@ardatan I can also confirm that 1.12.2-alpha-ea7264f9.15 fixed this issue! Thanks, look forward to the full release.

Fixed in 1.12.2! Thanks @ardatan

@ardatan Could you please go and claim the bounty? :smile_cat:

Was this page helpful?
0 / 5 - 0 ratings