Add support for client that uses GET as transport mechanism (#186)

Current implementation always uses POST as the transport mechanism. Adding GET support enables usage of GET queries for caching simply via URL.

Some notes:
- I left the existing API for creating a new client as is, but the implementation could be much cleaner by introducing some sort of configuration struct when creating a new client
- The construction of the query parameters follows the logic from Apollo's client implementation, which can be found here https://github.com/apollographql/apollo-client/blob/8beb4820edc6352996e08f7f73bde3573f1eb666/src/link/http/rewriteURIForGET.ts
- Updated integration tests to use both sets of clients. Updating the tests to use a test suite would be cleaner
This commit is contained in:
salman-rb
2022-04-13 16:16:27 -07:00
committed by GitHub
parent b2422452a1
commit 39a980ab4e
10 changed files with 798 additions and 335 deletions
+33 -2
View File
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http/httptest"
"strconv"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
@@ -73,6 +74,24 @@ func beingByID(id string) Being {
return nil
}
func getNewID() string {
maxID := 0
for _, user := range users {
intID, _ := strconv.Atoi(user.ID)
if intID > maxID {
maxID = intID
}
}
for _, animal := range animals {
intID, _ := strconv.Atoi(animal.ID)
if intID > maxID {
maxID = intID
}
}
newID := maxID + 1
return strconv.Itoa(newID)
}
func (r *queryResolver) Me(ctx context.Context) (*User, error) {
return userByID("1"), nil
}
@@ -129,9 +148,16 @@ func (r *queryResolver) Fail(ctx context.Context) (*bool, error) {
return &f, fmt.Errorf("oh no")
}
func (m mutationResolver) CreateUser(ctx context.Context, input NewUser) (*User, error) {
newUser := User{ID: getNewID(), Name: input.Name, Friends: []*User{}}
users = append(users, &newUser)
return &newUser, nil
}
func RunServer() *httptest.Server {
gqlgenServer := handler.New(NewExecutableSchema(Config{Resolvers: &resolver{}}))
gqlgenServer.AddTransport(transport.POST{})
gqlgenServer.AddTransport(transport.GET{})
gqlgenServer.AroundResponses(func(ctx context.Context, next graphql.ResponseHandler) *graphql.Response {
graphql.RegisterExtension(ctx, "foobar", "test")
return next(ctx)
@@ -140,10 +166,15 @@ func RunServer() *httptest.Server {
}
type (
resolver struct{}
queryResolver struct{}
resolver struct{}
queryResolver struct{}
mutationResolver struct{}
)
func (r *resolver) Mutation() MutationResolver {
return &mutationResolver{}
}
func (r *resolver) Query() QueryResolver { return &queryResolver{} }
//go:generate go run github.com/99designs/gqlgen