diff --git a/gql.go b/gql.go index 4051dd9..157daf3 100644 --- a/gql.go +++ b/gql.go @@ -277,14 +277,16 @@ type GQLServer struct { abort chan error listen string gql_channel chan error + extended_types map[reflect.Type]*graphql.Object } -func NewGQLServer(listen string) * GQLServer { +func NewGQLServer(listen string, extended_types map[reflect.Type]*graphql.Object) * GQLServer { server := &GQLServer{ BaseResource: NewBaseResource("GQL Server", "graphql server for event signals", []Resource{}), listen: listen, abort: make(chan error, 1), gql_channel: make(chan error, 1), + extended_types: extended_types, } return server @@ -295,17 +297,19 @@ func (server * GQLServer) update(signal GraphSignal) { server.BaseResource.update(signal) } -func (server * GQLServer) Handler(extended_types map[reflect.Type]*graphql.Object) func(http.ResponseWriter, *http.Request) { +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() - for go_t, gql_t := range(extended_types) { + gql_types := []graphql.Type{GQLTypeBaseEvent(), GQLTypeEventQueue()} + for go_t, gql_t := range(server.extended_types) { valid_events[go_t] = gql_t + gql_types = append(gql_types, gql_t) } schemaConfig := graphql.SchemaConfig{ - Types: []graphql.Type{GQLTypeBaseEvent(), GQLTypeEventQueue()}, + Types: gql_types, Query: graphql.NewObject(graphql.ObjectConfig{ Name: "Query", Fields: graphql.Fields{ @@ -337,7 +341,7 @@ func (server * GQLServer) Init(abort chan error) bool { log.Logf("gql", "GOROUTINE_START for %s", server.ID()) mux := http.NewServeMux() - mux.HandleFunc("/gql", server.Handler(map[reflect.Type]*graphql.Object{})) + mux.HandleFunc("/gql", server.Handler()) mux.HandleFunc("/", GraphiQLHandler()) srv := &http.Server{ diff --git a/gql_vex.go b/gql_vex.go new file mode 100644 index 0000000..2eae8c9 --- /dev/null +++ b/gql_vex.go @@ -0,0 +1,52 @@ +package main + +import ( + "github.com/graphql-go/graphql" + "reflect" +) + +func GQLVexTypes() map[reflect.Type]*graphql.Object { + types := map[reflect.Type]*graphql.Object{} + types[reflect.TypeOf((*Match)(nil))] = GQLVexTypeMatch() + + return types +} + +var gql_vex_type_match * graphql.Object = nil +func GQLVexTypeMatch() * graphql.Object { + if gql_vex_type_match == nil { + gql_vex_type_match = graphql.NewObject(graphql.ObjectConfig{ + Name: "Match", + Interfaces: []*graphql.Interface{ + GQLInterfaceEvent(), + }, + IsTypeOf: func(p graphql.IsTypeOfParams) bool { + _, ok := p.Value.(*Match) + return ok + }, + Fields: graphql.Fields{}, + }) + + gql_vex_type_match.AddFieldConfig("ID", &graphql.Field{ + Type: graphql.String, + Resolve: GQLEventID, + }) + + gql_vex_type_match.AddFieldConfig("Name", &graphql.Field{ + Type: graphql.String, + Resolve: GQLEventName, + }) + + gql_vex_type_match.AddFieldConfig("Description", &graphql.Field{ + Type: graphql.String, + Resolve: GQLEventDescription, + }) + + gql_vex_type_match.AddFieldConfig("Children", &graphql.Field{ + Type: GQLListEvent(), + Resolve: GQLEventChildren, + }) + } + + return gql_vex_type_match +} diff --git a/main.go b/main.go index 0a97761..cad4d9d 100644 --- a/main.go +++ b/main.go @@ -116,7 +116,7 @@ func fake_data() (* EventManager, []Arena, []Arena) { } - gql_server := NewGQLServer(":8080") + gql_server := NewGQLServer(":8080", GQLVexTypes()) resources = append(resources, gql_server) root_event := NewEventQueue("root_event", "", []Resource{gql_server}) event_manager := NewEventManager(root_event, resources)