Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use client preset with imported types #9894

Open
JElgar opened this issue Mar 7, 2024 · 1 comment
Open

Use client preset with imported types #9894

JElgar opened this issue Mar 7, 2024 · 1 comment

Comments

@JElgar
Copy link

JElgar commented Mar 7, 2024

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

Currently the typescript-operations plugin allows me to use the import-types preset and with preResolvesTypes to generate operation types which pick from existing types (generated elsewhere). I would like to have this same behavior when using the client preset.

Describe the solution you'd like

I would like to be able to specify preResolveTypes: false and provide a typesPath (same as currently available for import-types preset) when using the client preset. When set the client preset should not generate new top level operation types but should instead pick from the imported types from the typesPath (again same as is currently setup with import-types)

Describe alternatives you've considered

I have tried to use a combination of existing plugins to get the same behavior as client but with imported types. This has almost worked but the missing piece is generating the gql function which the client function is doing with the currently undocumented gql-tag-operations (https://github.com/dotansimha/graphql-code-generator/blob/master/packages/plugins/typescript/gql-tag-operations/src/index.ts).

My current config has the following components which successfully generates the types (with a custom plugin on the original schema types).

    './src/__gql__/schemaTypes.ts': {
        plugins: {
            './graphql/plugins/myPluginThing.js': {},
            'typescript': {
                nonOptionalTypename: true,
            },
            'typescript-operations': {
                skipTypename: true,
                dedupeFragments: true,
                preResolveTypes: false,
            },
        },
    },
    './src/__gql__/operationTypes.ts': {
        preset: 'import-types',
        presetConfig: {
            typesPath: './schemaTypes',
        },
        plugins: {
            'typescript-operations': {
                skipTypename: true,
                dedupeFragments: true,
                preResolveTypes: false,
            },
        },
    },
    './src/__gql__/apolloClient.ts': {
        preset: 'import-types',
        presetConfig: {
            typesPath: './operationTypes',
        },
        plugins: {
            'typescript-react-apollo': {
                withHooks: false,
            }
        },
     },
    './src/__gql__/typedDocNode.ts': {
        plugins: {
            'typed-document-node': {}
        },
     },

This all works great but we currently rely on the types generated by gql-tag-operations in the client preset to types the gql queries we write e.g. gql(query { stuff { id } }) has a return type of a TypedDocumentNode (rather than just the document node when using apollos gql function).

I considered using this gql-tag-operations plugin but was unsure how to generate the SourceWithOperations object which is handled here in the client preset: https://github.com/dotansimha/graphql-code-generator/blob/master/packages/presets/client/src/process-sources.ts.

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

I think the most ideal solution to me is client is extended to support importing types. Alternative better documentation on the gql-tag-operations might make sense as it might be a simple to plugin that in? Equally I could just be missing a simple fix for what I'm trying to do so any help if much appreciated.

@JElgar
Copy link
Author

JElgar commented Mar 7, 2024

Also in the documentation it currently states you can provide a list of presets

generates.preset - A list of presets to use for the output. Presets are a way to dynamically create the list of output files based on the input schema. near-operation-file-preset is a good example

Is that correct? I haven't seen any examples of this and couldn't understand how this would work but if that's the case it might be ideal?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant