diff --git a/README.md b/README.md index 4c6a534..b0fa5ca 100644 --- a/README.md +++ b/README.md @@ -6,27 +6,69 @@ Exemple d'API GraphQL, PostgresQL, en Go go build && go run . ``` +Accéder à l'API : +``` +http://localhost:8383/graphql +``` + +Accéder à l'UI graphiql : +``` +http://localhost:8383 +``` + ## 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 \ -H 'Content-Type: application/json' \ - -d '{"query": "{ user { id,firstname,lastname,roles{name} } }"}' \ - http://localhost:8383/ + -d '{"query": " query { user { id,firstname,lastname,roles{name} } }"}' \ + http://localhost:8383/graphql ``` +``` +# Query via GraphQL + +query GetAllUsers { + user { + id + firstname + lastname + roles { + name + } + } +} + +``` + ## Mutation Toute requếte visant à modifier une donnée est appellée une 'Mutation' ``` +# Mutation via curl + curl -X POST \ -H 'Content-Type: application/json' \ -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 + docker run --rm -d \ --name postgres \ -e POSTGRES_PASSWORD=postgres \ -p 5432:5432 \ -v $HOME/docker/postgres/volumes:/var/lib/postgresql/data \ 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 + ); + + ``` diff --git a/graphql b/graphql index ed1aa99..618c0df 100755 Binary files a/graphql and b/graphql differ diff --git a/main.go b/main.go index 019002e..7b4b039 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "github.com/graphql-go/graphql" "github.com/graphql-go/handler" + "github.com/mnmtanish/go-graphiql" ) func main() { @@ -38,9 +39,9 @@ func main() { postgres.DBConnect() defer postgres.DBClose() - http.Handle("/", httpHandler) + http.Handle("/graphql", httpHandler) + http.HandleFunc("/", graphiql.ServeGraphiQL) log.Print("ready: listening...\n") http.ListenAndServe(":8383", nil) - } diff --git a/queries/queries.go b/queries/queries.go index 2347b31..e0f40b1 100644 --- a/queries/queries.go +++ b/queries/queries.go @@ -7,6 +7,6 @@ import ( // GetRootFields returns all the available queries. func GetRootFields() graphql.Fields { return graphql.Fields{ - "user": GetUserQuery(), + "users": GetUserQuery(), } }