From 4d26ed73dda13a34a241756257919239617d2585 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 13 Jun 2023 15:29:23 -0600 Subject: [PATCH] Updated GQL to have node interface and type, and resource to have owner property that returns this --- gql.go | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gql_vex.go | 19 ++++++++++ 2 files changed, 124 insertions(+) diff --git a/gql.go b/gql.go index 7144170..38f281e 100644 --- a/gql.go +++ b/gql.go @@ -92,6 +92,90 @@ func GraphiQLHandler() func(http.ResponseWriter, *http.Request) { } +var gql_type_base_node *graphql.Object = nil +func GQLTypeBaseNode() *graphql.Object { + if gql_type_base_node == nil { + gql_type_base_node = graphql.NewObject(graphql.ObjectConfig{ + Name: "BaseNode", + Interfaces: []*graphql.Interface{ + GQLInterfaceNode(), + }, + IsTypeOf: func(p graphql.IsTypeOfParams) bool { + _, ok := p.Value.(*BaseNode) + return ok + }, + Fields: graphql.Fields{}, + }) + + gql_type_base_node.AddFieldConfig("ID", &graphql.Field{ + Type: graphql.String, + Resolve: GQLResourceID, + }) + + gql_type_base_node.AddFieldConfig("Name", &graphql.Field{ + Type: graphql.String, + Resolve: GQLResourceName, + }) + + gql_type_base_node.AddFieldConfig("Description", &graphql.Field{ + Type: graphql.String, + Resolve: GQLResourceDescription, + }) + } + + return gql_type_base_node +} + +var gql_interface_node *graphql.Interface = nil +func GQLInterfaceNode() *graphql.Interface { + if gql_interface_node == nil { + gql_interface_node = graphql.NewInterface(graphql.InterfaceConfig{ + Name: "Node", + ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object { + valid_events, ok := p.Context.Value("valid_events").(map[reflect.Type]*graphql.Object) + if ok == false { + return nil + } + + valid_resources, ok := p.Context.Value("valid_resources").(map[reflect.Type]*graphql.Object) + if ok == false { + return nil + } + + for key, value := range(valid_events) { + if reflect.TypeOf(p.Value) == key { + return value + } + } + + for key, value := range(valid_resources) { + if reflect.TypeOf(p.Value) == key { + return value + } + } + + return GQLTypeBaseNode() + }, + Fields: graphql.Fields{}, + }) + + gql_interface_node.AddFieldConfig("ID", &graphql.Field{ + Type: graphql.String, + }) + + gql_interface_node.AddFieldConfig("Name", &graphql.Field{ + Type: graphql.String, + }) + + gql_interface_node.AddFieldConfig("Description", &graphql.Field{ + Type: graphql.String, + }) + + } + + return gql_interface_node +} + type GQLQuery struct { Query string `json:"query"` OperationName string `json:"operationName"` @@ -175,6 +259,9 @@ func GQLInterfaceResource() * graphql.Interface { gql_interface_resource = graphql.NewInterface(graphql.InterfaceConfig{ Name: "Resource", ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object { + if p.Value == nil { + return GQLTypeBaseResource() + } valid_resources, ok := p.Context.Value("valid_resources").(map[reflect.Type]*graphql.Object) if ok == false { return nil @@ -209,6 +296,10 @@ func GQLInterfaceResource() * graphql.Interface { Type: GQLListResource(), }) + gql_interface_resource.AddFieldConfig("Owner", &graphql.Field{ + Type: GQLInterfaceNode(), + }) + } return gql_interface_resource @@ -245,6 +336,12 @@ func GQLResourceParents(p graphql.ResolveParams) (interface{}, error) { }) } +func GQLResourceOwner(p graphql.ResolveParams) (interface{}, error) { + return GQLResourceFn(p, func(resource Resource, p graphql.ResolveParams) (interface{}, error) { + return resource.Owner(), nil + }) +} + var gql_type_base_resource *graphql.Object = nil func GQLTypeBaseResource() * graphql.Object { if gql_type_base_resource == nil { @@ -252,6 +349,7 @@ func GQLTypeBaseResource() * graphql.Object { Name: "BaseResource", Interfaces: []*graphql.Interface{ GQLInterfaceResource(), + GQLInterfaceNode(), }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { _, ok := p.Value.(*BaseResource) @@ -279,6 +377,11 @@ func GQLTypeBaseResource() * graphql.Object { Type: GQLListResource(), Resolve: GQLResourceParents, }) + + gql_type_base_resource.AddFieldConfig("Owner", &graphql.Field{ + Type: GQLInterfaceNode(), + Resolve: GQLResourceOwner, + }) } return gql_type_base_resource @@ -343,6 +446,7 @@ func GQLTypeBaseEvent() * graphql.Object { Name: "BaseEvent", Interfaces: []*graphql.Interface{ GQLInterfaceEvent(), + GQLInterfaceNode(), }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { _, ok := p.Value.(*BaseEvent) @@ -381,6 +485,7 @@ func GQLTypeEventQueue() * graphql.Object { Name: "EventQueue", Interfaces: []*graphql.Interface{ GQLInterfaceEvent(), + GQLInterfaceNode(), }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { _, ok := p.Value.(*EventQueue) diff --git a/gql_vex.go b/gql_vex.go index 1f63cb7..f316f59 100644 --- a/gql_vex.go +++ b/gql_vex.go @@ -130,6 +130,7 @@ func GQLVexTypeArena() * graphql.Object { gql_vex_type_arena = graphql.NewObject(graphql.ObjectConfig{ Name: "Arena", Interfaces: []*graphql.Interface{ + GQLInterfaceNode(), GQLInterfaceResource(), }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { @@ -158,6 +159,11 @@ func GQLVexTypeArena() * graphql.Object { Type: GQLListResource(), Resolve: GQLResourceParents, }) + + gql_vex_type_arena.AddFieldConfig("Owner", &graphql.Field{ + Type: GQLInterfaceNode(), + Resolve: GQLResourceOwner, + }) } return gql_vex_type_arena @@ -169,6 +175,7 @@ func GQLVexTypeMatch() * graphql.Object { gql_vex_type_match = graphql.NewObject(graphql.ObjectConfig{ Name: "Match", Interfaces: []*graphql.Interface{ + GQLInterfaceNode(), GQLInterfaceEvent(), }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { @@ -228,6 +235,7 @@ func GQLVexTypeAlliance() * graphql.Object { gql_vex_type_alliance = graphql.NewObject(graphql.ObjectConfig{ Name: "Alliance", Interfaces: []*graphql.Interface{ + GQLInterfaceNode(), GQLInterfaceResource(), }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { @@ -257,6 +265,11 @@ func GQLVexTypeAlliance() * graphql.Object { Resolve: GQLResourceParents, }) + gql_vex_type_alliance.AddFieldConfig("Owner", &graphql.Field{ + Type: GQLInterfaceNode(), + Resolve: GQLResourceOwner, + }) + gql_vex_type_alliance.AddFieldConfig("Teams", &graphql.Field{ Type: GQLVexListTeam(), Resolve: GQLVexAllianceTeams, @@ -272,6 +285,7 @@ func GQLVexTypeTeam() * graphql.Object { gql_vex_type_team = graphql.NewObject(graphql.ObjectConfig{ Name: "Team", Interfaces: []*graphql.Interface{ + GQLInterfaceNode(), GQLInterfaceResource(), }, IsTypeOf: func(p graphql.IsTypeOfParams) bool { @@ -302,5 +316,10 @@ func GQLVexTypeTeam() * graphql.Object { Resolve: GQLResourceParents, }) + gql_vex_type_team.AddFieldConfig("Owner", &graphql.Field{ + Type: GQLInterfaceNode(), + Resolve: GQLResourceOwner, + }) + return gql_vex_type_team }