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: | User: _id: ObjectId username: Str email: Str BlogPost: _id: ObjectId author_id: ObjectId title: Str content: Str tags: [ code: Str ] types: User: collection: users BlogPost: collection: posts relations: - field: posts from: User to: BlogPost relation_type: to_many where: 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' services: mongoke: ports: - 8090:80 image: mongoke/mongoke:latest environment: - DB_URL=mongodb://mongo/db volumes: - ./mongoke.yml:/conf.yml populate-db: image: mongoke/populate-mongo volumes: - ./mongoke.yml/:/conf.yml environment: - DB_URL=mongodb://mongo/db mongo: image: mongo logging: 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"}}) { username email posts(first: 10, cursorField: title) { nodes { title tags { code } } } } }
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
Top comments (0)