Moved gql types to functions

graph-rework
noah metz 2023-06-07 22:30:06 -06:00
parent a42104883c
commit 592bf387f4
1 changed files with 107 additions and 52 deletions

@ -283,18 +283,35 @@ func (server * GQLServer) update(signal GraphSignal) {
server.BaseResource.update(signal) server.BaseResource.update(signal)
} }
func GQLEventFn(p graphql.ResolveParams, fn func(Event, graphql.ResolveParams)(interface{}, error))(interface{}, error) {
if event, ok := p.Source.(Event); ok {
return fn(event, p)
}
return nil, errors.New("Failed to cast source to event")
}
func GQLEventID(p graphql.ResolveParams) (interface{}, error) { func GQLEventID(p graphql.ResolveParams) (interface{}, error) {
if event, ok := p.Source.(Event); ok { return GQLEventFn(p, func(event Event, p graphql.ResolveParams)(interface{}, error) {
return event.ID(), nil return event.ID(), nil
} })
return nil, errors.New("Failed to cast source to event") }
func GQLEventName(p graphql.ResolveParams) (interface{}, error) {
return GQLEventFn(p, func(event Event, p graphql.ResolveParams)(interface{}, error) {
return event.Name(), nil
})
}
func GQLEventDescription(p graphql.ResolveParams) (interface{}, error) {
return GQLEventFn(p, func(event Event, p graphql.ResolveParams)(interface{}, error) {
return event.Description(), nil
})
} }
func GQLEventChildren(p graphql.ResolveParams) (interface{}, error) { func GQLEventChildren(p graphql.ResolveParams) (interface{}, error) {
if event, ok := p.Source.(Event); ok { return GQLEventFn(p, func(event Event, p graphql.ResolveParams)(interface{}, error) {
return event.Children(), nil return event.Children(), nil
} })
return nil, errors.New("Failed to cast source to event")
} }
var gql_list_event * graphql.List = nil var gql_list_event * graphql.List = nil
@ -333,6 +350,14 @@ func GQLInterfaceEvent() * graphql.Interface {
Type: graphql.String, Type: graphql.String,
}) })
gql_interface_event.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
})
gql_interface_event.AddFieldConfig("Description", &graphql.Field{
Type: graphql.String,
})
gql_interface_event.AddFieldConfig("Children", &graphql.Field{ gql_interface_event.AddFieldConfig("Children", &graphql.Field{
Type: gql_list_event, Type: gql_list_event,
}) })
@ -341,57 +366,89 @@ func GQLInterfaceEvent() * graphql.Interface {
return gql_interface_event return gql_interface_event
} }
func (server * GQLServer) Handler() func(http.ResponseWriter, *http.Request) { var gql_type_base_event * graphql.Object = nil
valid_events := map[reflect.Type]*graphql.Object{} func GQLTypeBaseEvent() * graphql.Object {
if gql_type_base_event == nil {
gql_type_base_event = graphql.NewObject(graphql.ObjectConfig{
Name: "BaseEvent",
Interfaces: []*graphql.Interface{
GQLInterfaceEvent(),
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*BaseEvent)
return ok
},
Fields: graphql.Fields{},
})
gql_type_base_event.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventID,
})
gql_type_base_event := graphql.NewObject(graphql.ObjectConfig{ gql_type_base_event.AddFieldConfig("Name", &graphql.Field{
Name: "BaseEvent", Type: graphql.String,
Interfaces: []*graphql.Interface{ Resolve: GQLEventName,
GQLInterfaceEvent(), })
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*BaseEvent)
return ok
},
Fields: graphql.Fields{},
})
valid_events[reflect.TypeOf((*BaseEvent)(nil))] = gql_type_base_event
gql_type_base_event.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventID,
})
gql_type_base_event.AddFieldConfig("Children", &graphql.Field{
Type: GQLListEvent(),
Resolve: GQLEventChildren,
})
gql_type_event_queue := graphql.NewObject(graphql.ObjectConfig{ gql_type_base_event.AddFieldConfig("Description", &graphql.Field{
Name: "EventQueue", Type: graphql.String,
Interfaces: []*graphql.Interface{ Resolve: GQLEventDescription,
GQLInterfaceEvent(), })
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool { gql_type_base_event.AddFieldConfig("Children", &graphql.Field{
_, ok := p.Value.(*EventQueue) Type: GQLListEvent(),
return ok Resolve: GQLEventChildren,
}, })
Fields: graphql.Fields{}, }
})
valid_events[reflect.TypeOf((*EventQueue)(nil))] = gql_type_event_queue return gql_type_base_event
gql_type_event_queue.AddFieldConfig("ID", &graphql.Field{ }
Type: graphql.String,
Resolve: GQLEventID, var gql_type_event_queue * graphql.Object = nil
}) func GQLTypeEventQueue() * graphql.Object {
gql_type_event_queue.AddFieldConfig("Children", &graphql.Field{ if gql_type_event_queue == nil {
Type: GQLListEvent(), gql_type_event_queue = graphql.NewObject(graphql.ObjectConfig{
Resolve: GQLEventChildren, Name: "EventQueue",
}) Interfaces: []*graphql.Interface{
GQLInterfaceEvent(),
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*EventQueue)
return ok
},
Fields: graphql.Fields{},
})
gql_type_event_queue.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventID,
})
gql_type_event_queue.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventName,
})
gql_type_event_queue.AddFieldConfig("Description", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventDescription,
})
gql_type_event_queue.AddFieldConfig("Children", &graphql.Field{
Type: GQLListEvent(),
Resolve: GQLEventChildren,
})
}
return gql_type_event_queue
}
func (server * GQLServer) Handler() func(http.ResponseWriter, *http.Request) {
valid_events := map[reflect.Type]*graphql.Object{}
valid_events[reflect.TypeOf((*BaseEvent)(nil))] = GQLTypeBaseEvent()
valid_events[reflect.TypeOf((*EventQueue)(nil))] = GQLTypeEventQueue()
schemaConfig := graphql.SchemaConfig{ schemaConfig := graphql.SchemaConfig{
Types: []graphql.Type{gql_type_base_event, gql_type_event_queue}, Types: []graphql.Type{GQLTypeBaseEvent(), GQLTypeEventQueue()},
Query: graphql.NewObject(graphql.ObjectConfig{ Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query", Name: "Query",
Fields: graphql.Fields{ Fields: graphql.Fields{
"Event": &graphql.Field{ "Owner": &graphql.Field{
Type: GQLInterfaceEvent(), Type: GQLInterfaceEvent(),
Resolve: func(p graphql.ResolveParams) (interface{}, error) { Resolve: func(p graphql.ResolveParams) (interface{}, error) {
server.lock_holder_lock.Lock() server.lock_holder_lock.Lock()
@ -505,9 +562,7 @@ func GQLHandler(schema graphql.Schema, ctx context.Context) func(http.ResponseWr
return return
} }
res := GQLQuery{} res := GQLQuery{}
log.Logf("gql", "GQL_REQ: %s", str)
json.Unmarshal(str, &res) json.Unmarshal(str, &res)
log.Logf("gql", "GQL_QUERY: %s", res.Query)
result := graphql.Do(graphql.Params{ result := graphql.Do(graphql.Params{
Schema: schema, Schema: schema,
Context: ctx, Context: ctx,