dotansimha / dotansimha/graphql-code-generator-community

Adding support to include non-base types in the generated file only with near-operation-file preset

Open
#437 1 comment 3 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

### Is your feature request related to a problem? Please describe.

Given the current schema size I am assuming, I don't want to use a single generated file as it's impossible. to identify which unit tests to run using `jest --findRelatedTests=`.
Ideal way of solving this problem would be to have separate types file generated for individual `*.query.tsx` files. `near-operation-files` comes to rescue by generating separate types file corresponding to each `*.query.tsx` file but this doesn't help us either because of the required configuration called `baseTypesPath` which needs to be the typescript type of all the properties defined in schema-json and those types are being referred in individual generated files as `Types.XYZ`.
Since we have a common import again, it defeats the purpose of using this preset.
```js
{
schema: schemaFile,
documents: [
'src/**/*.query.tsx',
'src/**/*.queries.tsx',
'src/**/*.mutation.tsx',
'src/**/*.fragment.tsx',
'src/**/*.fragments.tsx',
'src/**/*.columns.tsx',
'src/**/*.graphql',
'!src/**/__tests__/*',
'!src/generated/**/*',
],
ignoreNoDocuments: true,
generates: {
'src/generated/types.ts': {
plugins: ['typescript']
},
'src/': {
preset: 'near-operation-file',
presetConfig: {
extension: '.generated.tsx',
baseTypesPath: 'generated/types.ts',
},
```

### Describe the solution you'd like

If we have configuration option to enable types getting included as part of the generated file itself, it will serve the purpose. Let's say we call the option `enableInlineTypes`(I am bad with names).

#### Before
Screenshot 2023-09-19 at 1 09 53 PM

#### After
Screenshot 2023-09-19 at 1 14 14 PM

### Describe alternatives you've considered

For now, hacky way of fixing this as per our need would be to make this a two-step process where the second step would be to add a AST parser which can identify types which needs to be extracted out from `src/generated/types.ts` and moved to corresponding `*.query.tsx` file.

### Is your feature request related to a problem? Please describe.

_No response_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by tracing the near-operation-file preset, especially its baseTypesPath handling, alongside the typescript plugin configuration shown in the issue. Compare the current generated per-operation files with the requested inline output; done means the preset can include required non-base types locally without the shared generated/types.ts import, with coverage for the new configuration option.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.