Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/synw/goregraph
Turn a documents database into a Graphql server
https://github.com/synw/goregraph
graphql-server rethinkdb
Last synced: 28 days ago
JSON representation
Turn a documents database into a Graphql server
- Host: GitHub
- URL: https://github.com/synw/goregraph
- Owner: synw
- License: mit
- Created: 2017-05-08T11:56:29.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-06-08T14:38:38.000Z (over 7 years ago)
- Last Synced: 2024-11-15T19:43:39.511Z (about 2 months ago)
- Topics: graphql-server, rethinkdb
- Language: Go
- Homepage:
- Size: 37.1 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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 queriesThe 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
- [x] Rethinkdb
## Install
```bash
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:```javascript
{
"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
```bash
./goregraph
```The server is ready for queries at `http://localhost:8080`
Check the [available queries](https://github.com/synw/goregraph#available-queries)
## Use as library
```go
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:
```bash
# 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
- [x] Add options for the http server
- [x] 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
- [Gorethink](https://github.com/GoRethink/gorethink): Rethinkdb drivers
- [Graphql-go](https://github.com/graphql-go/graphql): Graphql drivers
- [Chi](https://github.com/pressly/chi): http router
- [Cors](https://github.com/goware/cors): cors http headers
- [Terr](https://github.com/synw/terr): error handling