Button finally transisitons game over GQL

graph-rework
noah metz 2023-06-16 17:42:49 -06:00
parent 76cea43d9b
commit 353b54757d
2 changed files with 40 additions and 13 deletions

@ -197,6 +197,9 @@ type GQLWSMsg struct {
func enableCORS(w *http.ResponseWriter) { func enableCORS(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*") (*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) { 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 { func sendOneResultAndClose(res *graphql.Result) chan *graphql.Result {
resultChannel := make(chan *graphql.Result) resultChannel := make(chan *graphql.Result)
go func() {
resultChannel <- res resultChannel <- res
close(resultChannel) close(resultChannel)
}()
return resultChannel return resultChannel
} }
@ -260,7 +265,11 @@ func getOperationTypeOfReq(p graphql.Params) string{
for _, node := range AST.Definitions { for _, node := range AST.Definitions {
if operationDef, ok := node.(*ast.OperationDefinition); ok { 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 return operationDef.Operation
} }
} }
@ -270,12 +279,14 @@ func getOperationTypeOfReq(p graphql.Params) string{
func GQLWSDo(p graphql.Params) chan *graphql.Result { func GQLWSDo(p graphql.Params) chan *graphql.Result {
operation := getOperationTypeOfReq(p) operation := getOperationTypeOfReq(p)
log.Logf("gqlws", "GQLWSDO_OPERATION: %s %+v", operation, p.RequestString)
if operation == ast.OperationTypeSubscription { if operation == ast.OperationTypeSubscription {
return graphql.Subscribe(p) 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) { 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{ gql_subscriptions := graphql.Fields{
"Update": GQLSubscriptionUpdate(), "Updates": GQLSubscriptionUpdate(),
} }
for key, value := range(server.extended_subscriptions) { for key, value := range(server.extended_subscriptions) {

@ -14,25 +14,41 @@ const client = createClient({
var game_id = null var game_id = null
console.log("STARTING_CLIENT") 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({ client.subscribe({
operationName: "Sub", query: "subscription { Updates { String } }"
query: "query GetArenas { Arenas { Name Owner { ... on Match { Name, ID } } } } subscription Sub { Update { String } }",
}, },
{ {
next: (data) => { next: (data) => {
console.log("NEXT")
console.log(data) console.log(data)
}, },
error: (err) => { error: (err) => {
console.log("ERROR")
console.log(err) console.log(err)
}, },
complete: () => { complete: () => {
console.log("COMPLETED")
}, },
}); });
async function match_state(match_id, state) { async function match_state(match_id, state) {
let url = "http://localhost:8080/gql" let url = "http://localhost:8080/gql"
let data = { let data = {
@ -45,9 +61,9 @@ async function match_state(match_id, state) {
} }
const response = await fetch(url, { const response = await fetch(url, {
method: "POST", method: "POST",
mode: "same-origin", mode: "cors",
cache: "no-cache", cache: "no-cache",
credentials: "include", credentials: "same-origin",
headers: { headers: {
"Content-Type": "applicaton/json", "Content-Type": "applicaton/json",
}, },
@ -60,4 +76,4 @@ async function match_state(match_id, state) {
</script> </script>
<Button on:click={()=>match_state("eafd0201-caa4-4b35-a99b-869aac9455fa", "queue_autonomous")}>Queue Autonomous</Button> <Button on:click={()=>match_state(game_id, "queue_autonomous")}>Queue Autonomous</Button>