Logo by @Caneco
db2graphql
Generates a Graphql schema and resolvers from an existing relational database
![Last Commit Badge](https://img.shields.io/github/last-commit/taviroquai/db2graphql.svg?style=flat)
Features
- Fully compatible with express, koa, hapi and Apollo Server
- Converts an existing relational database (only PostgreSQL and MySQL for now) schema to a JSON schema
- Generates a Graphql SDL schema with convenient types, queries and mutations
- Implements a generic Graphql resolver ready for API prototyping
- Load related records based on foreign keys
- Allows to add/override resolvers
Demo
![link to youtube video](demo/demo.png)
Query example
query {
getPageUsers(
filter: "id#1,2,3"
pagination: "limit=2;orderby=username desc"
_debug: true
) {
items {
id
username
fullname(foo: "hello ")
password
posts(filter: "publish=true", _cache: false) {
total
items {
title
publish
categories {
title
}
}
}
}
}
}
Limitations/TODO
- Only PostgreSQL and MySQL supported
- Better database types handling
- Better database queries optimization
Create testsCreate an NPM module- Move to TypeScript
- Add more and improve convenient API methods. Currently, only:
- getPage
- getFirst
- putItem
Usage
Generate a Graphql schema from an existing relational database
const knex = require('knex');
const db2g = require('db2graphql');
const conn = knex(require('./connection.json'));
const api = new db2g('demo', conn);
api.connect().then(() => {
const schema = api.getSchema();
console.log(schema);
conn.destroy();
});
Example of file connection.json
{
"client": "pg",
"version": "10.6",
"debug": false,
"connection": {
"host" : "127.0.0.1",
"user" : "postgres",
"password" : "postgres",
"database" : "db2graphql"
},
"exclude": []
}
Complete example with Apollo Server
const knex = require('knex');
const db2g = require('db2graphql');
const { ApolloServer, gql } = require('apollo-server');
const start = async (cb) => {
const api = new db2g('demo', knex(require('./connection.json')));
await api.connect();
const validator = async (type, field, parent, args, context) => {
return true;
}
const denied = async (type, field, parent, args, context) => {
throw new Error('Access Denied');
}
api.isAuthorized(validator, denied);
api.addField('Users.fullname', 'String', (parent, args, context) => {
return String(args.foo + parent.username);
}, { foo: 'String' });
api.addField('Users.password', 'String', () => '');
const schema = api.getSchema();
const resolvers = api.getResolvers();
if (!schema) throw new Error('Error: empty schema');
console.log(schema);
const server = new ApolloServer({
typeDefs: gql`${schema}`,
resolvers,
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
}
start();
Example without database connection
const db2g = require('db2graphql');
const api = new db2g('demo');
api.addField('Query.getFoo', 'Boolean', async (root, args, context) => {
return true;
}, { param: 'String!' });
const schema = api.getSchema();
const resolvers = api.getResolvers();
Run de demo
$ git clone https://github.com/taviroquai/db2graphql.git
$ cd db2graphql
$ npm install
$ psql -h localhost -U postgres -c "CREATE DATABASE db2graphql"
$ psql -h localhost -U postgres -f demo/database.sql db2graphql
$ cp demo/connection.example.json demo/connection.json
# Edit demo/connection.json
$ npm run start
Open browser on http://localhost:4000 and see your Graphql API ready!
Credits
Collab
Anyone is free to collab :)
License
MIT, what else?