graphql

package module
v0.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2020 License: MIT Imports: 3 Imported by: 0

README

graphql-crud-server

GraphQL CRUD server library for golang

What is graphql-crud-server?

graphql-crud-server is a GraphQL library written in golang for building generic CRUD (Create, Read, Update, Delete) graphql servers fast and simple. No need for implementing resolvers, models or other overhead. Just load a graphql schema and start the server. See sections "Schema definition" and "Examples" for more details.

Installation

# go modules
require github.com/baba2k/graphql-crud-server v0.1.7

Usage

The following samples will assist you to become as comfortable as possible with graphql-crud-server library.

// import graphql-crud-server into your code and refer it as `graphql`
import "github.com/baba2k/graphql-crud-server"

Schema definition

Query
  • Name of the collection / table will be parsed from the field definition in lowercase
  • "read" prefix and "s" suffix will be cut off
  • List as return type defines a field which returns a list
  • All other fields return an object
Example
type Query {
    readTodos: [Todo]
    readTodo(id: ID!): Todo
}
  • The collection / table is "todo"
  • readTodos returns all todos
  • readTodo returns a todo by id
  • todo(id: ID!): Todo and todos: [Todo] would be also fine

See section "Examples" for more examples.

Mutation
  • Name of the collection / table will be parsed from the field definition in lowercase
  • All fields must have one of these prefixes: "create", "update" or "delete"
  • Prefixes will be cut off
  • All fields return the modified object
Example
type Mutation {
    createTodo(todo: TodoInput!): Todo
    updateTodo(id: ID!, todo: TodoInput!): Todo
    deleteTodo(id: ID!): Todo
}
  • The collection / table is "todo"

See section "Examples" for more examples.

Supported database interfaces

MapDB (in-memory)

Create a MapDB interface and pass it to the graphql-crud-server as argument. See section "Examples" for more details.

Example
// create mapdb database interface
db, err := storage.NewMapDB()
if err != nil {
    log.Fatal("can not connect to database: " + err.Error())
}
MongoDB

Create a MongoDB interface with the official mongo driver (https://github.com/mongodb/mongo-go-driver) and pass it to the graphql-crud-server as argument. See section "Examples" for more details.

Example
// create mongodb database interface
opt := options.Client()
opt = opt.SetHosts([]string{"localhost"})
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
db, err := storage.NewMongoDB(ctx, opt, "graphql")
if err != nil {
    log.Fatal("can not connect to database: " + err.Error())
}

Docker

Simple example with MapDB
docker build -t graphql-crud-server . \
    && docker run -p 8080:8080 -d -e SCHEMA_FILE=example/mapdb/schema/todo.graphql -e DATABASE_TYPE=MapDB --name graphql-crud-server graphql-crud-server
More complex example with MongoDB
  1. Have a look at docker-compose.yml for configuration.
  2. Start server and database with:
    docker-compose up -d
    

Examples

See "example" dir for more examples

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadSchemaFromFile added in v0.1.3

func LoadSchemaFromFile(filename string) (string, error)

func StartServer added in v0.1.3

func StartServer(addr, schema string, db storage.Database)

Types

This section is empty.

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL