Graphql-code-generator: schema directive no longer works when two difference types linked with eachother

Created on 22 Jun 2020  ·  7Comments  ·  Source: dotansimha/graphql-code-generator

Describe the bug
schema directive doesn't work when one type depends on other type and both types are indifference files

To Reproduce
Here is a live example: https://codesandbox.io/s/blissful-lewin-lgf3f?file=/codegen.yml

  1. My GraphQL schema:

user.graphql

type User {
  name: String!
  posts: [Post!]!
}

type Query {
  users: [User!]!
}

post.graphql

type Post {
  author: User!
  title: String!
}

type Query {
  posts: [Post!]!
}

root.graphql

# import * from './post/post.graphql'
# import * from './user/user.graphql'
  1. My codegen.yml config file:
schema: ./root.graphql

generates:
  ./generated.ts:
    plugins:
      - typescript

Expected behaviour
should generate generated.ts file

Environment:

  • OS: Ubuntu
  • @graphql-codegen/...: 1.15.4
  • NodeJS: 12.16.3

Here is the error message

yarn run v1.22.4
$ graphql-codegen
  ✔ Parse configuration
  ❯ Generate outputs
    ❯ Generate ./generated.ts
      ✖ Load GraphQL schemas
        → Failed to load schema
        Load GraphQL documents
        Generate


 Found 1 error

  ✖ ./generated.ts
    Failed to load schema from ./root.graphql:

        Couldn't find type User in any of the schemas.
        Error: Couldn't find type User in any of the schemas.
    at visitFile (/home/suhailbinnisar/Desktop/dummy/node_modules/@graphql-tools/import/index.cjs.js:242:31)
    at visitFile (/home/suhailbinnisar/Desktop/dummy/node_modules/@graphql-tools/import/index.cjs.js:175:45)
    at Object.processImport (/home/suhailbinnisar/Desktop/dummy/node_modules/@graphql-tools/import/index.cjs.js:30:17)
    at GraphQLFileLoader.handleFileContent (/home/suhailbinnisar/Desktop/dummy/node_modules/@graphql-tools/graphql-file-loader/index
.cjs.js:53:35)
    at GraphQLFileLoader.load (/home/suhailbinnisar/Desktop/dummy/node_modules/@graphql-tools/graphql-file-loader/index.cjs.js:41:21
)
    at async loadFile (/home/suhailbinnisar/Desktop/dummy/node_modules/@graphql-tools/load/index.cjs.js:48:24)
    at async /home/suhailbinnisar/Desktop/dummy/node_modules/@graphql-tools/load/index.cjs.js:425:24

        GraphQL Code Generator supports:
          - ES Modules and CommonJS exports (export as default or named export "schema")
          - Introspection JSON File
          - URL of GraphQL endpoint
          - Multiple files with type definitions (glob expression)
          - String in config file

        Try to use one of above options and run codegen again.
    Error: Failed to load schema
        at loadSchema (/home/suhailbinnisar/Desktop/dummy/node_modules/@graphql-codegen/cli/bin.js:407:15)
    Error: Failed to load schema
        at loadSchema (/home/suhailbinnisar/Desktop/dummy/node_modules/@graphql-codegen/cli/bin.js:407:15)
dependencies

Most helpful comment

yeah but in larger projects, we have to import every type we need in every files which was not the case before, I think it's an issue with graphql-import previously it used, to sum up, all files then parse but now its parse each file before summing up so we need to import explicitly in every file but previously we just need to import all files in root graphql file (root.graphql) then it worked, correct me if I am wrong

Edit: Also now the order matters previously it wasn't the case

All 7 comments

https://codesandbox.io/s/nice-lewin-f9ffh?file=/schema.graphql
You have missing imports in post.graphql and user.graphql files. In order to see the correct usage, you can take a look at the fork above.

yeah but in larger projects, we have to import every type we need in every files which was not the case before, I think it's an issue with graphql-import previously it used, to sum up, all files then parse but now its parse each file before summing up so we need to import explicitly in every file but previously we just need to import all files in root graphql file (root.graphql) then it worked, correct me if I am wrong

Edit: Also now the order matters previously it wasn't the case

This is not an issue related to codegen. You can open an issue on import's repo to discuss the flow.
Also I don't recommend to use #import syntax at all. You can modularize your schema in different ways.
https://www.graphql-tools.com/docs/merge-typedefs

@suhailibnnisar I can confirm that we don't have to import explicitly previously.

How are you dealing with it now especially when you have to work with generated .graphql files

@suhailibnnisar I'm still figuring out how to solve that.

@LucienLee Please let us know when you figure out

Was this page helpful?
0 / 5 - 0 ratings