|
7 | 7 | * [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html). |
8 | 8 | * |
9 | 9 | * ```ts |
10 | | - * import { g } from "@graphql-ts/schema"; |
| 10 | + * import { initG } from "@graphql-ts/schema"; |
11 | 11 | * import { GraphQLSchema, graphql } from "graphql"; |
12 | 12 | * |
| 13 | + * type Context = { |
| 14 | + * loadPerson: (id: string) => Person | undefined; |
| 15 | + * loadFriends: (id: string) => Person[]; |
| 16 | + * }; |
| 17 | + * const g = initG<Context>(); |
| 18 | + * type g< |
| 19 | + * Key extends initG.Key, |
| 20 | + * Arg extends initG.Arg[Key] = initG.ArgDefaults[Key], |
| 21 | + * OtherArg extends |
| 22 | + * initG.OtherArg[Key] = initG.OtherArgDefaults<Arg>[Key], |
| 23 | + * > = initG<Context, Key, Arg, OtherArg>; |
| 24 | + * |
| 25 | + * type Person = { |
| 26 | + * id: string; |
| 27 | + * name: string; |
| 28 | + * }; |
| 29 | + * |
| 30 | + * const Person: g<"object", Person> = g.object<Person>()({ |
| 31 | + * name: "Person", |
| 32 | + * fields: () => ({ |
| 33 | + * id: g.field({ type: g.nonNull(g.ID) }), |
| 34 | + * name: g.field({ type: g.nonNull(g.String) }), |
| 35 | + * friends: g.field({ |
| 36 | + * type: g.list(g.nonNull(Person)), |
| 37 | + * resolve(source, _, context) { |
| 38 | + * return context.loadFriends(source.id); |
| 39 | + * }, |
| 40 | + * }), |
| 41 | + * }), |
| 42 | + * }); |
| 43 | + * |
13 | 44 | * const Query = g.object()({ |
14 | 45 | * name: "Query", |
15 | 46 | * fields: { |
16 | | - * hello: g.field({ |
17 | | - * type: g.String, |
18 | | - * resolve() { |
19 | | - * return "Hello!"; |
| 47 | + * person: g.field({ |
| 48 | + * type: Person, |
| 49 | + * args: { |
| 50 | + * id: g.arg({ type: g.nonNull(g.ID) }), |
| 51 | + * }, |
| 52 | + * resolve(_, args, context) { |
| 53 | + * return context.loadPerson(args.id); |
20 | 54 | * }, |
21 | 55 | * }), |
22 | 56 | * }, |
|
26 | 60 | * query: Query, |
27 | 61 | * }); |
28 | 62 | * |
29 | | - * graphql({ |
30 | | - * source: ` |
31 | | - * query { |
32 | | - * hello |
33 | | - * } |
34 | | - * `, |
35 | | - * schema, |
36 | | - * }).then((result) => { |
37 | | - * console.log(result); |
38 | | - * }); |
| 63 | + * { |
| 64 | + * const people = new Map<string, Person>([ |
| 65 | + * ["1", { id: "1", name: "Alice" }], |
| 66 | + * ["2", { id: "2", name: "Bob" }], |
| 67 | + * ]); |
| 68 | + * const friends = new Map<string, string[]>([ |
| 69 | + * ["1", ["2"]], |
| 70 | + * ["2", ["1"]], |
| 71 | + * ]); |
| 72 | + * const contextValue: Context = { |
| 73 | + * loadPerson: (id) => people.get(id), |
| 74 | + * loadFriends: (id) => { |
| 75 | + * return (friends.get(id) ?? []) |
| 76 | + * .map((id) => people.get(id)) |
| 77 | + * .filter((person) => person !== undefined) as Person[]; |
| 78 | + * }, |
| 79 | + * }; |
| 80 | + * graphql({ |
| 81 | + * source: ` |
| 82 | + * query { |
| 83 | + * person(id: "1") { |
| 84 | + * id |
| 85 | + * name |
| 86 | + * friends { |
| 87 | + * id |
| 88 | + * name |
| 89 | + * } |
| 90 | + * } |
| 91 | + * } |
| 92 | + * `, |
| 93 | + * schema, |
| 94 | + * contextValue, |
| 95 | + * }).then((result) => { |
| 96 | + * console.log(result); |
| 97 | + * }); |
| 98 | + * } |
39 | 99 | * ``` |
40 | 100 | * |
41 | 101 | * @module |
42 | 102 | */ |
43 | 103 | export * as g from "./schema-api"; |
| 104 | +export type g< |
| 105 | + Key extends initG.Key, |
| 106 | + FirstArg extends initG.Arg[Key] = initG.ArgDefaults[Key], |
| 107 | + SecondArg extends initG.OtherArg[Key] = initG.OtherArgDefaults<FirstArg>[Key], |
| 108 | +> = initG<unknown, Key, FirstArg, SecondArg>; |
44 | 109 | export { initG, type GWithContext } from "./output"; |
45 | 110 |
|
46 | 111 | export { |
@@ -78,6 +143,7 @@ export type { |
78 | 143 | ScalarType, |
79 | 144 | } from "./api-without-context"; |
80 | 145 |
|
| 146 | +import { initG } from "./output"; |
81 | 147 | import type { |
82 | 148 | GArg, |
83 | 149 | GField, |
|
0 commit comments