项目作者: OscarYuen

项目描述 :
This repository uses graph-gophers/graphql-go to build a starter web application
高级语言: Go
项目地址: git://github.com/OscarYuen/go-graphql-starter.git
创建时间: 2017-12-13T10:22:40Z
项目社区:https://github.com/OscarYuen/go-graphql-starter

开源协议:MIT License

下载


Go Graphql Starter

GitHub license

This project aims to use graph-gophers/graphql-go to build a starter web application. This project has already been used as backend application in production.

In case you need to get called from another frontend side, CORS may needed to be enabled in this application as this project mainly focuses on backend logic at this stage.

This project would be continuously under development for enhancement. Pull request is welcome.

RoadMap:

  • Integrated with sqlx
  • Integrated with graphql-go
  • Use go-bindata to generate Go code from .graphql file
  • Use psql
  • Integrated with dataloader
  • Add authentication & authorization
  • Add unit test cases
  • Support subscription
  • Support web-socket notification and messaging

Structure

  1. go-graphql-starer
  2. README.md
  3. server.go --- the entry file
  4. Config.toml --- the configuration file for setting server parameter
  5. Dockerfile
  6. Gopkg.lock --- generated file from dependency management tool, dep
  7. Gopkg.toml --- generated file from dependency management tool, dep
  8. graphiql.html --- the html for graphiql which is used for testing query & mutation
  9. └───context --- application context like db configuration
  10. └───data --- storing the sql data patch for different version
  11. └───1.0 --- storing sql data patch for version 1.0
  12. └───... --- sql files
  13. └───handler --- the handler used for chaining http request like authentication, logging etc.
  14. └───loader --- implementation of dataloader for caching and batching the graphql query
  15. └───model --- the folder putting struct file
  16. └───resolver --- implementation of graphql resolvers
  17. └───schema --- definition of graphql schemas
  18. schema.go --- used for generate go code from static graphql files inside 'type' folder
  19. schema.graphql --- graphql root schema
  20. └───type --- folder for storing graphql schema in *.graphql format
  21. └───... --- graphql schema files in *.graphql format
  22. └───service --- services for users, authorization etc.
  23. └───util --- utilities

Requirement:

  1. Postgres database
  2. Golang

Remark: If you want to use other databases, please feel free to change the driver in context/db.go

Usage(Without docker):

  1. Run the sql scripts under data/1.0 folder inside Postgres database console

  2. Install go-bindata

    1. go get -u github.com/jteeuwen/go-bindata/...
  3. Setup GOPATH (Optional if already set)

    For example: MacOS

    1. export GOPATH=/Users/${username}/go
    2. export PATH=$PATH:$GOPATH/bin
  4. Run the following command at root directory to generate Go code from .graphql file

    1. go-bindata -ignore=\.go -pkg=schema -o=schema/bindata.go schema/...

    OR

    1. go generate ./schema

    There would be bindata.go generated under schema folder

  5. Start the server (Ensure your postgres database is live and its setting in Config.toml is correct)

    1. go build server.go

Usage(With docker):

  1. Run the sql scripts under data/1.0 folder inside Postgres database console

  2. Build docker image

    1. docker build -t go-graphql-starter .
  3. Run docker image (Ensure your database setting in Config.toml is correct)

    1. docker run go-graphql-starter

Usage(With docker-compose):

  1. Create a folder /psqldata on your OS system and set it for file sharing in docker

  2. Create and starter services by docker-compose

    1. docker-compose up

Graphql Example:

Test in graphiql by the following endpoint

  1. localhost:3000

Basically there are two graphql queries and one mutation

Query:
  1. Get a user by email
  2. Get user list by cursor pagination
Mutation:

To query a list of users, you need to be authenticated.
Authentication is not required for other operations.
In order to authenticate, here are the steps to follow:

  1. Create a user
  1. mutation {
  2. createUser (email: "tester@tester.com", password: "123456") {
  3. id
  4. }
  5. }
  1. Log in by submitting your email and password through a Basic Authorization Header.

Here’s an example on how to achieve this:

  1. a. Download [Insomnia](https://insomnia.rest/) OR Other RESTful endpoint testing tools e.g. Postman
  2. b. Create a new POST request, paste `localhost:3000/login` in the URL bar and go to the Header tab
  3. c. Generate your basic Authorization on [blitter.se](https://www.blitter.se/utils/basic-authentication-header-generator/)
  4. d. In Insomnia, first column, type Authorization, second column enter the Basic you just copied `Basic dGVzdGVyQHRlc3Rlci5jb206a3Rta3Rt`
  5. e. Click send, you should get a jwt token back.

You can change the Authorization of request header in graphiql.html and restart the server to see the effect of authentication using token

Test:

  • Run Unit Tests
    1. go test

Reference

-graph-gophers/graphql-go

-tonyghita/graphql-go-example