Overview
What is Graphqlator?
Graphqlator generates a GraphQL API implemented in Go.
How does it work?
Graphqlator uses your existing database table schema(s) to generate a generic GraphQL API server implemented in go.
Getting Started
Prerequisites
- Install Go
- go get GORM
- go get GraphQL Go
One of the following databases running locally:
Graphqlator Installation
Install using go get
go get github.com/ahmedalhulaibi/graphqlator
Download Pre-Built Binaries from GitHub
Basic Tutorial
Make sure you have installed all the prerequisites.
Define Database Table Schema
If you already have an existing database with one or more tables defined you can skip this step.
MySQL
Follow the instructions here to connect to your MySQL Server, create a database and create a table.
MariaDB
Follow the instructions here to connect to your MariaDB Server, create a database and create a table.
PostgreSQL
Follow the instructions here to create a PostgresQL database.
Follow the instructions here to access your PostgresQL database.
Follow the instructions here to create a table in your PostgresQL database.
Graphqlator Walkthrough
Make sure you have graphqlator installed.
$ graphqlator version
Graphqlator GraphQL Generator v0.3.3-alpha
If you do not see output or get a command not found error:
- If you downloaded a binary, make sure you have added the graphqlator binary to your PATH.
- If you installed via go get, make sure you have added $GOPATH/bin to your PATH variable.
Graphqlator Initialize
Create a new directory in $GOPATH/src/project-name
$ mkdir $GOPATH/src/project-name
Navigate to your project directory
$ cd $GOPATH/src/project-name
Run the graphqlator init command.
$ graphqlator init
This command will have you input a project name, database type, database connection string, git repo URL and table names.
$ graphqlator init
Input project name (enter to continue): first-graphql-server
Input database type e.g. mysql (enter to continue): mysql
Input database connection string (enter to continue): ahmed:password@tcp(localhost:3306)/delivery
Input git repo url (enter to continue): github.com/ahmedalhulaibi/first-graphql-server
Input table names - Must be EXACT spelling and case (enter without input to skip)
Table #1 : Persons
Table #2 : Orders
Table #3 :
After running the init command, you should have a file called graphqlator-pkg.json. To make changes to this config file just edit it.
Graphqlator Generate
Navigate to your project directory where you created graphqlator-pkg.json
$ cd $GOPATH/src/project-name
Run the graphqlator generate command.
$ graphqlator generate --update-all
You should now see multiple files created in your current folder.
Filename | Description |
---|---|
main.go | Contains GraphQL http handler. |
gormQueries.go | Contains CRUD functions for each table (These should work although they are a WIP and may change). |
graphqlFields.go | Graphql-Go root query fields. This includes resolver implementations. |
graphqlMutations.go | Graphql-Go root mutation fields. This includes resolver implementations. |
graphqlTypes.go | The data model defined in Graphql-Go. |
model.go | The data model in the form of Go structs, derived from the database itself. |
schema.graphql | The GraphQL types defined in GraphQL syntax. (Not 100% accurate in current state) |
You can also use CLI flags to generate just one of these files. If no flags are given all files will be regenerated.
$ graphqlator generate --help
Generate GraphQL-Go API implementation from database information schema and tables defined in grapqhlator-pkg.json
Run 'graphqlator init' before running 'graphqlator generate'
Usage:
graphqlator generate [flags]
Flags:
-h, --help help for generate
-a, --update-all update and overwrite all files
-q, --update-gormQueries update and overwrite gormQueries.go
-g, --update-gqlFields update and overwrite graphqlFields.go
-t, --update-gqlTypes update and overwrite graphqlTypes.go
-m, --update-gqlMutations update and overwrite graphqlMutations.go
-M, --update-main update and overwrite main.go
-u, --update-schema update and overwrite schema.graphql