-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example for getting the selection sets from ctx
- Loading branch information
Showing
9 changed files
with
970 additions
and
5 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// This file was generated by github.com/vektah/gqlgen, DO NOT EDIT | ||
|
||
package selection | ||
|
||
import ( | ||
time "time" | ||
) | ||
|
||
type Event interface{} | ||
type Like struct { | ||
Reaction string | ||
Sent time.Time | ||
Selection []string | ||
Collected []string | ||
} | ||
type Post struct { | ||
Message string | ||
Sent time.Time | ||
Selection []string | ||
Collected []string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
### todo app | ||
|
||
This is the simplest example of a graphql server. | ||
|
||
to run this server | ||
```bash | ||
go run ./example/todo/server/server.go | ||
``` | ||
|
||
and open http://localhost:8081 in your browser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
interface Event { | ||
selection: [String!] | ||
collected: [String!] | ||
} | ||
|
||
type Post implements Event { | ||
message: String! | ||
sent: Time! | ||
selection: [String!] | ||
collected: [String!] | ||
} | ||
|
||
type Like implements Event { | ||
reaction: String! | ||
sent: Time! | ||
selection: [String!] | ||
collected: [String!] | ||
} | ||
|
||
type Query { | ||
events: [Event] | ||
} | ||
|
||
scalar Time |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//go:generate gorunpkg github.com/vektah/gqlgen -out generated.go | ||
|
||
package selection | ||
|
||
import ( | ||
context "context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/vektah/gqlgen/graphql" | ||
query "github.com/vektah/gqlgen/neelance/query" | ||
) | ||
|
||
type SelectionResolver struct{} | ||
|
||
func (r *SelectionResolver) Query_events(ctx context.Context) ([]Event, error) { | ||
var sels []string | ||
|
||
reqCtx := graphql.GetRequestContext(ctx) | ||
fieldSelections := graphql.GetResolverContext(ctx).Field.Selections | ||
for _, sel := range fieldSelections { | ||
switch sel := sel.(type) { | ||
case *query.Field: | ||
sels = append(sels, fmt.Sprintf("%s as %s", sel.Name.Name, sel.Alias.Name)) | ||
case *query.InlineFragment: | ||
sels = append(sels, fmt.Sprintf("inline fragment on %s", sel.On.Name)) | ||
case *query.FragmentSpread: | ||
fragment := reqCtx.Doc.Fragments.Get(sel.Name.Name) | ||
sels = append(sels, fmt.Sprintf("named fragment %s on %s", sel.Name.Name, fragment.On.Name)) | ||
} | ||
} | ||
|
||
var events []Event | ||
for i := 0; i < 10; i++ { | ||
if i%2 == 0 { | ||
events = append(events, &Like{ | ||
Selection: sels, | ||
Collected: formatCollected(graphql.CollectFieldsCtx(ctx, []string{"Like"})), | ||
Reaction: ":=)", | ||
Sent: time.Now(), | ||
}) | ||
} else { | ||
events = append(events, &Post{ | ||
Selection: sels, | ||
Collected: formatCollected(graphql.CollectFieldsCtx(ctx, []string{"Post"})), | ||
Message: "Hey", | ||
Sent: time.Now(), | ||
}) | ||
} | ||
} | ||
|
||
return events, nil | ||
} | ||
|
||
func formatCollected(cf []graphql.CollectedField) []string { | ||
var res []string | ||
|
||
for _, f := range cf { | ||
res = append(res, fmt.Sprintf("%s as %s", f.Name, f.Alias)) | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package selection | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/vektah/gqlgen/client" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/vektah/gqlgen/handler" | ||
) | ||
|
||
func TestSelection(t *testing.T) { | ||
srv := httptest.NewServer(handler.GraphQL(MakeExecutableSchema(&SelectionResolver{}))) | ||
c := client.New(srv.URL) | ||
|
||
query := `{ | ||
events { | ||
selection | ||
collected | ||
... on Post { | ||
message | ||
sent | ||
} | ||
...LikeFragment | ||
} | ||
} | ||
fragment LikeFragment on Like { reaction sent } | ||
` | ||
|
||
var resp struct { | ||
Events []struct { | ||
Selection []string | ||
Collected []string | ||
|
||
Message string | ||
Reaction string | ||
Sent string | ||
} | ||
} | ||
c.MustPost(query, &resp) | ||
|
||
require.Equal(t, []string{ | ||
"selection as selection", | ||
"collected as collected", | ||
"inline fragment on Post", | ||
"named fragment LikeFragment on Like", | ||
}, resp.Events[0].Selection) | ||
|
||
require.Equal(t, []string{ | ||
"selection as selection", | ||
"collected as collected", | ||
"reaction as reaction", | ||
"sent as sent", | ||
}, resp.Events[0].Collected) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/vektah/gqlgen/example/selection" | ||
"github.com/vektah/gqlgen/handler" | ||
) | ||
|
||
func main() { | ||
http.Handle("/", handler.Playground("Selection Demo", "/query")) | ||
http.Handle("/query", handler.GraphQL(selection.MakeExecutableSchema(&selection.SelectionResolver{}))) | ||
log.Fatal(http.ListenAndServe(":8086", nil)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters