dotansimha / dotansimha/graphql-code-generator-community

typed-document-node with documentMode external doesn't export a typed document node

Open
#165 2 comments 10 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

**Describe the bug**
We use `graphql-tag/loader` in webpack to load operations stored in `.graphql` files. We want to generate types to corresponding `.graphql.d.ts` files for the operations.

Using the `typescript-operations` plugin we get variable and result types correctly. We'd also like a `DocumentNode` type, basically `export type OperationName = TypedDocumentNode`

I've looked at using the `typed-document-node` and can get most of the way. With `documentMode: documentNode` I can generate the full parsed AST with the appropriate type. However, I'd rather leave this step to webpack.

I've tried `documentMode: external`, which seems like it would do the right thing. I'd expect if the document is handled externally, the `typed-document-node` plugin would just generate a corresponding document node type. Currently with `documentMode: external` it doesn't do anything, but doesn't validate against it like `documentMode: string`.

**To Reproduce**
Steps to reproduce the behavior:

https://codesandbox.io/s/frosty-ramanujan-3b74y?file=/document.graphql.d.ts

1. My GraphQL schema:

```graphql
type Query {
user(id: ID!): User!
}

type User {
id: ID!
username: String!
email: String!
}
```

2. My GraphQL operations:

```graphql
query user {
user(id: 1) {
id
username
email
}
}
```

3. My `codegen.yml` config file:

```yml
schema: schema.graphql
documents: document.graphql
generates:
types.ts:
plugins:
- typescript
'near-operation':
preset: 'near-operation-file'
presetConfig:
extension: .graphql.d.ts
baseTypesPath: types.ts
plugins:
- typescript-operations
- typed-document-node
config:
documentVariableSuffix: ''
documentMode: external
```

**Expected behavior**
Currently this generates:
```ts
import * as Types from './near-operation/types';

import * as Operations from '';
export type UserQueryVariables = Types.Exact<{ [key: string]: never; }>;

export type UserQuery = (
{ __typename?: 'Query' }
& { user: (
{ __typename?: 'User' }
& Pick
) }
);
```

I'd expect this to also have a typed document node
```ts
export type User = TypedDocumentNode;
```

**Environment:**

- OS: mac OS
```
"@graphql-codegen/cli": "1.20.1",
"@graphql-codegen/near-operation-file-preset": "2.2.2",
"@graphql-codegen/typed-document-node": "2.2.1",
"@graphql-codegen/typescript": "1.21.0",
"@graphql-codegen/typescript-operations": "1.17.14",
"graphql": "15.5.0"
```
- NodeJS: 16

**Additional context**

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.