
Product
Introducing GitHub Actions Scanning Support
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.
@graphql-ts/schema
Advanced tools
@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);
});
}
FAQs
Type-safety for constructing GraphQL schemas in TypeScript
The npm package @graphql-ts/schema receives a total of 3,562 weekly downloads. As such, @graphql-ts/schema popularity was classified as popular.
We found that @graphql-ts/schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.

Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.

Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.