graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.87k stars 839 forks source link

examples/todo: extracts TodoSchema #574

Closed chris-ramon closed 3 years ago

chris-ramon commented 3 years ago

Overview

Test plan

(todo)-> go run main.go 
Now server is running on port 8080
Get single todo: curl -g 'http://localhost:8080/graphql?query={todo(id:"b"){id,text,done}}'
Create new todo: curl -g 'http://localhost:8080/graphql?query=mutation+_{createTodo(text:"My+new+todo"){id,text,done}}'
Update todo: curl -g 'http://localhost:8080/graphql?query=mutation+_{updateTodo(id:"a",done:true){id,text,done}}'
Load todo list: curl -g 'http://localhost:8080/graphql?query={todoList{id,text,done}}'
Access the web app via browser at 'http://localhost:8080'
(graphql)-> curl -g 'http://localhost:8080/graphql?query={todo(id:"b"){id,text,done}}'
{"data":{"todo":{"done":false,"id":"b","text":"This is the most important"}}}
(graphql)-> curl -g 'http://localhost:8080/graphql?query=mutation+_{createTodo(text:"My+new+todo"){id,text,done}}'
{"data":{"createTodo":{"done":false,"id":"qtVJmbwj","text":"My new todo"}}}
(graphql)-> curl -g 'http://localhost:8080/graphql?query=mutation+_{updateTodo(id:"a",done:true){id,text,done}}'
{"data":{"updateTodo":{"done":true,"id":"a","text":"A todo not to forget"}}}
(graphql)-> curl -g 'http://localhost:8080/graphql?query={todoList{id,text,done}}'
{"data":{"todoList":[{"done":true,"id":"a","text":"A todo not to forget"},{"done":false,"id":"b","text":"This is the most important"},{"done":false,"id":"c","text":"Please do this or else"},{"done":false,"id":"qtVJmbwj","text":"My new todo"}]}}

package main

import (
    "log"
    "net/http"

    "github.com/graphql-go/graphql/examples/todo/schema"
    "github.com/graphql-go/handler"
)

func main() {
    h := handler.New(&handler.Config{
        Schema:     &schema.TodoSchema,
        Pretty:     true,
        GraphiQL:   false,
        Playground: true,
    })

    http.Handle("/graphql", h)
    log.Println("server running on port :8080")
    http.ListenAndServe(":8080", nil)
}

1

maapteh commented 3 years ago

Why would you want to add result := executeQuery(r.URL.Query().Get("query"), schema.TodoSchema still?

Just update your curl options for this demo:

curl \
  -X POST \
  -H "Content-Type: application/json" \
  --data '{ "query": "{ todoList { id text } }" }' \
  http://localhost:8080/graphql

Isnt it possible to have an example with POST only server? For example in another setup i learned i could do:

    r := gin.New()

    r.GET("/", playgroundHandler())
    r.POST("/graphql", graphqlHandler())
    r.GET("/.well-known/server-health", healthHandler())

and choose myself simple

chris-ramon commented 3 years ago

Why would you want to add result := executeQuery(r.URL.Query().Get("query"), schema.TodoSchema still?

@maapteh FMHO we should keep the usage of GET just for simplicity, the usage of the the correct HTTP verb should rely on lib users, if a secure protocol is a requirement then a proper secure protocol should be implemented like HTTPS.


Just update your curl options for this demo:

curl \
  -X POST \
  -H "Content-Type: application/json" \
  --data '{ "query": "{ todoList { id text } }" }' \
  http://localhost:8080/graphql

Planning to send a PR for adding examples/http-post in order to show the usage of HTTP POST, and yes we can continue to use curl for consistency with examples/todo to show the usage of the endpoint.


Then you dont open a GET option to get the results. All web clients use POST. By giving the options to GET it as well its superweird and users will think they can integrate like this as well. But maybe you like it to act like REST?

As mentioned above, FMHO lib users should decide which HTTP verb to use, we just use GET in the example for simplicity.


But nice work, i totally missed the option by default you can open playground and have POST working. This wasn't demonstrated in any example.

Yes, good call, I've created graphql-go/handler-examples/todo for the matter.


Im wondering what happens if i turn it off (on production we turn off playground and introspection and deploy another instance with stuff on, which is only reachable internally).

Have a nice day

Using graphql-go/handler the GraphQL Playground can be disable via the Playground: false option, disabling it does not interfere the correct execution of the graphql-go/handler itself.

chris-ramon commented 3 years ago

Isnt it possible to have an example with POST only server? For example in another setup i learned i could do:

  r := gin.New()

  r.GET("/", playgroundHandler())
  r.POST("/graphql", graphqlHandler())
  r.GET("/.well-known/server-health", healthHandler())

and choose myself simple

Yes, planning to send a PR for adding a new example: examples/http-post for the matter after margining this PR.

chris-ramon commented 3 years ago

Yes, planning to send a PR for adding a new example: examples/http-post for the matter after margining this PR.

^ Related PR for adding examples/http-post: https://github.com/graphql-go/graphql/pull/575

maapteh commented 3 years ago

Nice work Chris-Ramon!