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
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