Cover image for Instant Graphql API for MongoDb with Mongoke

Instant Graphql API for MongoDb with Mongoke

remorses profile image Tommaso De Rossi ・2 min read

When creating a new project (usually a web service of some sort) i try to create a MVP (minimum viable product) as fast as possible.
To do that i try to use tools that generate code for me or speed up development.
One task that always takes a lot of time is the creation of the graphql query layer: creating the graphql service, connect it to the database and add authorization.

This task is so boring that i decided to create a service that does it automatically for me.
So i created Mongoke, it is a docker image that generates a totally functional graphql api for you.
Mongoke generates a service written in python that supports among the many features:

  • powerful optimized queries
  • relay style pagination
  • authorization via your jwt
  • apollo federation to be able to integrate other graphql services

Mongoke works thanks to a yaml configuration like the following:

# ./mongoke.yml
skema: |
        _id: ObjectId
        username: Str
        email: Str
        _id: ObjectId
        author_id: ObjectId
        title: Str
        content: Str
        tags: [
            code: Str
        collection: users
        collection: posts

    -   field: posts
        from: User
        to: BlogPost
        relation_type: to_many
            author_id: ${{ parent['_id'] }}

Here we are configuring a graphql service to create tha api of a blog site.
The configuration is divided in 3 main steps:

  • skema: This is where you describe the mongodb schema, it is written in the skema language, a DSL that compiles to grpahql and json-schema. The syntax is quite simple, object types are defined just like in yaml and you can also write inline list types. The skema built in types are Str, Int, Float, Bool, Any and Mongoke adds support for the mongodb ObjectId type.
  • types: here we connect the database types to their collection names, you can add authorization guards here.
  • relations: here are declared the relations to add to other types, to connect the different types you write the where query and you can evaluate the python code inside ${{ }} to access the connected type.

Then to start the graphql api you use docker-compose with the docker mongoke image, here i am also using the mongoke/populate-mongo image that populates the mongodb database with mock data generated from the above configuration.

version: '3'

            - 8090:80
        image: mongoke/mongoke:latest
            - DB_URL=mongodb://mongo/db
            - ./mongoke.yml:/conf.yml
        image: mongoke/populate-mongo
            - ./mongoke.yml/:/conf.yml
            - DB_URL=mongodb://mongo/db
        image: mongo
            driver: none

Then execute docker-compose up and you have a working graphql api at http://localhost:8090/graphiql

This is an example query that you can execute:

  user(where: {username: {neq: "hidden"}}) {
    posts(first: 10, cursorField: title) {
      nodes {
        tags {

You can extend the configration above to add authorization, more relations, more complex simply changing the yaml config and relaunching docker-compose.

See more examples and documentation on github

Posted on by:


markdown guide