graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Custom TS schema loaders are not working

Open taletski opened this issue 2 years ago • 3 comments
trafficstars

Which packages are impacted by your issue?

@graphql-codegen/cli

Describe the bug

I am following the guide for creating a custom loader using TS. It appears that the codegen fails to load the .ts loader file yet works ok with a .js one.

Your Example Website or App

https://codesandbox.io/p/sandbox/graphql-codegen-custom-schema-loader-issue-hkve7f

Steps to Reproduce the Bug or Issue

  • Run yarn generate The command fails with an error

  • Change loader.ts to loader.js in codegen.ts

  • Run yarn generate This time the command will work

Expected behavior

As a user, I expected that the custom schema loader written in TS will work if I follow the guide

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Linux]
  • NodeJS: 16.17.0
  • graphql version: 16.2.0
  • @graphql-codegen/* version(s): 2.6.2

Codegen Config File

import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
  schema: {
    "schema.graphql": {
      // loader: "./loader.js", // JS - ✅ working
      loader: "./loader.ts", // TS - ❌ not working
    },
  },
  documents: "document.graphql",
  generates: {
    "types.ts": { plugins: ["typescript", "typescript-operations"] },
  },
};

export default config;

Additional context

Please, note that I am registering the ts-node for the codegen command.

graphql-codegen -r ts-node/register --config ./codegen.ts

taletski avatar Feb 13 '23 12:02 taletski

Just a note on reproduction details. This is happening to us explicitly on 3.2.2 We have this condegen config:

const config = {
  schema: {
    [schemaUrl]: {
      headers: {
        Authorization: AUTHORIZATION
      }
    }
  },
  documents: {
    'path/to/my/query': {
      templateValues: 'my/template/values.json',
      loader: './documentLoader.ts'
    }
  },
  config: {
    skipTypeName: true
  },
  generates: {
    ....
  }
}

Which runs just fine on codegen 3.2.1 and lower, but we started a new project where we installed 3.2.2 and it fails not able to find the loader: Screenshot 2023-03-08 at 11 18 20 AM On the initial run of generate:local package.json was set to install codegen ^3.2.1 which would make npm fetch the 3.2.2 version. When forcing npm to install 3.2.1 the command ran without issues (yes, we only had a single ^3.2.1 package, so sed worked just fine)

Axel-P avatar Mar 08 '23 16:03 Axel-P

Yes I'm seeing the same 3.2.1 works fine but upgrading to 3.2.2 gives an error of Failed to load custom loader

dottodot avatar Mar 08 '23 19:03 dottodot

Was able to fix this by doing the following in codegen.ts in version 5.0.6

graphql-codegen-esm --config ./codegen.ts
import loader from './loader'
import { type CodegenConfig } from '@graphql-codegen/cli'

const config: CodegenConfig = {
  ...
  // @ts-expect-error loader can be a function, even though the type doesn't allow it
  documents: {
    'path/to/my/query': {
      templateValues: 'my/template/values.json',
      loader
    },
   ...
}

brandhaug avatar May 09 '25 08:05 brandhaug