eddeee888 / graphql-code-generator-plugins

List of GraphQL Code Generator plugins that complements the official plugins.
MIT License
52 stars 13 forks source link

Unclear way on how to use custom resolver structure #328

Closed jorgev259 closed 3 months ago

jorgev259 commented 3 months ago

I have been reading the docs, and i've been trying to find a way to keep my current resolver structure but still use the codegen for type generation.

imagen

eddeee888 commented 3 months ago

Hello,

You can use the externalResolvers to provide your own resolvers. For example:

externalResolvers: {
  'Query.me': '~@org/meResolver#default'
  Profile: '~modules/otherResolvers.js#Profile'
  User: 'otherResolvers#User as UserResolver'
  UserPayload: 'otherResolvers.js#UserPayload as UserPayloadResolver'
  ErrorType: 'base/CustomErrorType#ErrorType'
}

Note that this will give you full control i.e. you'd lose static analysis, type enforcement, etc.

jorgev259 commented 3 months ago

I see that makes sense. Another question: IS it normal for generated resolvers to be one file per resolver with the default config? Is there a config so that resolverse respect the structure the schemas are in

eddeee888 commented 3 months ago

IS it normal for generated resolvers to be one file per resolver with the default config

Yes, it's normal and it's done this way to make it easy to jump to a query/mutation/subscription and types on the filesystem.

Is there a config so that resolverse respect the structure the schemas are in

Could you please elaborate what this looks like?

jorgev259 commented 3 months ago

IS it normal for generated resolvers to be one file per resolver with the default config

Yes, it's normal and it's done this way to make it easy to jump to a query/mutation/subscription and types on the filesystem.

Is there a config so that resolverse respect the structure the schemas are in

Could you please elaborate what this looks like?

Thanks for the help! This is my current schema files structure: imagen

I would want it for the generated resolver files to have a similar structure or to maintain a similar ammount of files

eddeee888 commented 3 months ago

I see, could you please try the following config:

const config: CodegenConfig = {
  schema:
    '**/*.graphql',
  generates: {
    'path/to/your/graphql/resolvers': defineConfig({
      mode: 'merged', // intended for setups with schemas in one place
      resolverGeneration: 'minimal', // only generate the minimal: queries, mutations and subscriptions. You can add resolvers if needed
      mergeSchema: false, // this is useful if you don't need a merged schema in `resolvers` folder
    }),
  },
};

export default config;
jorgev259 commented 3 months ago
import { type CodegenConfig } from '@graphql-codegen/cli'
import { defineConfig } from '@eddeee888/gcg-typescript-resolver-files'

const config: CodegenConfig = {
  schema: 'src/graphql/typeDefs/**/*.graphql',
  documents: ['src/**/*.{astro,ts,tsx,mts}'],
  generates: {
    './src/graphql/__generated__/client/': {
      preset: 'client',
      plugins: [],
      presetConfig: { gqlTagName: 'gql' },
      config: { useTypeImports: true }
    },
    './src/graphql/resolvers/': defineConfig({
      mode: 'merged', // intended for setups with schemas in one place
      resolverGeneration: 'minimal', // only generate the minimal: queries, mutations and subscriptions. You can add resolvers if needed
      mergeSchema: false, // this is useful if you don't need a merged schema in `resolvers` folder
      typesPluginsConfig: {
        contextType: '../client.mts#ResolverContext',
        maybeValue: 'T'
      },
      add: {
        './types.generated.ts': { content: '// @ts-nocheck' },
      },
    })
  },
  ignoreNoDocuments: true
}

export default config

This is my full config with the following generated resolvers.

imagen

eddeee888 commented 3 months ago

Hi @jorgev259 , could you please let me know the difference between the generated resolvers compared to your current setup?

From a few screenshots, I can see you have queries and mutations folders instead of Query and Mutation. Are there other notable differences?

jorgev259 commented 3 months ago

Hi @jorgev259 , could you please let me know the difference between the generated resolvers compared to your current setup?

From a few screenshots, I can see you have queries and mutations folders instead of Query and Mutation. Are there other notable differences?

The biggest difference is that currently my resolvers arr split by concerns rather than by queries/mutatoond. So a resolver file container both queries and mutations for that specific aspect. Also resolvers aren't being divided into one file per resolver

eddeee888 commented 3 months ago

I see, I think your approach is a mix of both merged and modules mod. Maybe a bit closer to modules, but as you said, things are split into smaller files.

At this time, there's no plan to support custom structure yet, as it'd be hard (and probably slower in terms of performance) to code static analysis to work effectively on arbitrary structure 🙂

jorgev259 commented 3 months ago

I understand! Still found a comfortable setup by disabling resolver generation and importing the Resolver type into my manual made files. Works wonderfully for my project.

Closing now, thanks for the help :)