This project is an attempt to bring similar functionality to GraphQL that
exists with the Protocol Buffer compiler protoc
. The goal is to create an
ecosystem of GraphQL client SDKs that can be generated from a schema file.
graphc schema.graphql --ts_out=$DST_DIR
The clients generated by this project are intentionally simple and easy to use. Each client should create RPC style interfaces for the client developer that removes the need to understand the syntax of GraphQL queries. The reasons for this are:
- Easier to get up an running quickly
- Removes the need to regenerate types based on a DSL whenever you add or modify queries
- Reduces the bundle size
- Create your GraphQL schema (i.e.,
schema.graphql
) - Compile your client library code.
- Run your queries and mutations.
- Profit.
Example typescript client usage:
import { queryUsers } from "./client.ts";
const users = await queryUsers({sort: "-createdAt", selections: {
name:true, id: true, groups: {id: true, name: true}
}
})
Example generated client.ts
library (unfinished):
import { GQLClient, GQLResponse } from "./imports/common"
export interface Group {
name: string;
}
export interface GroupSelection {
name?: boolean;
}
export interface User {
groups: number[];
mainGroup: Group;
isActive: boolean;
firstName: string;
}
export interface UserSelection {
mainGroup?: { name?: boolean; };
isActive?: boolean;
firstName?: boolean;
groups?: boolean;
}
export async function queryUsers({ sort, filter, selections }: { sort: string, filter?: string, selections: UserSelection }): Promise<GQLResponse<User>> {
let query = "query runUsers { ";
for (const item in selections) {
if (selections[item] === true) {
query += item + " ";
}
}
query += "}";
const client = new GQLClient();
const response: GQLResponse<User> = await client.post<User>("", { sort: sort, filter: filter });
return response;
}
// Example function call
const users = await queryUsers({ sort: "", selections: { isActive: true, mainGroup: { name: true } } })