|
7 | 7 | * [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html). |
8 | 8 | * |
9 | 9 | * ```ts |
10 | | - * import { initG } from "@graphql-ts/schema"; |
| 10 | + * import { gWithContext } from "@graphql-ts/schema"; |
11 | 11 | * import { GraphQLSchema, graphql } from "graphql"; |
12 | 12 | * |
13 | 13 | * type Context = { |
14 | | - * loadPerson: (id: string) => Person; |
| 14 | + * loadPerson: (id: string) => Person | undefined; |
15 | 15 | * loadFriends: (id: string) => Person[]; |
16 | 16 | * }; |
17 | | - * const g = initG<Context>(); |
18 | | - * type g<T> = initG<T>; |
| 17 | + * const g = gWithContext<Context>(); |
| 18 | + * type g<T> = gWithContext.infer<T>; |
19 | 19 | * |
20 | 20 | * type Person = { |
21 | 21 | * id: string; |
|
25 | 25 | * const Person: g<typeof g.object<Person>> = g.object<Person>()({ |
26 | 26 | * name: "Person", |
27 | 27 | * fields: () => ({ |
28 | | - * id: g.field({ type: g.ID }), |
29 | | - * name: g.field({ type: g.String }), |
| 28 | + * id: g.field({ type: g.nonNull(g.ID) }), |
| 29 | + * name: g.field({ type: g.nonNull(g.String) }), |
30 | 30 | * friends: g.field({ |
31 | | - * type: g.list(Person), |
| 31 | + * type: g.list(g.nonNull(Person)), |
32 | 32 | * resolve(source, _, context) { |
33 | 33 | * return context.loadFriends(source.id); |
34 | 34 | * }, |
|
42 | 42 | * person: g.field({ |
43 | 43 | * type: Person, |
44 | 44 | * args: { |
45 | | - * id: g.arg({ type: g.ID }), |
| 45 | + * id: g.arg({ type: g.nonNull(g.ID) }), |
46 | 46 | * }, |
47 | 47 | * resolve(_, args, context) { |
48 | 48 | * return context.loadPerson(args.id); |
|
55 | 55 | * query: Query, |
56 | 56 | * }); |
57 | 57 | * |
58 | | - * const people = new Map<string, Person>([ |
59 | | - * ["1", { id: "1", name: "Alice" }], |
60 | | - * ["2", { id: "2", name: "Bob" }], |
61 | | - * ]); |
62 | | - * const friends = new Map<string, string[]>([ |
63 | | - * ["1", ["2"]], |
64 | | - * ["2", ["1"]], |
65 | | - * ]); |
66 | | - * |
67 | | - * graphql({ |
68 | | - * source: ` |
| 58 | + * { |
| 59 | + * const people = new Map<string, Person>([ |
| 60 | + * ["1", { id: "1", name: "Alice" }], |
| 61 | + * ["2", { id: "2", name: "Bob" }], |
| 62 | + * ]); |
| 63 | + * const friends = new Map<string, string[]>([ |
| 64 | + * ["1", ["2"]], |
| 65 | + * ["2", ["1"]], |
| 66 | + * ]); |
| 67 | + * const contextValue: Context = { |
| 68 | + * loadPerson: (id) => people.get(id), |
| 69 | + * loadFriends: (id) => { |
| 70 | + * return (friends.get(id) ?? []) |
| 71 | + * .map((id) => people.get(id)) |
| 72 | + * .filter((person) => person !== undefined) as Person[]; |
| 73 | + * }, |
| 74 | + * }; |
| 75 | + * graphql({ |
| 76 | + * source: ` |
69 | 77 | * query { |
70 | 78 | * person(id: "1") { |
71 | 79 | * id |
|
77 | 85 | * } |
78 | 86 | * } |
79 | 87 | * `, |
80 | | - * schema, |
81 | | - * context: { |
82 | | - * loadPerson: (id) => people.get(id), |
83 | | - * loadFriends: (id) => { |
84 | | - * return (friends.get(id) ?? []) |
85 | | - * .map((id) => people.get(id)) |
86 | | - * .filter((person) => person !== undefined); |
87 | | - * }, |
88 | | - * }, |
89 | | - * }).then((result) => { |
90 | | - * console.log(result); |
91 | | - * }); |
| 88 | + * schema, |
| 89 | + * contextValue, |
| 90 | + * }).then((result) => { |
| 91 | + * console.log(result); |
| 92 | + * }); |
| 93 | + * } |
92 | 94 | * ``` |
93 | 95 | * |
94 | 96 | * @module |
95 | 97 | */ |
96 | | -import { initG, type GWithContext } from "./output"; |
97 | | -export { initG }; |
| 98 | +import { gWithContext, type GWithContext } from "./output"; |
| 99 | +export { gWithContext }; |
98 | 100 | export type { GWithContext }; |
99 | 101 |
|
100 | 102 | export { |
|
0 commit comments