Coder Social home page Coder Social logo

goregraph's Introduction

Goregraph

Turn a schemaless document database into an Graphql server in minutes: just connect a database and run the server. It is also possible to use this package as a library to run Reql queries from Graphql queries

The goal of this project is to have an API server that can plug on an existing documents database and be instantly ready to serve some basic read only queries from it

Supported databases

  • Rethinkdb

Install

go get github.com/synw/goregraph
go install github.com/synw/goregraph

Grab the binary and make a config.json file to in the same folder than the binary. Set your database's type, address and credentials:

{
 "type": "rethinkdb",
 "host": "localhost:8080",
 "addr":"localhost:28015",
 "user":"",
 "password":"",
 "cors": ["*"]
 }

The cors parameter is a list of authorized domains that will receive cors headers in the http responses. host is the http host adress and addr is the database location

Run the Graphql server

./goregraph

The server is ready for queries at http://localhost:8080

Check the available queries

Use as library

package main

import (
 "log"
 "net/http"
 "github.com/synw/goregraph/lib-r/types"
 "github.com/synw/goregraph/db"
 grg "github.com/synw/goregraph/lib-r/httpServer"
 
)

func main() {
 // map your graphql endpoint here
 http.HandleFunc("/graphql", grg.HandleQuery)
 
 // database config
 host := ":8080"
 addr := "localhost:28015"
 user := "admin"
 pwd := "adminpasswd"
 cors := []string{"localhost"}
 verbosity = 0
 conf := &types.Conf{host, addr, user, pwd, dev, verbosity, cors}
 
 // init and check the database connection
 err := db.Init(conf)
 if err != nil {
 	fmt.Println(err)
 }
 
 // done
 log.Fatal(http.ListenAndServe(":8080", nil))
}

Available queries:

# get a list of databases
curl -g 'http://localhost:8080/graphql?query={dbs{name}}'

# get a list of tables in a database
curl -g 'http://localhost:8080/graphql?query={tables(db:"rethinkdb"){name}}'

# count documents in a table
curl -g 'http://localhost:8080/graphql?query={count(db:"rethinkdb",table:"logs"){num}}'

# get all documents from a table
curl -g 'http://localhost:8080/graphql?query={docs(db:"rethinkdb",table:"server_status"){data}}'

# limit the number of documents to return
curl -g 'http://localhost:8080/graphql?query={docs(db:"rethinkdb",table:"logs",limit:10){data}}'

# pluck: limit the fields to return
curl -g 'http://localhost:8080/graphql?query={docs(db:"rethinkdb",table:"server_status", \
pluck:"name,network,time_connected"){data}}'

You can use multiple options together like pluck with limit.

Note: the data received is a string: you will have to parse it to turn it into json data

Todo

  • Add options for the http server
  • Add cors headers option
  • Add options to limit the dbs and tables that can be queried
  • Better error handling
  • Ratelimit requests
  • Custom schema injection mechanism
  • Consider adding some authentication or token mechanism
  • More queries and query options

Credits

goregraph's People

Contributors

synw avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.