From a88c704c57462d057ce46905820a87b71eb6d90d Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Thu, 20 Jul 2023 22:17:45 -0600 Subject: [PATCH] Added Users query to GQLThread --- gql_graph.go | 42 ++++++++++++++++++++++++++++++++++++++++++ gql_test.go | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/gql_graph.go b/gql_graph.go index f4dbf47..0e2c86e 100644 --- a/gql_graph.go +++ b/gql_graph.go @@ -385,6 +385,43 @@ func GQLLockableOwner(p graphql.ResolveParams) (interface{}, error) { return owner, nil } +func GQLThreadUsers(p graphql.ResolveParams) (interface{}, error) { + node, ok := p.Source.(*GQLThread) + if ok == false || node == nil { + return nil, fmt.Errorf("Failed to cast source to GQLThread") + } + + ctx, ok := p.Context.Value("graph_context").(*Context) + if ok == false { + return nil, fmt.Errorf("Failed to cast context graph_context to Context") + } + + var users []*User + err := UseStates(ctx, []Node{node}, func(nodes NodeMap) error { + users = make([]*User, len(node.Users)) + i := 0 + for _, user := range(node.Users) { + users[i] = user + i += 1 + } + return nil + }) + + if err != nil { + return nil, err + } + + return users, nil +} + +var gql_list_user *graphql.List = nil +func GQLListUser() *graphql.List { + if gql_list_user == nil { + gql_list_user = graphql.NewList(GQLTypeUser()) + } + return gql_list_user +} + var gql_type_user *graphql.Object = nil func GQLTypeUser() * graphql.Object { if gql_type_user == nil { @@ -501,6 +538,11 @@ func GQLTypeGQLThread() * graphql.Object { Type: GQLListLockable(), Resolve: GQLLockableDependencies, }) + + gql_type_gql_thread.AddFieldConfig("Users", &graphql.Field{ + Type: GQLListUser(), + Resolve: GQLThreadUsers, + }) } return gql_type_gql_thread } diff --git a/gql_test.go b/gql_test.go index c13bd3a..95a0078 100644 --- a/gql_test.go +++ b/gql_test.go @@ -209,7 +209,7 @@ func TestGQLAuth(t * testing.T) { url = fmt.Sprintf("http://localhost:%d/gql", port) ser, err := json.MarshalIndent(&GQLPayload{ - Query: "query { Self { ID } }", + Query: "query { Self { Users { ID } } }", }, "", " ") fatalErr(t, err)