Skip to content

Commit

Permalink
Update field collection to allow inline fragment without type (#3384)
Browse files Browse the repository at this point in the history
* init new defer example

* restructure example as flat, update gqlgen.yml

* update resolver to return data

* re-implement field collection for inline fragment to always test if the fragment type applies first
and that this test only happens if there is a fragment type defined

* remove superfluous field collection wrappers and simplify test format

* add tests; replicate logic for fragment spreads

* remove optional type fragment for spreads

* now that we've excised them from tests, re-introduce field collection helpers

* re-introduce collect all tests

* lint

* add back in context helpers to example

* make explanatory comments read easier and remove unnecessary changes
  • Loading branch information
phughes-scwx authored Nov 22, 2024
1 parent 0d2ba68 commit be20ed5
Show file tree
Hide file tree
Showing 10 changed files with 4,471 additions and 28 deletions.
3,914 changes: 3,914 additions & 0 deletions _examples/deferexample/generated.go

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions _examples/deferexample/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
schema: "*.graphql"

exec:
filename: generated.go

model:
filename: models_gen.go

resolver:
layout: follow-schema
dir: .
filename_template: "{name}.resolvers.go"

call_argument_directives_with_null: true

models:
ID:
model:
- github.com/99designs/gqlgen/graphql.ID
Todo:
fields:
user:
resolver: true
extraFields:
userID:
type: "string"
27 changes: 27 additions & 0 deletions _examples/deferexample/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions _examples/deferexample/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package deferexample

import "sync"

type Resolver struct {
mu sync.RWMutex
todos []*Todo
}
28 changes: 28 additions & 0 deletions _examples/deferexample/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# GraphQL schema example
#
# https://gqlgen.com/getting-started/

type Todo {
id: ID!
text: String!
done: Boolean!
user: User!
}

type User {
id: ID!
name: String!
}

type Query {
todos: [Todo!]!
}

input NewTodo {
text: String!
userId: String!
}

type Mutation {
createTodo(input: NewTodo!): Todo!
}
62 changes: 62 additions & 0 deletions _examples/deferexample/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 59 additions & 0 deletions _examples/deferexample/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"log"
"net/http"
"os"
"time"

"github.com/99designs/gqlgen/_examples/deferexample"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/gorilla/websocket"
"github.com/rs/cors"
)

const defaultPort = "8080"

func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}

c := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowCredentials: true,
Debug: false,
})

srv := handler.New(
deferexample.NewExecutableSchema(
deferexample.Config{Resolvers: &deferexample.Resolver{}},
),
)

srv.AddTransport(transport.SSE{})
srv.AddTransport(transport.MultipartMixed{
Boundary: "graphql",
DeliveryTimeout: time.Millisecond * 10,
})
srv.AddTransport(transport.POST{})
srv.AddTransport(transport.Websocket{
KeepAlivePingInterval: 10 * time.Second,
Upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
})
srv.Use(extension.Introspection{})

http.Handle("/", playground.Handler("Todo", "/query"))
http.Handle("/query", c.Handler(srv))

log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
2 changes: 1 addition & 1 deletion graphql/context_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func HasOperationContext(ctx context.Context) bool {
return ok && val != nil
}

// This is just a convenient wrapper method for CollectFields
// CollectFieldsCtx is just a convenient wrapper method for CollectFields.
func CollectFieldsCtx(ctx context.Context, satisfies []string) []CollectedField {
resctx := GetFieldContext(ctx)
return CollectFields(GetOperationContext(ctx), resctx.Field.Selections, satisfies)
Expand Down
Loading

0 comments on commit be20ed5

Please sign in to comment.