|
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; |
| 15 | + * loadFriends: (id: string) => Person[]; |
| 16 | + * }; |
| 17 | + * const g = initG<Context>(); |
| 18 | + * type g<T> = initG<T>; |
| 19 | + * |
| 20 | + * type Person = { |
| 21 | + * id: string; |
| 22 | + * name: string; |
| 23 | + * }; |
| 24 | + * |
| 25 | + * const Person: g<typeof g.object<Person>> = g.object<Person>()({ |
| 26 | + * name: "Person", |
| 27 | + * fields: () => ({ |
| 28 | + * id: g.field({ type: g.ID }), |
| 29 | + * name: g.field({ type: g.String }), |
| 30 | + * friends: g.field({ |
| 31 | + * type: g.list(Person), |
| 32 | + * resolve(source, _, context) { |
| 33 | + * return context.loadFriends(source.id); |
| 34 | + * }, |
| 35 | + * }), |
| 36 | + * }), |
| 37 | + * }); |
| 38 | + * |
13 | 39 | * const Query = g.object()({ |
14 | 40 | * name: "Query", |
15 | 41 | * fields: { |
16 | | - * hello: g.field({ |
17 | | - * type: g.String, |
18 | | - * resolve() { |
19 | | - * return "Hello!"; |
| 42 | + * person: g.field({ |
| 43 | + * type: Person, |
| 44 | + * args: { |
| 45 | + * id: g.arg({ type: g.ID }), |
| 46 | + * }, |
| 47 | + * resolve(_, args, context) { |
| 48 | + * return context.loadPerson(args.id); |
20 | 49 | * }, |
21 | 50 | * }), |
22 | 51 | * }, |
|
26 | 55 | * query: Query, |
27 | 56 | * }); |
28 | 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 | + * |
29 | 67 | * graphql({ |
30 | 68 | * source: ` |
31 | | - * query { |
32 | | - * hello |
33 | | - * } |
34 | | - * `, |
| 69 | + * query { |
| 70 | + * person(id: "1") { |
| 71 | + * id |
| 72 | + * name |
| 73 | + * friends { |
| 74 | + * id |
| 75 | + * name |
| 76 | + * } |
| 77 | + * } |
| 78 | + * } |
| 79 | + * `, |
35 | 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 | + * }, |
36 | 89 | * }).then((result) => { |
37 | 90 | * console.log(result); |
38 | 91 | * }); |
|
41 | 94 | * @module |
42 | 95 | */ |
43 | 96 | export * as g from "./schema-api"; |
| 97 | +export type g<T> = import("./output").initG<T>; |
44 | 98 | export { initG, type GWithContext } from "./output"; |
45 | 99 |
|
46 | 100 | export type { |
|
0 commit comments