Log in to GraphQL Editor
Generate GraphQL resolvers easily with GraphQL Centaur
Tomek

Tomek Poniatowicz

4/21/2020

Generate GraphQL resolvers easily with GraphQL Centaur

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.

Check out our other blogposts

TS backend with slothking & Imp Gist implementation server
Artur Czemiel
Artur Czemiel
TS backend with slothking & Imp Gist implementation server
5 min read
over 5 years ago
Top 3 Development trends of 2019
Tomek Poniatowicz
Tomek Poniatowicz
Top 3 Development trends of 2019
3 min read
about 5 years ago
GraphQL newbie tutorial - introduction
Robert Matyszewski
Robert Matyszewski
GraphQL newbie tutorial - introduction
5 min read
over 5 years ago

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor