Generate GraphQL resolvers easily with GraphQL Centaur CLI

Tomek Poniatowicz

Tomek Poniatowicz

GraphQL Centaur is a CLI tool with a goal to provide seamless experience creating GraphQL as a service.

GraphQL Centaur generates Mongo/Stucco-js database resolvers in TypeScript from any give GraphQL Schema.

Generate fully-functional basic resolvers with Centaur CLI, customize them the way that suits your project and enjoy your GraphQL based backend.

Installation

If you want to install GraphQL Centaur globally run:

npm i -g graphql-centaur

to install it inside your backend repo performer:

npm i graphql-centaur

then use with npx or as a package.json script.

DatabaseSupport
MongoDBYES
JavaScriptYES
TypeScriptYES
PostgreSQLSOON

Resolver generation

First time when you generate a resolver centaur will also generate needed libraries for collections, DB, Utils and graphql-zeus definitions, then given the following schema:

type Person{
    firstName: String!
}
type Query{
    people: [Person]!
}
schema{
    query: Query
}

after choosing the following elements:

  • Query
  • people
  • CRUD
  • listFilter

GraphQL Centaur should generate TypeScript resolver placed in $src/Query/people.ts directory:

import { FieldResolveInput, FieldResolveOutput } from "stucco-js";
import { PersonCollection } from "../db/collections";
import { DB } from "../db/mongo";
import { Utils } from "../Utils";
import { Person, ResolverType, ValueTypes } from "../graphql-zeus";

export const handler = async (): Promise<FieldResolveOutput> => {
    const db = await DB();
    const col = await db.collection(PersonCollection);
    return Utils.CursorToGraphQLArray<Person>(
        await col.find({}),
    );
};

and append correct entries to the stucco.json file:

{
    "resolvers":{
        "Query.people":{
            "resolve":{
                "name":"lib/Query/people"
            }
        }
    }
}

After running Stucco-js, your resolver should work out of the box. Of course, some resolver types, however, might need a little bit of custom code to make them work the way you want, but basically you have just generated your GraphQL Backend!

Available Resolvers

Resolvers are split into the following categories:

CategoryResolverDesc
CRUDCreateCreate an object in your database and return it
CRUDUpdateUpdate an object in your database and return it
CRUDListList all objects of selected type
CRUDGet by parameterGet object by parameter from the database
CRUDRemoveRemove an object from the database and return true
COMMONPipePipe the arguments of the query as the source for the next resolver
COMMONResolverSimple Resolver you need to write
COMMONRestRest proxy resolvers for pointing to existing REST APIs
COMMONSourceA Resolver that receives source from the parent resolver
COMMONSourcedCRUDThe same as CRUD, but also use source

GraphQL Centaur is also compatible with GraphQL Editor projects.

⚡ Speed up your GraphQL API development

The GraphQL Editor is a supportive tool for both advanced GraphQL users as well as those taking their first steps with GraphQL APIs. Our all-in-one development environment for GraphQL will help you build, manage & deploy your GraphQL API much faster thanks to dozens of built-in micro features. Its graphical interface will also fix communication within your product team. Visualization is the key!

Try it for free