Closed asharnadeem closed 1 month ago
Hi @asharnadeem ,
To achieve what you need, mappers can be used.
Here's what you can do if you use @graphql-codegen/typescript-resolvers
Book
type in the schema e.g.
// src/mappers.ts
export type BookMapper = { id: string; name: string; }
2. Then, wire it up in codegen config:
```ts
// codegen.ts
import type { CodegenConfig } from '@graphql-codegen/cli';
const config: CodegenConfig = {
schema: '**/schema.graphql'
generates: {
'src/types.generated.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
mappers: {
Book: './mappers#BookMapper'
}
}
}
}
}
export default config;
BookMapper
in Query.book
Using base plugin can get repetitive and it also doesn't provide runtime safety. We recommend using Server Preset to get a more managed experience so you can focus more on developing features instead of worrying about types.
To set up server preset, here's what you do:
// codegen.ts
import type { CodegenConfig } from '@graphql-codegen/cli';
import { defineConfig } from '@eddeee888/gcg-typescript-resolver-files';
const config: CodegenConfig = { schema: '**/schema.graphql' generates: { 'src/schema': defineConfig(), }
export default config;
2. Make sure you schema is in the right module e.g.
```graphql
# src/schema/schema.graphql
type Query {
book(id: ID!): Book!
}
# ... other types
// src/schema/schema.mappers.ts
export type BookMapper = {
id: string;
name: string;
}
BookMapper
resolver.Here's the full guide to integrate Server Preset into GraphQL Yoga or Apollo Server
Hi @asharnadeem , I'll close this issue as it's an expected behaviour. If you have issues using mappers
or Server Preset, please create other issues and ping me.
Is your feature request related to a problem? Please describe.
The
Resolvers
type generated bytypescript-resolvers
does not allow for resolving relationships on the types nested resolver, but forces you to fill them in on the base query, even if it was not requested. For example, if a book must have an author, codegen types will make you return the author even if it was not requestedIn database terms, its okay to call and return all the fields from a base table and have graphql filter out what it doesn't need and send it to the client, but having to join every possible relationship an entity could have and return them all is highly inperformant, and kills the point of GraphQL.
Describe the solution you'd like
Let's assume the following schema, where a book must have an author.
I should be able to do the below, where I get just the base book on the
book
query, and can fill in theauthor
on the subresolver if it was requested by the client.Describe alternatives you've considered
No response
Is your feature request related to a problem? Please describe.
No response