Skip to content

Commit 09346e7

Browse files
authored
Rename initG to gWithContext and move initG type to gWithContext.infer (#47)
1 parent a70f999 commit 09346e7

File tree

12 files changed

+183
-136
lines changed

12 files changed

+183
-136
lines changed

.changeset/pretty-knives-rule.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@graphql-ts/schema": major
33
---
44

5-
`bindGraphQLSchemaAPIToContext` and `GraphQLSchemaAPIWithContext` have been replaced with `initG` and `GWithContext` which also include all the APIs that aren't specifically bound to a context.
5+
`bindGraphQLSchemaAPIToContext` and `GraphQLSchemaAPIWithContext` have been replaced with the `gWithContext` function and `GWithContext` type respectively. They now also include all the APIs that aren't specifically bound to a context.

.changeset/purple-beers-mate.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
"@graphql-ts/schema": major
33
---
44

5-
The `g` export exported from `@graphql-ts/schema` directly is now deprecated. Using `initG` is now the recommended way to use `@graphql-ts/schema` instead of the instance of `g` exported by `@graphql-ts/schema` or creating multiple files to setup `g` though using those is still possible.
5+
The `g` export exported from `@graphql-ts/schema` directly is now deprecated. Using `gWithContext` is now the recommended way to use `@graphql-ts/schema` instead of the instance of `g` exported by `@graphql-ts/schema` or creating multiple files to setup `g` though using those is still possible.
66

77
```ts
88
import { GraphQLSchema } from "graphql";
9-
import { initG } from "@graphql-ts/schema";
9+
import { gWithContext } from "@graphql-ts/schema";
1010

1111
type Context = {
1212
something: string;
1313
};
1414

15-
const g = initG<Context>();
16-
type g<T> = initG<T>;
15+
const g = gWithContext<Context>();
16+
type g<T> = gWithContext.infer<T>;
1717

1818
const Query = g.object()({
1919
name: "Query",

packages/schema/README.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ constructing GraphQL Schemas while avoiding type-generation, [declaration mergin
66
and [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html).
77

88
```ts
9-
import { initG } from "@graphql-ts/schema";
9+
import { gWithContext } from "@graphql-ts/schema";
1010
import { GraphQLSchema, graphql } from "graphql";
1111

1212
type Context = {
13-
loadPerson: (id: string) => Person;
13+
loadPerson: (id: string) => Person | undefined;
1414
loadFriends: (id: string) => Person[];
1515
};
16-
const g = initG<Context>();
17-
type g<T> = initG<T>;
16+
const g = gWithContext<Context>();
17+
type g<T> = gWithContext.infer<T>;
1818

1919
type Person = {
2020
id: string;
@@ -24,10 +24,10 @@ type Person = {
2424
const Person: g<typeof g.object<Person>> = g.object<Person>()({
2525
name: "Person",
2626
fields: () => ({
27-
id: g.field({ type: g.ID }),
28-
name: g.field({ type: g.String }),
27+
id: g.field({ type: g.nonNull(g.ID) }),
28+
name: g.field({ type: g.nonNull(g.String) }),
2929
friends: g.field({
30-
type: g.list(Person),
30+
type: g.list(g.nonNull(Person)),
3131
resolve(source, _, context) {
3232
return context.loadFriends(source.id);
3333
},
@@ -41,7 +41,7 @@ const Query = g.object()({
4141
person: g.field({
4242
type: Person,
4343
args: {
44-
id: g.arg({ type: g.ID }),
44+
id: g.arg({ type: g.nonNull(g.ID) }),
4545
},
4646
resolve(_, args, context) {
4747
return context.loadPerson(args.id);
@@ -54,17 +54,25 @@ const schema = new GraphQLSchema({
5454
query: Query,
5555
});
5656

57-
const people = new Map<string, Person>([
58-
["1", { id: "1", name: "Alice" }],
59-
["2", { id: "2", name: "Bob" }],
60-
]);
61-
const friends = new Map<string, string[]>([
62-
["1", ["2"]],
63-
["2", ["1"]],
64-
]);
65-
66-
graphql({
67-
source: `
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+
const contextValue: Context = {
67+
loadPerson: (id) => people.get(id),
68+
loadFriends: (id) => {
69+
return (friends.get(id) ?? [])
70+
.map((id) => people.get(id))
71+
.filter((person) => person !== undefined) as Person[];
72+
},
73+
};
74+
graphql({
75+
source: `
6876
query {
6977
person(id: "1") {
7078
id
@@ -76,16 +84,10 @@ graphql({
7684
}
7785
}
7886
`,
79-
schema,
80-
context: {
81-
loadPerson: (id) => people.get(id),
82-
loadFriends: (id) => {
83-
return (friends.get(id) ?? [])
84-
.map((id) => people.get(id))
85-
.filter((person) => person !== undefined);
86-
},
87-
},
88-
}).then((result) => {
89-
console.log(result);
90-
});
87+
schema,
88+
contextValue,
89+
}).then((result) => {
90+
console.log(result);
91+
});
92+
}
9193
```

packages/schema/src/api-with-context/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* @module
3-
* @deprecated This entrypoint should no longer be used. Use {@link initG}
3+
* @deprecated This entrypoint should no longer be used. Use {@link gWithContext}
44
* instead.
55
*/
6-
import { initG } from "@graphql-ts/schema";
6+
import { gWithContext } from "@graphql-ts/schema";
77
export {
88
field,
99
fields,

packages/schema/src/api-without-context/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/**
22
* @module
3-
* @deprecated This entrypoint should no longer be used. Use {@link initG}
3+
* @deprecated This entrypoint should no longer be used. Use {@link gWithContext}
44
* instead.
55
*/
6-
import { initG } from "@graphql-ts/schema";
6+
import { gWithContext } from "@graphql-ts/schema";
77
export {
88
arg,
99
inputObject,

packages/schema/src/index.ts

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
* [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html).
88
*
99
* ```ts
10-
* import { initG } from "@graphql-ts/schema";
10+
* import { gWithContext } from "@graphql-ts/schema";
1111
* import { GraphQLSchema, graphql } from "graphql";
1212
*
1313
* type Context = {
14-
* loadPerson: (id: string) => Person;
14+
* loadPerson: (id: string) => Person | undefined;
1515
* loadFriends: (id: string) => Person[];
1616
* };
17-
* const g = initG<Context>();
18-
* type g<T> = initG<T>;
17+
* const g = gWithContext<Context>();
18+
* type g<T> = gWithContext.infer<T>;
1919
*
2020
* type Person = {
2121
* id: string;
@@ -25,10 +25,10 @@
2525
* const Person: g<typeof g.object<Person>> = g.object<Person>()({
2626
* name: "Person",
2727
* 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) }),
3030
* friends: g.field({
31-
* type: g.list(Person),
31+
* type: g.list(g.nonNull(Person)),
3232
* resolve(source, _, context) {
3333
* return context.loadFriends(source.id);
3434
* },
@@ -42,7 +42,7 @@
4242
* person: g.field({
4343
* type: Person,
4444
* args: {
45-
* id: g.arg({ type: g.ID }),
45+
* id: g.arg({ type: g.nonNull(g.ID) }),
4646
* },
4747
* resolve(_, args, context) {
4848
* return context.loadPerson(args.id);
@@ -55,17 +55,25 @@
5555
* query: Query,
5656
* });
5757
*
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: `
6977
* query {
7078
* person(id: "1") {
7179
* id
@@ -77,24 +85,18 @@
7785
* }
7886
* }
7987
* `,
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+
* }
9294
* ```
9395
*
9496
* @module
9597
*/
96-
import { initG, type GWithContext } from "./output";
97-
export { initG };
98+
import { gWithContext, type GWithContext } from "./output";
99+
export { gWithContext };
98100
export type { GWithContext };
99101

100102
export {

0 commit comments

Comments
 (0)