Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

examples/todo: extracts TodoSchema #574

Merged
merged 1 commit into from
Oct 11, 2020
Merged

Conversation

chris-ramon
Copy link
Member

@chris-ramon chris-ramon commented Oct 10, 2020

Overview

  • examples/todo: extracts todo schema so it can be re-use for demo purposes.

Test plan

  • Tested that examples/todo is working as expected:
(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"}]}}

  • Tested that TodoSchema works as expected using graphql-go/handler:
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

So TodoSchema can be re-use for demo purposes
@maapteh
Copy link
Contributor

maapteh commented Oct 11, 2020

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

@@ -207,7 +33,7 @@ func executeQuery(query string, schema graphql.Schema) *graphql.Result {

func main() {
http.HandleFunc("/graphql", func(w http.ResponseWriter, r *http.Request) {
result := executeQuery(r.URL.Query().Get("query"), schema)
result := executeQuery(r.URL.Query().Get("query"), schema.TodoSchema)
json.NewEncoder(w).Encode(result)
})
Copy link
Contributor

@maapteh maapteh Oct 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not utilising your sample given:

	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)

why not take this static out of the example? and why still give GET example. Bu showcasing POST you close the open Issue #420 and its shown by example it can do it :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maapteh yes, the given example just shows how to connect handle.New with schema.TodoSchema, in order to show this kind of examples I've created a new repository graphql-go/handler-examples which we can use to point lib users to in order to show graphql-go/handler related examples.

I think is ok to keep the usage of GET in examples/todo for simplicity of the example itself.

In order to showcase the usage of POST, planning to send a PR for adding a new example examples/http-post which will be based from your PR#573.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^ Related PR for adding examples/http-post: #575

@chris-ramon
Copy link
Member Author

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
Copy link
Member Author

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 chris-ramon merged commit 05c348c into master Oct 11, 2020
@chris-ramon chris-ramon deleted the examples-todo-schema branch October 11, 2020 17:03
@chris-ramon
Copy link
Member Author

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: #575

@maapteh
Copy link
Contributor

maapteh commented Oct 11, 2020

Nice work Chris-Ramon!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants