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 GQLEventID(p graphql.ResolveParams) (interface{}, error) { func GQLEventFn(p graphql.ResolveParams, fn func(Event, graphql.ResolveParams)(interface{}, error))(interface{}, error) {
if event, ok := p.Source.(Event); ok { if event, ok := p.Source.(Event); ok {
return event.ID(), nil return fn(event, p)
} }
return nil, errors.New("Failed to cast source to event") return nil, errors.New("Failed to cast source to event")
} }
func GQLEventID(p graphql.ResolveParams) (interface{}, error) {
return GQLEventFn(p, func(event Event, p graphql.ResolveParams)(interface{}, error) {
return event.ID(), nil
})
}
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,10 +366,10 @@ 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{ gql_type_base_event = graphql.NewObject(graphql.ObjectConfig{
Name: "BaseEvent", Name: "BaseEvent",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceEvent(), GQLInterfaceEvent(),
@ -355,17 +380,34 @@ func (server * GQLServer) Handler() func(http.ResponseWriter, *http.Request) {
}, },
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })
valid_events[reflect.TypeOf((*BaseEvent)(nil))] = gql_type_base_event
gql_type_base_event.AddFieldConfig("ID", &graphql.Field{ gql_type_base_event.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String, Type: graphql.String,
Resolve: GQLEventID, Resolve: GQLEventID,
}) })
gql_type_base_event.AddFieldConfig("Name", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventName,
})
gql_type_base_event.AddFieldConfig("Description", &graphql.Field{
Type: graphql.String,
Resolve: GQLEventDescription,
})
gql_type_base_event.AddFieldConfig("Children", &graphql.Field{ gql_type_base_event.AddFieldConfig("Children", &graphql.Field{
Type: GQLListEvent(), Type: GQLListEvent(),
Resolve: GQLEventChildren, Resolve: GQLEventChildren,
}) })
}
gql_type_event_queue := graphql.NewObject(graphql.ObjectConfig{ return gql_type_base_event
}
var gql_type_event_queue * graphql.Object = nil
func GQLTypeEventQueue() * graphql.Object {
if gql_type_event_queue == nil {
gql_type_event_queue = graphql.NewObject(graphql.ObjectConfig{
Name: "EventQueue", Name: "EventQueue",
Interfaces: []*graphql.Interface{ Interfaces: []*graphql.Interface{
GQLInterfaceEvent(), GQLInterfaceEvent(),
@ -376,22 +418,37 @@ func (server * GQLServer) Handler() func(http.ResponseWriter, *http.Request) {
}, },
Fields: graphql.Fields{}, Fields: graphql.Fields{},
}) })
valid_events[reflect.TypeOf((*EventQueue)(nil))] = gql_type_event_queue
gql_type_event_queue.AddFieldConfig("ID", &graphql.Field{ gql_type_event_queue.AddFieldConfig("ID", &graphql.Field{
Type: graphql.String, Type: graphql.String,
Resolve: GQLEventID, 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{ gql_type_event_queue.AddFieldConfig("Children", &graphql.Field{
Type: GQLListEvent(), Type: GQLListEvent(),
Resolve: GQLEventChildren, 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,