How can I use a typescript loader for graphql-codegen
? I need to build my graphql schema dynamically, therefore I have to load it from a typescript file.
Here are the files I'm using:
codegen.yaml
generates:
src/generated/auth-resolvers.types.ts:
schema:
- authTypeDefs:
loader: ./src/graphql/auth.typedefs.ts
config:
useIndexSignature: true
plugins:
- typescript
- typescript-resolvers
require:
- ts-node/register
- tsconfig-paths/register
auth.typedefs.ts
import { Permission } from "@src/generated/model.types";
import { gql } from "apollo-server";
import { makeExecutableSchema } from "graphql-tools";
let permFields = "";
for (const perm in Permission) {
permFields += ` ${perm}: PermissionDomains\n`;
}
export default gql`
type JsonType {
json: Json
}
type BooleanType {
boolean: Boolean
}
union PermissionOperationDomain = JsonType | BooleanType
type SecurityDomain {
read: PermissionOperationDomain,
write: PermissionOperationDomain,
update: PermissionOperationDomain,
delete: PermissionOperationDomain
}
type SecurityDomains {
domains: [SecurityDomain!]
}
union PermissionDomains = SecurityDomains | BooleanType
extend type Query {
getPermissions(userId: ID!): SecurityContext
}
type SecurityContext {
${permFields}}
`;
To run the code generation, I have this npm script:
> graphql-codegen -r ts-node/register
But I keep on getting the error:
✖ src/generated/auth-resolvers.types.ts
Failed to load schema from authTypeDefs,loader:
Cannot read properties of null (reading 'loader')
TypeError: Cannot read properties of null (reading 'loader')