1.0.2 • Published 7 months ago
@graphql-ts/schema v1.0.2
@graphql-ts/schema
@graphql-ts/schema
is a thin wrapper around
GraphQL.js providing type-safety for
constructing GraphQL Schemas while avoiding type-generation, declaration merging
and decorators.
import { gWithContext } from "@graphql-ts/schema";
import { GraphQLSchema, graphql } from "graphql";
type Context = {
loadPerson: (id: string) => Person | undefined;
loadFriends: (id: string) => Person[];
};
const g = gWithContext<Context>();
type g<T> = gWithContext.infer<T>;
type Person = {
id: string;
name: string;
};
const Person: g<typeof g.object<Person>> = g.object<Person>()({
name: "Person",
fields: () => ({
id: g.field({ type: g.nonNull(g.ID) }),
name: g.field({ type: g.nonNull(g.String) }),
friends: g.field({
type: g.list(g.nonNull(Person)),
resolve(source, _, context) {
return context.loadFriends(source.id);
},
}),
}),
});
const Query = g.object()({
name: "Query",
fields: {
person: g.field({
type: Person,
args: {
id: g.arg({ type: g.nonNull(g.ID) }),
},
resolve(_, args, context) {
return context.loadPerson(args.id);
},
}),
},
});
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"]],
]);
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
name
friends {
id
name
}
}
}
`,
schema,
contextValue,
}).then((result) => {
console.log(result);
});
}
1.0.2
7 months ago
1.0.1
7 months ago
1.0.0
7 months ago
0.0.0-test-20250311000959
7 months ago
0.0.0-test-20250203043703
9 months ago
0.0.0-test-20250312030016
7 months ago
0.0.0-test-20250228012525
8 months ago
0.0.0-test-20250131063742
9 months ago
0.0.0-test-20250311234328
7 months ago
0.0.0-test-20250203035839
9 months ago
0.0.0-test-20250228021821
8 months ago
0.0.0-test-20250311071901
7 months ago
0.0.0-test-20250311072637
7 months ago
0.0.0-test-20250228011109
8 months ago
0.0.0-test-20250312054156
7 months ago
0.0.0-test-20250311063540
7 months ago
0.6.3
8 months ago
0.6.2
8 months ago
0.6.4
8 months ago
0.0.0-test-20250228065144
8 months ago
0.0.0-test-20250228003718
8 months ago
0.6.1
9 months ago
0.6.0
3 years ago
0.5.3
3 years ago
0.5.2
4 years ago
0.5.1
4 years ago
0.5.0
4 years ago
0.4.0
4 years ago
0.3.1
4 years ago
0.3.0
4 years ago
0.2.0
4 years ago
0.1.2
4 years ago
0.1.1
4 years ago
0.1.0
4 years ago