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
/* 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!
}
`;
n/a
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
@graphql-codegen/.cli v1.8.3: Additional context
Appreciate any insight, thanks!
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:
If everything is good, we can close the issue :)
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: