dotansimha / dotansimha/graphql-code-generator

Receiving a basic error "Cannot read properties of undefined" when trying to run codegen from client repo.

Open
#9,772 3 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### Which packages are impacted by your issue?

@graphql-codegen/cli, @graphql-codegen/client-preset

### Describe the bug

Following the documentation provided in Apollo GraphQL docs: https://www.apollographql.com/docs/react/development-testing/static-typing#setting-up-your-project I installed dependencies with a basic codegen configuration. However when running `graphql-codegen` command through npm I get the following error, which is not very useful to know what is exactly happening here.

```
> graphql-codegen

✔ Parse Configuration
⚠ Generate outputs
❯ Generate to ./src/__generated__/

Failed to load schema from /graphql:
Cannot read properties of undefined (reading 'profile')
GraphQLError: Cannot read properties of undefined (reading 'profile')
at createGraphQLError (//node_modules/@graphql-tools/utils/cjs/errors.js:31:12)
at //node_modules/@graphql-tools/wrap/cjs/introspect.js:12:102
at Array.map ()
at getSchemaFromIntrospection (//node_modules/@graphql-tools/wrap/cjs/introspect.js:12:58)
at //node_modules/@graphql-tools/wrap/cjs/introspect.js:35:32
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async UrlLoader.load (//node_modules/@graphql-tools/url-loader/cjs/index.js:219:29)
at async //node_modules/@graphql-tools/load/cjs/load-typedefs/load-file.js:14:39
at async Promise.all (index 5)
at async loadFile (//node_modules/@graphql-tools/load/cjs/load-typedefs/load-file.js:12:9)
GraphQL Code Generator supports:
- ES Modules and CommonJS exports (export as default or named export "schema")
- Introspection JSON File
- URL of GraphQL endpoint
- Multiple files with type definitions (glob expression)
- String in config file
Try to use one of above options and run codegen again.
◼ Load GraphQL documents
◼ Generate
```
Before this I requested the backend team to enable introspection query for the GraphQL endpoint.

My current dependencies are the following:
```
"react": "^16.14.0",
"@apollo/client": "^3.7.16",
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/client-preset": "^4.1.0",
```
And my codegen setup is this:
```typescript
import { CodegenConfig } from '@graphql-codegen/cli'

const config: CodegenConfig = {
schema: '/graphql',
// this assumes that all your source files are in a top-level `src/` directory - you might need to adjust this to your file structure
documents: ['src/**/*.{ts,tsx}'],
generates: {
'./src/__generated__/': {
preset: 'client',
plugins: [],
presetConfig: {
gqlTagName: 'gql'
}
}
},
ignoreNoDocuments: true,
}
```
Any tips on how to debug this issue are welcome.

### Your Example Website or App

Can't disclose it.

### Steps to Reproduce the Bug or Issue

1. Ask backend team to enable introspection query for GraphQL endpoint.
2. Install dependencies.
3. Create codegen.ts file.
4. Run command graphql-codegen.

### Expected behavior

Expected result is having typings generated in src/__generated__/ folder.

### Screenshots or Videos

_No response_

### Platform

- OS: WSL on Windows
- NodeJS: 18.16.1
- `graphql` version: 16.6.0
- `@graphql-codegen/cli` version(s): 5.0.0
- `@graphql-codegen/client-preset` version(s): 4.1.0

### Codegen Config File

```typescript
const config: CodegenConfig = {
schema: '/graphql',
// this assumes that all your source files are in a top-level `src/` directory - you might need to adjust this to your file structure
documents: ['src/**/*.{ts,tsx}'],
generates: {
'./src/__generated__/': {
preset: 'client',
plugins: [],
presetConfig: {
gqlTagName: 'gql'
}
}
},
ignoreNoDocuments: true,
}
```

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.