Skip to content

Commit d49124e

Browse files
authored
[Docs] Update server preset guide to reflect changes in v0.8 (#9954)
* Fix blog post name * Update doc to reflect server preset v0.8
1 parent 3ec9bda commit d49124e

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

website/src/pages/docs/guides/graphql-server-apollo-yoga-with-server-preset.mdx

+46-12
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ npm i -D @graphql-codegen/cli @eddeee888/gcg-typescript-resolver-files
7272

7373
Create or update your Codegen config as follows:
7474

75-
<Tabs items={['codegen.ts','codegen.yml']}>
75+
<Tabs items={['codegen.ts','codegen.ts (GraphQL Yoga with File Uploads)','codegen.yml']}>
7676

7777
<Tabs.Tab>
7878

@@ -93,6 +93,38 @@ export default config
9393

9494
<Tabs.Tab>
9595

96+
```ts filename="codegen.ts"
97+
import type { CodegenConfig } from '@graphql-codegen/cli'
98+
import { defineConfig } from '@eddeee888/gcg-typescript-resolver-files'
99+
100+
const config: CodegenConfig = {
101+
schema: '**/schema.graphql',
102+
generates: {
103+
'src/schema': defineConfig({
104+
// The following config is designed to work with GraphQL Yoga's File uploads feature
105+
// https://the-guild.dev/graphql/yoga-server/docs/features/file-uploads
106+
scalarsOverrides: {
107+
File: { type: 'File' }
108+
},
109+
resolverGeneration: {
110+
query: '*',
111+
mutation: '*',
112+
subscription: '*',
113+
scalar: '!*.File',
114+
object: '*',
115+
union: '',
116+
interface: ''
117+
}
118+
})
119+
}
120+
}
121+
export default config
122+
```
123+
124+
</Tabs.Tab>
125+
126+
<Tabs.Tab>
127+
96128
```yaml filename="codegen.yml"
97129
schema: '**/schema.graphql'
98130
generates:
@@ -257,7 +289,7 @@ The server preset handles all resolver types and imports. So, you only need to i
257289

258290
This means instead of updating the `codegen.ts` config file, you make changes in each module. This keeps the GraphQL API maintainable at any scale.
259291

260-
Read more about this concept on our blog: [Scalability APIs with GraphQL Server Codegen Preset](https://the-guild.dev/blog/scalable-apis-with-graphql-server-codegen-preset)
292+
Read more about this concept on our blog: [Scalable APIs with GraphQL Server Codegen Preset](https://the-guild.dev/blog/scalable-apis-with-graphql-server-codegen-preset)
261293

262294
</Callout>
263295

@@ -322,22 +354,24 @@ Furthermore, the type is updated to use the recommended type from `graphql-scala
322354
// ... other generated types
323355

324356
export type Scalars = {
325-
ID: string
326-
String: string
327-
Boolean: boolean
328-
Int: number
329-
Float: number
330-
DateTime: Date | string // Type comes from graphql-scalars
357+
ID: { input: string; output: string | number }
358+
String: { input: string; output: string }
359+
Boolean: { input: boolean; output: boolean }
360+
Int: { input: number; output: number }
361+
Float: { input: number; output: number }
362+
DateTime: { input: Date | string; output: Date | string } // Type comes from graphql-scalars
331363
}
332364

333365
// ... other generated types
334366
```
335367

336368
The type of any custom scalar is `any` by default. Without the server preset, you have to configure the `DateTime` type by manually updating `codegen.ts`.
337369

338-
#### Adding Mappers
370+
#### Adding Mappers To Chain Resolvers
371+
372+
By default, the generated types make resolvers return objects that match the schema types. However, this means we must handle all field mapping in the root-level resolvers.
339373

340-
Mappers allow returning a different object interface in the resolvers. Object type's field resolvers are used to return the final value to clients.
374+
This is where we can use [mappers](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-resolvers#use-your-model-types-mappers) to enable resolver chaining. When a mapper is used this way, it can be returned in one resolver, and become the `parent` argument in the next resolver in the chain.
341375

342376
With the server preset, you can add mappers by exporting interfaces or types with `Mapper` suffix from `*.mappers.ts` files in appropriate modules:
343377

@@ -374,11 +408,11 @@ export type ResolversParentTypes = {
374408
```ts filename="src/schema/user/resolvers/User.ts" {3-5,7-10}
375409
import type { UserResolvers } from './../../types.generated'
376410
export const User: UserResolvers = {
377-
fullName: () => {
411+
fullName: async (_parent, _arg, _ctx) => {
378412
/* User.fullName resolver is required because User.fullName exists but UserMapper.fullName does not */
379413
},
380414

381-
isAdmin: ({ isAdmin }) => {
415+
isAdmin: ({ isAdmin }, _arg, _ctx) => {
382416
/* User.isAdmin resolver is required because User.isAdmin and UserMapper.isAdmin are not compatible */
383417
return isAdmin
384418
}

0 commit comments

Comments
 (0)