Thanks to visit codestin.com
Credit goes to github.com

Skip to content

mentat/graphc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL Client Compiler

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.

Usage

graphc schema.graphql --ts_out=$DST_DIR

Client Design Philosophy

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

Example workflow

  1. Create your GraphQL schema (i.e., schema.graphql)
  2. Compile your client library code.
  3. Run your queries and mutations.
  4. 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 } } })

About

A GraphQL client compiler.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published