Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changeset/pretty-knives-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@graphql-ts/schema": major
---

`bindGraphQLSchemaAPIToContext` and `GraphQLSchemaAPIWithContext` have been replaced with `initG` and `GWithContext` which also include all the APIs that aren't specifically bound to a context.
`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.
8 changes: 4 additions & 4 deletions .changeset/purple-beers-mate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
"@graphql-ts/schema": major
---

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.
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.

```ts
import { GraphQLSchema } from "graphql";
import { initG } from "@graphql-ts/schema";
import { gWithContext } from "@graphql-ts/schema";

type Context = {
something: string;
};

const g = initG<Context>();
type g<T> = initG<T>;
const g = gWithContext<Context>();
type g<T> = gWithContext.infer<T>;

const Query = g.object()({
name: "Query",
Expand Down
64 changes: 33 additions & 31 deletions packages/schema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ constructing GraphQL Schemas while avoiding type-generation, [declaration mergin
and [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html).

```ts
import { initG } from "@graphql-ts/schema";
import { gWithContext } from "@graphql-ts/schema";
import { GraphQLSchema, graphql } from "graphql";

type Context = {
loadPerson: (id: string) => Person;
loadPerson: (id: string) => Person | undefined;
loadFriends: (id: string) => Person[];
};
const g = initG<Context>();
type g<T> = initG<T>;
const g = gWithContext<Context>();
type g<T> = gWithContext.infer<T>;

type Person = {
id: string;
Expand All @@ -24,10 +24,10 @@ type Person = {
const Person: g<typeof g.object<Person>> = g.object<Person>()({
name: "Person",
fields: () => ({
id: g.field({ type: g.ID }),
name: g.field({ type: g.String }),
id: g.field({ type: g.nonNull(g.ID) }),
name: g.field({ type: g.nonNull(g.String) }),
friends: g.field({
type: g.list(Person),
type: g.list(g.nonNull(Person)),
resolve(source, _, context) {
return context.loadFriends(source.id);
},
Expand All @@ -41,7 +41,7 @@ const Query = g.object()({
person: g.field({
type: Person,
args: {
id: g.arg({ type: g.ID }),
id: g.arg({ type: g.nonNull(g.ID) }),
},
resolve(_, args, context) {
return context.loadPerson(args.id);
Expand All @@ -54,17 +54,25 @@ const schema = new GraphQLSchema({
query: Query,
});

const people = new Map<string, Person>([
["1", { id: "1", name: "Alice" }],
["2", { id: "2", name: "Bob" }],
]);
const friends = new Map<string, string[]>([
["1", ["2"]],
["2", ["1"]],
]);

graphql({
source: `
{
const people = new Map<string, Person>([
["1", { id: "1", name: "Alice" }],
["2", { id: "2", name: "Bob" }],
]);
const friends = new Map<string, string[]>([
["1", ["2"]],
["2", ["1"]],
]);
const contextValue: Context = {
loadPerson: (id) => people.get(id),
loadFriends: (id) => {
return (friends.get(id) ?? [])
.map((id) => people.get(id))
.filter((person) => person !== undefined) as Person[];
},
};
graphql({
source: `
query {
person(id: "1") {
id
Expand All @@ -76,16 +84,10 @@ graphql({
}
}
`,
schema,
context: {
loadPerson: (id) => people.get(id),
loadFriends: (id) => {
return (friends.get(id) ?? [])
.map((id) => people.get(id))
.filter((person) => person !== undefined);
},
},
}).then((result) => {
console.log(result);
});
schema,
contextValue,
}).then((result) => {
console.log(result);
});
}
```
4 changes: 2 additions & 2 deletions packages/schema/src/api-with-context/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @module
* @deprecated This entrypoint should no longer be used. Use {@link initG}
* @deprecated This entrypoint should no longer be used. Use {@link gWithContext}
* instead.
*/
import { initG } from "@graphql-ts/schema";
import { gWithContext } from "@graphql-ts/schema";
export {
field,
fields,
Expand Down
4 changes: 2 additions & 2 deletions packages/schema/src/api-without-context/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* @module
* @deprecated This entrypoint should no longer be used. Use {@link initG}
* @deprecated This entrypoint should no longer be used. Use {@link gWithContext}
* instead.
*/
import { initG } from "@graphql-ts/schema";
import { gWithContext } from "@graphql-ts/schema";
export {
arg,
inputObject,
Expand Down
68 changes: 35 additions & 33 deletions packages/schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
* [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html).
*
* ```ts
* import { initG } from "@graphql-ts/schema";
* import { gWithContext } from "@graphql-ts/schema";
* import { GraphQLSchema, graphql } from "graphql";
*
* type Context = {
* loadPerson: (id: string) => Person;
* loadPerson: (id: string) => Person | undefined;
* loadFriends: (id: string) => Person[];
* };
* const g = initG<Context>();
* type g<T> = initG<T>;
* const g = gWithContext<Context>();
* type g<T> = gWithContext.infer<T>;
*
* type Person = {
* id: string;
Expand All @@ -25,10 +25,10 @@
* const Person: g<typeof g.object<Person>> = g.object<Person>()({
* name: "Person",
* fields: () => ({
* id: g.field({ type: g.ID }),
* name: g.field({ type: g.String }),
* id: g.field({ type: g.nonNull(g.ID) }),
* name: g.field({ type: g.nonNull(g.String) }),
* friends: g.field({
* type: g.list(Person),
* type: g.list(g.nonNull(Person)),
* resolve(source, _, context) {
* return context.loadFriends(source.id);
* },
Expand All @@ -42,7 +42,7 @@
* person: g.field({
* type: Person,
* args: {
* id: g.arg({ type: g.ID }),
* id: g.arg({ type: g.nonNull(g.ID) }),
* },
* resolve(_, args, context) {
* return context.loadPerson(args.id);
Expand All @@ -55,17 +55,25 @@
* query: Query,
* });
*
* const people = new Map<string, Person>([
* ["1", { id: "1", name: "Alice" }],
* ["2", { id: "2", name: "Bob" }],
* ]);
* const friends = new Map<string, string[]>([
* ["1", ["2"]],
* ["2", ["1"]],
* ]);
*
* graphql({
* source: `
* {
* const people = new Map<string, Person>([
* ["1", { id: "1", name: "Alice" }],
* ["2", { id: "2", name: "Bob" }],
* ]);
* const friends = new Map<string, string[]>([
* ["1", ["2"]],
* ["2", ["1"]],
* ]);
* const contextValue: Context = {
* loadPerson: (id) => people.get(id),
* loadFriends: (id) => {
* return (friends.get(id) ?? [])
* .map((id) => people.get(id))
* .filter((person) => person !== undefined) as Person[];
* },
* };
* graphql({
* source: `
* query {
* person(id: "1") {
* id
Expand All @@ -77,24 +85,18 @@
* }
* }
* `,
* schema,
* context: {
* loadPerson: (id) => people.get(id),
* loadFriends: (id) => {
* return (friends.get(id) ?? [])
* .map((id) => people.get(id))
* .filter((person) => person !== undefined);
* },
* },
* }).then((result) => {
* console.log(result);
* });
* schema,
* contextValue,
* }).then((result) => {
* console.log(result);
* });
* }
* ```
*
* @module
*/
import { initG, type GWithContext } from "./output";
export { initG };
import { gWithContext, type GWithContext } from "./output";
export { gWithContext };
export type { GWithContext };

export {
Expand Down
Loading