Updated GQL to have node interface and type, and resource to have owner property that returns this

graph-rework
noah metz 2023-06-13 15:29:23 -06:00
parent 4a6edade74
commit 4d26ed73dd
2 changed files with 124 additions and 0 deletions

105
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 { type GQLQuery struct {
Query string `json:"query"` Query string `json:"query"`
OperationName string `json:"operationName"` OperationName string `json:"operationName"`
@ -175,6 +259,9 @@ func GQLInterfaceResource() * graphql.Interface {
gql_interface_resource = graphql.NewInterface(graphql.InterfaceConfig{ gql_interface_resource = graphql.NewInterface(graphql.InterfaceConfig{
Name: "Resource", Name: "Resource",
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object { 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) valid_resources, ok := p.Context.Value("valid_resources").(map[reflect.Type]*graphql.Object)
if ok == false { if ok == false {
return nil return nil
@ -209,6 +296,10 @@ func GQLInterfaceResource() * graphql.Interface {
Type: GQLListResource(), Type: GQLListResource(),
}) })
gql_interface_resource.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceNode(),
})
} }
return gql_interface_resource 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 var gql_type_base_resource *graphql.Object = nil
func GQLTypeBaseResource() * graphql.Object { func GQLTypeBaseResource() * graphql.Object {
if gql_type_base_resource == nil { if gql_type_base_resource == nil {
@ -252,6 +349,7 @@ func GQLTypeBaseResource() * graphql.Object {
Name: "BaseResource", Name: "BaseResource",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceResource(), GQLInterfaceResource(),
GQLInterfaceNode(),
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*BaseResource) _, ok := p.Value.(*BaseResource)
@ -279,6 +377,11 @@ func GQLTypeBaseResource() * graphql.Object {
Type: GQLListResource(), Type: GQLListResource(),
Resolve: GQLResourceParents, Resolve: GQLResourceParents,
}) })
gql_type_base_resource.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceNode(),
Resolve: GQLResourceOwner,
})
} }
return gql_type_base_resource return gql_type_base_resource
@ -343,6 +446,7 @@ func GQLTypeBaseEvent() * graphql.Object {
Name: "BaseEvent", Name: "BaseEvent",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceEvent(), GQLInterfaceEvent(),
GQLInterfaceNode(),
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*BaseEvent) _, ok := p.Value.(*BaseEvent)
@ -381,6 +485,7 @@ func GQLTypeEventQueue() * graphql.Object {
Name: "EventQueue", Name: "EventQueue",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceEvent(), GQLInterfaceEvent(),
GQLInterfaceNode(),
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*EventQueue) _, ok := p.Value.(*EventQueue)

@ -130,6 +130,7 @@ func GQLVexTypeArena() * graphql.Object {
gql_vex_type_arena = graphql.NewObject(graphql.ObjectConfig{ gql_vex_type_arena = graphql.NewObject(graphql.ObjectConfig{
Name: "Arena", Name: "Arena",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceNode(),
GQLInterfaceResource(), GQLInterfaceResource(),
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
@ -158,6 +159,11 @@ func GQLVexTypeArena() * graphql.Object {
Type: GQLListResource(), Type: GQLListResource(),
Resolve: GQLResourceParents, Resolve: GQLResourceParents,
}) })
gql_vex_type_arena.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceNode(),
Resolve: GQLResourceOwner,
})
} }
return gql_vex_type_arena return gql_vex_type_arena
@ -169,6 +175,7 @@ func GQLVexTypeMatch() * graphql.Object {
gql_vex_type_match = graphql.NewObject(graphql.ObjectConfig{ gql_vex_type_match = graphql.NewObject(graphql.ObjectConfig{
Name: "Match", Name: "Match",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceNode(),
GQLInterfaceEvent(), GQLInterfaceEvent(),
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
@ -228,6 +235,7 @@ func GQLVexTypeAlliance() * graphql.Object {
gql_vex_type_alliance = graphql.NewObject(graphql.ObjectConfig{ gql_vex_type_alliance = graphql.NewObject(graphql.ObjectConfig{
Name: "Alliance", Name: "Alliance",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceNode(),
GQLInterfaceResource(), GQLInterfaceResource(),
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
@ -257,6 +265,11 @@ func GQLVexTypeAlliance() * graphql.Object {
Resolve: GQLResourceParents, Resolve: GQLResourceParents,
}) })
gql_vex_type_alliance.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceNode(),
Resolve: GQLResourceOwner,
})
gql_vex_type_alliance.AddFieldConfig("Teams", &graphql.Field{ gql_vex_type_alliance.AddFieldConfig("Teams", &graphql.Field{
Type: GQLVexListTeam(), Type: GQLVexListTeam(),
Resolve: GQLVexAllianceTeams, Resolve: GQLVexAllianceTeams,
@ -272,6 +285,7 @@ func GQLVexTypeTeam() * graphql.Object {
gql_vex_type_team = graphql.NewObject(graphql.ObjectConfig{ gql_vex_type_team = graphql.NewObject(graphql.ObjectConfig{
Name: "Team", Name: "Team",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceNode(),
GQLInterfaceResource(), GQLInterfaceResource(),
}, },
IsTypeOf: func(p graphql.IsTypeOfParams) bool { IsTypeOf: func(p graphql.IsTypeOfParams) bool {
@ -302,5 +316,10 @@ func GQLVexTypeTeam() * graphql.Object {
Resolve: GQLResourceParents, Resolve: GQLResourceParents,
}) })
gql_vex_type_team.AddFieldConfig("Owner", &graphql.Field{
Type: GQLInterfaceNode(),
Resolve: GQLResourceOwner,
})
return gql_vex_type_team return gql_vex_type_team
} }