DEV Community

Cover image for Generate GraphQL resolvers easily with GraphQL Centaur CLI
Tomek Poniatowicz for GraphQL Editor

Posted on

Generate GraphQL resolvers easily with GraphQL Centaur CLI

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
Enter fullscreen mode Exit fullscreen mode

to install it inside your backend repo performer:

npm i graphql-centaur
Enter fullscreen mode Exit fullscreen mode

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

Database Support
MongoDB ✔️
JavaScript ✔️
TypeScript ✔️
PostgreSQL coming soon

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
}
Enter fullscreen mode Exit fullscreen mode

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({}),
    );
};
Enter fullscreen mode Exit fullscreen mode

and append correct entries to the stucco.json file:

{
    "resolvers":{
        "Query.people":{
            "resolve":{
                "name":"lib/Query/people"
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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:

Category Resolver Desc
CRUD Create Create an object in your database and return it
CRUD Update Update an object in your database and return it
CRUD List List all objects of selected type
CRUD Get by parameter Get object by parameter from the database
CRUD Remove Remove an object from the database and return true
COMMON Pipe Pipe the arguments of the query as the source for the next resolver
COMMON Resolver Simple Resolver you need to write
COMMON Rest Rest proxy resolvers for pointing to existing REST APIs
COMMON Source A Resolver that receives source from the parent resolver
COMMON SourcedCRUD The same as CRUD, but also use source

GraphQL Centaur is also compatible with GraphQL Editor projects.


Speed up your GraphQL API development

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.

GraphQL Editor

Top comments (0)