graphql-go / graphql

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

feature: show POST example how all apps do it #573

Closed maapteh closed 3 years ago

maapteh commented 3 years ago

I decided it would be nice to have an example with POST. Since it's more common. I also updated the playground which offers more options. So:

closes #420

Screenshot 2020-10-08 at 17 29 44

credits to @atombender who showed an example :)

coveralls commented 3 years ago

Coverage Status

Coverage remained the same at 92.426% when pulling 102e6e1d00ab205b6f3c3b5191b4ac93ecdd3754 on maapteh:chore/example-with-post into d6b7434614ec6dd976827ff2a59483d35fae46e4 on graphql-go:master.

chris-ramon commented 3 years ago

@maapteh thanks a lot for working on this PR 👍 –– very interesting improvement for the examples/todo.

I think GraphQL Playground is very useful tool, but I think the examples/todo aims to demonstrate how to use graphql-go/graphql as a HTTP endpoint & how to integrate it with a Web App.

But I definitely understand your motivation to improve the examples/todo to show how to use graphql-go/graphql via HTTP POST and integrate it with GraphQL Playground.

For this type of use cases I would recommend to use graphql-go/handler which handles both HTTP verbs: GET & POST.

Using graphql-go/handler and the examples/todo schema, both can be integrated to have GraphQL Playground working, Eg:

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

chris-ramon commented 3 years ago

I went ahead and sent a PR#574 for exposing the TodoSchema so it can be use as shown above.

chris-ramon commented 3 years ago

Closing this one in favor of: https://github.com/graphql-go/graphql/pull/574