Graphql-code-generator: Unable to load schema.ts if generated.d.ts types are used in resolvers

Created on 12 Nov 2019  路  3Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
When I run the graphql-codegen command for the second time, after referencing the types in my generated.d.ts file, it throws an error:
'Failed to load schema.ts' ...
'unable to compile Typescript' ...
'Cannot find name 'User' at line:
export const getUserById = (id: number): User | null => User.getUser(id);

To Reproduce

  1. successfully run the graphql-codegen to output a generated.d.ts file (set with noExport=true).
  2. add generated.d.ts to the typeRoots and includes arrays in tsconfig.json.
  3. use the generated type (ex: User) as a function return type, (correctly get autocomplete, etc).
  4. try to run graphql-codegen again, get the error.
  1. My GraphQL schema:
/* SCHEMA.TS */
import typeDefs from './typeDefs';
import resolvers from './resolvers';
import { makeExecutableSchema } from 'graphql-tools';

//used by Apollo Server for the schema.
//also used by codegen to generate the typescript types.
const schema = makeExecutableSchema({ typeDefs, resolvers });
export default schema;

/* TYPEDEFS/USER.TS (typeDef/index.js rolls up everything) */

const User = /* GraphQL */ `
  # main type definition
  type User {
    id: Int!
    email: String!
    name: String!
  }

  # query definitions
  extend type Query {
    getUserById(input: GetUserByIdInput): User
  }

  # inputs for queries and mutations
  input GetUserByIdInput {
    id: Int!
  }
`;
  1. My GraphQL operations:
n/a
  1. My codegen.yml config file:
overwrite: true
schema: "./src/schema.ts"
noRequire: true  # to help with the 'cannot use modules outside..' issue.
documents: null
generates:
  src/generated/generated.d.ts:
    hooks:
      afterOneFileWrite:
        - prettier --write
    plugins:
      - "typescript"
    config:
      noExport: true #to make the types available not as a module. 

require:
  - ts-node/register
  - tsconfig-paths/register

Expected behavior
running the graphql-codegen works to generated a new generated.d.ts file.

Environment:
typescript: 3.6.4
ts-node: 8.4.1

  • OS: OSX 10.15
  • @graphql-codegen/.cli v1.8.3:
  • NodeJS: 12.13.0

Additional context

  • I thought maybe the generated.d.ts was getting deleted in first step so tried to produce a generated2.d.ts file, no success.
  • I tried a combination of the noExport and global options, no success.

Appreciate any insight, thanks!

Most helpful comment

@ardatan thank you! The switch from ts-node/registry to ts-node/registry/transpile-only worked. For reference, my codegen.yml is now:
```overwrite: true
schema: "./src/schema.ts"
documents: null
generates:
src/generated/generated.d.ts:
hooks:
afterOneFileWrite:
- prettier --write
plugins:
- "typescript"
config:
noExport: true

require:

  • ts-node/register/transpile-only
  • tsconfig-paths/register```

All 3 comments

If you have one file that exports GraphQLSchemas, noRequire would not work for that.
If you need to import a TS file using ts-node or something else, you need to add require: ts-node/register/transpile-only in that case.
So could you try to replace ts-node/register with ts-node/register/transpile-only?

@ardatan thank you! The switch from ts-node/registry to ts-node/registry/transpile-only worked. For reference, my codegen.yml is now:
```overwrite: true
schema: "./src/schema.ts"
documents: null
generates:
src/generated/generated.d.ts:
hooks:
afterOneFileWrite:
- prettier --write
plugins:
- "typescript"
config:
noExport: true

require:

  • ts-node/register/transpile-only
  • tsconfig-paths/register```

If everything is good, we can close the issue :)

Was this page helpful?
0 / 5 - 0 ratings