Integration of graphiql UI

This commit is contained in:
Matthieu Lamalle 2019-07-25 10:22:54 +02:00
parent bbe34b6055
commit 9ddca46ea7
4 changed files with 67 additions and 7 deletions

View File

@ -6,27 +6,69 @@ Exemple d'API GraphQL, PostgresQL, en Go
go build && go run . go build && go run .
``` ```
Accéder à l'API :
```
http://localhost:8383/graphql
```
Accéder à l'UI graphiql :
```
http://localhost:8383
```
## Query ## Query
Toute rếquete visant à récupérer une donnée est appellée une 'Query' Toute rếquete visant à récupérer une donnée est appellée une 'Query'
``` ```
# Exemple de Query # Query via curl
curl -X POST \ curl -X POST \
-H 'Content-Type: application/json' \ -H 'Content-Type: application/json' \
-d '{"query": "{ user { id,firstname,lastname,roles{name} } }"}' \ -d '{"query": " query { user { id,firstname,lastname,roles{name} } }"}' \
http://localhost:8383/ http://localhost:8383/graphql
``` ```
```
# Query via GraphQL
query GetAllUsers {
user {
id
firstname
lastname
roles {
name
}
}
}
```
## Mutation ## Mutation
Toute requếte visant à modifier une donnée est appellée une 'Mutation' Toute requếte visant à modifier une donnée est appellée une 'Mutation'
``` ```
# Mutation via curl
curl -X POST \ curl -X POST \
-H 'Content-Type: application/json' \ -H 'Content-Type: application/json' \
-d '{"query": "mutation { createUser(firstname: \"John\", lastname: \"Snow\") { id,firstname,lastname } }"}' \ -d '{"query": "mutation { createUser(firstname: \"John\", lastname: \"Snow\") { id,firstname,lastname } }"}' \
http://localhost:8383 http://localhost:8383/graphql
```
```
# Mutation via GraphQL
mutation CreateUser($firstname: String!, $lastname: String!) {
createUser(firstname: $firstname, lastname: $lastname) {
id
firstname
lastname
}
}
``` ```
@ -35,10 +77,27 @@ Si besoin, lancer un serveur postgres via Docker
``` ```
mkdir $HOME/docker/postgres/volumes mkdir $HOME/docker/postgres/volumes
docker run --rm -d \ docker run --rm -d \
--name postgres \ --name postgres \
-e POSTGRES_PASSWORD=postgres \ -e POSTGRES_PASSWORD=postgres \
-p 5432:5432 \ -p 5432:5432 \
-v $HOME/docker/postgres/volumes:/var/lib/postgresql/data \ -v $HOME/docker/postgres/volumes:/var/lib/postgresql/data \
postgres postgres
psql -U postgres -h localhost
> create user graphql;
> alter user graphql with encrypted password 'graphql';
> create database graphql;
> GRANT ALL PRIVILEGES ON DATABASE "graphql" to graphql;
> \exit
psql -U graphql -h localhost
> CREATE TABLE users (
id SERIAL PRIMARY KEY,
firstname TEXT,
lastname TEXT
);
``` ```

BIN
graphql

Binary file not shown.

View File

@ -9,6 +9,7 @@ import (
"github.com/graphql-go/graphql" "github.com/graphql-go/graphql"
"github.com/graphql-go/handler" "github.com/graphql-go/handler"
"github.com/mnmtanish/go-graphiql"
) )
func main() { func main() {
@ -38,9 +39,9 @@ func main() {
postgres.DBConnect() postgres.DBConnect()
defer postgres.DBClose() defer postgres.DBClose()
http.Handle("/", httpHandler) http.Handle("/graphql", httpHandler)
http.HandleFunc("/", graphiql.ServeGraphiQL)
log.Print("ready: listening...\n") log.Print("ready: listening...\n")
http.ListenAndServe(":8383", nil) http.ListenAndServe(":8383", nil)
} }

View File

@ -7,6 +7,6 @@ import (
// GetRootFields returns all the available queries. // GetRootFields returns all the available queries.
func GetRootFields() graphql.Fields { func GetRootFields() graphql.Fields {
return graphql.Fields{ return graphql.Fields{
"user": GetUserQuery(), "users": GetUserQuery(),
} }
} }