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

Codegen with printSchema fails for ForbiddenError

Open trixobird opened this issue 1 year ago • 6 comments

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/@[email protected][email protected]/node_modules/@pothos/core/lib/utils/index.js:88:15)
    at SchemaBuilder.objectType (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@[email protected][email protected]/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/[email protected]/node_modules/jiti/dist/jiti.js:1:255202)
    at jiti (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/jiti/dist/jiti.js:1:255202)
    at jiti (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/jiti/dist/jiti.js:1:255202)
    at jiti (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/jiti/dist/jiti.js:1:255202)
    at jiti (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]/node_modules/jiti/dist/jiti.js:1:253130)
    at loadTypeScript (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/graphql-config/esm/helpers/cosmiconfig.js:36:12)
    at Explorer.loadFileContent (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]/node_modules/cosmiconfig/dist/Explorer.js:90:20)
    at Explorer.createCosmiconfigResult (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]/node_modules/cosmiconfig/dist/Explorer.js:98:36)
    at runLoad (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]/node_modules/cosmiconfig/dist/Explorer.js:115:33)
    at async cacheWrapper (/Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]/node_modules/cosmiconfig/dist/cacheWrapper.js:16:18)
    at async getConfig (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/graphql-config/esm/helpers/get-config.js:6:17)
    at async loadConfig (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/graphql-config/esm/config.js:25:15)
    at async findAndLoadGraphQLConfig (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@[email protected]_@[email protected][email protected]/node_modules/@graphql-codegen/cli/esm/graphql-config.js:31:20)
    at async loadContext (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@[email protected]_@[email protected][email protected]/node_modules/@graphql-codegen/cli/esm/config.js:73:27)
    at async createContext (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@[email protected]_@[email protected][email protected]/node_modules/@graphql-codegen/cli/esm/config.js:181:21)
    at async runCli (file:///Users/trixobird/w/magpie/magpie/node_modules/.pnpm/@[email protected]_@[email protected][email protected]/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

  • OS: macOS
  • NodeJS: 18.17.1
  • graphql version: 16.8.1
  • "@graphql-codegen/cli": "^5.0.0",
    
    "@graphql-codegen/client-preset": "^4.1.0", "@graphql-codegen/typescript-generic-sdk": "^3.1.0", "@graphql-codegen/typescript-operations": "^4.0.1", "@graphql-codegen/typescript-react-query": "^4.1.0",

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

trixobird avatar Sep 22 '23 05:09 trixobird