dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.73k stars 1.31k forks source link

Codegen with printSchema fails for ForbiddenError #9679

Open trixobird opened 10 months ago

trixobird commented 10 months ago

Which packages are impacted by your issue?

@graphql-codegen/cli, @graphql-codegen/core

Describe the bug

I was trying the error plugin along with the ScopeAuthPlugin and everything is working on the server side. I tried to execute the codegen and I am gettting

PothosSchemaError: Received undefined as a type ref.

This is often caused by a circular import If this ref is imported from a file that re-exports it (like index.ts) you may be able to resolve this by importing it directly from the file that defines it. If I replace schema: printSchema(schema), in codegen with `schema: 'http://localhost:4000/graphql', the codegen works.

I have a sample repo that the issue is reproducable: https://github.com/trixobird/typescript_playground/tree/pothos-codegen-auth-bug. Just execute the codegen script pnpm codegen

Initially I thought that this is a pothos error, so I opened an issue there. Hayes came back saying that "This is a bug in graphql-code-generator (or more accurately, whatever they use to load typescript). After some debugging, I found that their loader doesn't expose any named exports if you also have a default export."

Your Example Website or App

https://github.com/trixobird/typescript_playground/tree/pothos-codegen-auth-bug

Steps to Reproduce the Bug or Issue

  1. pnpm i
  2. pnpm codegen

Expected behavior

I expected to have the codegen passed successfully, instead I am seeing the following error:

> graphql-codegen-esm --config src/schema/codegen.ts

[shopify-api/INFO] version 7.6.0, environment Node v18.17.1
PothosSchemaError: Received undefined as a type ref.

This is often caused by a circular import
If this ref is imported from a file that re-exports it (like index.ts)
you may be able to resolve this by importing it directly from the file that defines it.

    at verifyRef (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@pothos+core@3.37.0_graphql@16.8.1/node_modules/@pothos/core/lib/utils/index.js:88:15)
    at SchemaBuilder.objectType (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@pothos+core@3.37.0_graphql@16.8.1/node_modules/@pothos/core/lib/builder.js:94:30)
    at /Users/trixobird/w/magpie/magpie/apps/server/src/schema/errors.ts:27:18
    at evalModule (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/jiti@1.19.3/node_modules/jiti/dist/jiti.js:1:255202)
    at jiti (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/jiti@1.19.3/node_modules/jiti/dist/jiti.js:1:253130)
    at /Users/trixobird/w/magpie/magpie/apps/server/src/schema/integrations/list.ts:6:15
    at evalModule (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/jiti@1.19.3/node_modules/jiti/dist/jiti.js:1:255202)
    at jiti (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/jiti@1.19.3/node_modules/jiti/dist/jiti.js:1:253130)
    at /Users/trixobird/w/magpie/magpie/apps/server/src/schema/index.ts:8:1
    at evalModule (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/jiti@1.19.3/node_modules/jiti/dist/jiti.js:1:255202)
    at jiti (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/jiti@1.19.3/node_modules/jiti/dist/jiti.js:1:253130)
    at /Users/trixobird/w/magpie/magpie/apps/server/src/schema/codegen.ts:3:37
    at evalModule (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/jiti@1.19.3/node_modules/jiti/dist/jiti.js:1:255202)
    at jiti (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/jiti@1.19.3/node_modules/jiti/dist/jiti.js:1:253130)
    at loadTypeScript (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/graphql-config@5.0.2_@types+node@20.5.0_graphql@16.8.1/node_modules/graphql-config/esm/helpers/cosmiconfig.js:36:12)
    at Explorer.loadFileContent (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/cosmiconfig@8.2.0/node_modules/cosmiconfig/dist/Explorer.js:90:20)
    at Explorer.createCosmiconfigResult (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/cosmiconfig@8.2.0/node_modules/cosmiconfig/dist/Explorer.js:98:36)
    at runLoad (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/cosmiconfig@8.2.0/node_modules/cosmiconfig/dist/Explorer.js:115:33)
    at async cacheWrapper (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/cosmiconfig@8.2.0/node_modules/cosmiconfig/dist/cacheWrapper.js:16:18)
    at async getConfig (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/graphql-config@5.0.2_@types+node@20.5.0_graphql@16.8.1/node_modules/graphql-config/esm/helpers/get-config.js:6:17)
    at async loadConfig (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/graphql-config@5.0.2_@types+node@20.5.0_graphql@16.8.1/node_modules/graphql-config/esm/config.js:25:15)
    at async findAndLoadGraphQLConfig (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@graphql-codegen+cli@5.0.0_@types+node@20.5.0_graphql@16.8.1/node_modules/@graphql-codegen/cli/esm/graphql-config.js:31:20)
    at async loadContext (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@graphql-codegen+cli@5.0.0_@types+node@20.5.0_graphql@16.8.1/node_modules/@graphql-codegen/cli/esm/config.js:73:27)
    at async createContext (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@graphql-codegen+cli@5.0.0_@types+node@20.5.0_graphql@16.8.1/node_modules/@graphql-codegen/cli/esm/config.js:181:21)
    at async runCli (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@graphql-codegen+cli@5.0.0_@types+node@20.5.0_graphql@16.8.1/node_modules/@graphql-codegen/cli/esm/cli.js:11:21) {
  filepath: '/Users/trixobird/w/magpie/magpie/apps/server/src/schema/codegen.ts'
}
 ELIFECYCLE  Command failed with exit code 1.

Screenshots or Videos

No response

Platform

Codegen Config File

import type { CodegenConfig } from '@graphql-codegen/cli'; import { printSchema } from 'graphql'; import schema from './index.js';

const config: CodegenConfig = { schema: printSchema(schema), emitLegacyCommonJSImports: false, generates: { './src/schema/generated/schema.graphql': { plugins: ['schema-ast'], }, }, config: { scalars: { Date: 'Date', UUID: 'string', }, }, }; export default config;

Additional context

No response

ardatan commented 10 months ago

You don't need to print the schema or pass the schema itself directly. You can just give the path to the schema. I guess "./schema" in your case

trixobird commented 10 months ago

Tried schema: './schema/index.js', and schema: './schema', in both cases the error is:

 Generate outputs
  ❯ Generate to ./src/schema/generated/schema.graphql
    ✖
      Failed to load schema from ./schema:
      Unable to find any GraphQL type definitions for the following pointers:
      - ./schema
      Error:
      Unable to find any GraphQL type definitions for the following pointers:
      - ./schema

Which makes sense to me; I am using a code first approach, shouldn't the schema be first compiled to a *.graphql file? That is whay the printSchema function of pothos is trying to do

hayes commented 10 months ago

Not sure exactly where the bug is, but it has something to do with how modules are loaded by graphql-codegen.

I initially assumed it was an upstream issue in jiti, but it looks like it might be more complicated: codegen.ts

import * as PothosExports from '@pothos/plugin-errors';
console.log(PothosExports)

export default {}
> pnpm graphql-codegen-esm --config src/schema/codegen.ts
{ default: 'errors' }
> pnpm jiti src/schema/codegen.ts
{
  default: [Getter],
  capitalize: [Getter],
  defaultGetResultName: [Getter],
  defaultGetUnionName: [Getter],
  unwrapError: [Getter],
  PothosErrorsPlugin: [Getter]
}
hayes commented 10 months ago

tracked down the issue, https://github.com/unjs/mlly/blob/main/src/cjs.ts#L38 sourceModule.defaultis a string, and then it tries to use that default export as the new export object to assign properties to.

hayes commented 10 months ago

opened an issue here: https://github.com/unjs/mlly/issues/194

tylerjbainbridge commented 1 month ago

I'm running into this issue after switching to the turbo option of Next.js, any insight into what might be going on?