From 353b54757d35d3910c0f595126ff8349a41bd2f8 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Fri, 16 Jun 2023 17:42:49 -0600 Subject: [PATCH] Button finally transisitons game over GQL --- gql.go | 21 +++++++++++++++----- test-site/src/routes/+page.svelte | 32 +++++++++++++++++++++++-------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/gql.go b/gql.go index 654a300..9e121c3 100644 --- a/gql.go +++ b/gql.go @@ -197,6 +197,9 @@ type GQLWSMsg struct { func enableCORS(w *http.ResponseWriter) { (*w).Header().Set("Access-Control-Allow-Origin", "*") + (*w).Header().Set("Access-Control-Allow-Credentials", "true") + (*w).Header().Set("Access-Control-Allow-Headers", "*") + (*w).Header().Set("Access-Control-Allow-Methods", "*") } func GQLHandler(schema graphql.Schema, ctx context.Context) func(http.ResponseWriter, *http.Request) { @@ -241,8 +244,10 @@ func GQLHandler(schema graphql.Schema, ctx context.Context) func(http.ResponseWr func sendOneResultAndClose(res *graphql.Result) chan *graphql.Result { resultChannel := make(chan *graphql.Result) - resultChannel <- res - close(resultChannel) + go func() { + resultChannel <- res + close(resultChannel) + }() return resultChannel } @@ -260,7 +265,11 @@ func getOperationTypeOfReq(p graphql.Params) string{ for _, node := range AST.Definitions { if operationDef, ok := node.(*ast.OperationDefinition); ok { - if operationDef.Name.Value == p.OperationName || p.OperationName == "" { + name := "" + if operationDef.Name != nil { + name = operationDef.Name.Value + } + if name == p.OperationName || p.OperationName == "" { return operationDef.Operation } } @@ -270,12 +279,14 @@ func getOperationTypeOfReq(p graphql.Params) string{ func GQLWSDo(p graphql.Params) chan *graphql.Result { operation := getOperationTypeOfReq(p) + log.Logf("gqlws", "GQLWSDO_OPERATION: %s %+v", operation, p.RequestString) if operation == ast.OperationTypeSubscription { return graphql.Subscribe(p) } - return sendOneResultAndClose(graphql.Do(p)) + res := graphql.Do(p) + return sendOneResultAndClose(res) } func GQLWSHandler(schema graphql.Schema, ctx context.Context) func(http.ResponseWriter, *http.Request) { @@ -932,7 +943,7 @@ func MakeGQLHandlers(server * GQLServer) (func(http.ResponseWriter, *http.Reques } gql_subscriptions := graphql.Fields{ - "Update": GQLSubscriptionUpdate(), + "Updates": GQLSubscriptionUpdate(), } for key, value := range(server.extended_subscriptions) { diff --git a/test-site/src/routes/+page.svelte b/test-site/src/routes/+page.svelte index 1dfc8cc..a899aa7 100644 --- a/test-site/src/routes/+page.svelte +++ b/test-site/src/routes/+page.svelte @@ -14,25 +14,41 @@ const client = createClient({ var game_id = null console.log("STARTING_CLIENT") + + +client.subscribe({ + query: "query { Arenas { Name Owner { ... on Match { Name, ID } } } }" + }, + { + next: (data) => { + let obj = JSON.parse(data.data) + game_id = obj.Arenas[0].Owner.ID + console.log(game_id) + }, + error: (err) => { + console.log(err) + }, + complete: () => { + }, +}); + client.subscribe({ - operationName: "Sub", - query: "query GetArenas { Arenas { Name Owner { ... on Match { Name, ID } } } } subscription Sub { Update { String } }", + query: "subscription { Updates { String } }" }, { next: (data) => { - console.log("NEXT") console.log(data) }, error: (err) => { - console.log("ERROR") console.log(err) }, complete: () => { - console.log("COMPLETED") }, }); + + async function match_state(match_id, state) { let url = "http://localhost:8080/gql" let data = { @@ -45,9 +61,9 @@ async function match_state(match_id, state) { } const response = await fetch(url, { method: "POST", - mode: "same-origin", + mode: "cors", cache: "no-cache", - credentials: "include", + credentials: "same-origin", headers: { "Content-Type": "applicaton/json", }, @@ -60,4 +76,4 @@ async function match_state(match_id, state) { - +