graphql: use an interface instead of an union for the operations for easier query

Michael Muré created

Change summary

graphql/graph/gen_graph.go   | 40 +------------------------------------
graphql/models/gen_models.go |  5 +--
graphql/resolvers/bug.go     |  2 
graphql/schema.graphql       |  9 -------
4 files changed, 6 insertions(+), 50 deletions(-)

Detailed changes

graphql/graph/gen_graph.go 🔗

@@ -1060,7 +1060,7 @@ func (ec *executionContext) _OperationEdge_node(ctx context.Context, field graph
 	rctx.PushField(field.Alias)
 	defer rctx.Pop()
 	res := obj.Node
-	return ec._OperationUnion(ctx, field.Selections, &res)
+	return ec._Operation(ctx, field.Selections, &res)
 }
 
 var pageInfoImplementors = []string{"PageInfo"}
@@ -2345,35 +2345,6 @@ func (ec *executionContext) _Operation(ctx context.Context, sel []query.Selectio
 	}
 }
 
-func (ec *executionContext) _OperationUnion(ctx context.Context, sel []query.Selection, obj *models.OperationUnion) graphql.Marshaler {
-	switch obj := (*obj).(type) {
-	case nil:
-		return graphql.Null
-	case operations.CreateOperation:
-		return ec._CreateOperation(ctx, sel, &obj)
-	case *operations.CreateOperation:
-		return ec._CreateOperation(ctx, sel, obj)
-	case operations.SetTitleOperation:
-		return ec._SetTitleOperation(ctx, sel, &obj)
-	case *operations.SetTitleOperation:
-		return ec._SetTitleOperation(ctx, sel, obj)
-	case operations.AddCommentOperation:
-		return ec._AddCommentOperation(ctx, sel, &obj)
-	case *operations.AddCommentOperation:
-		return ec._AddCommentOperation(ctx, sel, obj)
-	case operations.SetStatusOperation:
-		return ec._SetStatusOperation(ctx, sel, &obj)
-	case *operations.SetStatusOperation:
-		return ec._SetStatusOperation(ctx, sel, obj)
-	case operations.LabelChangeOperation:
-		return ec._LabelChangeOperation(ctx, sel, &obj)
-	case *operations.LabelChangeOperation:
-		return ec._LabelChangeOperation(ctx, sel, obj)
-	default:
-		panic(fmt.Errorf("unexpected type %T", obj))
-	}
-}
-
 func UnmarshalConnectionInput(v interface{}) (models.ConnectionInput, error) {
 	var it models.ConnectionInput
 	var asMap = v.(map[string]interface{})
@@ -2523,7 +2494,7 @@ type OperationConnection {
 
 type OperationEdge {
   cursor: String!
-  node: OperationUnion!
+  node: Operation!
 }
 
 # An operation applied to a bug.
@@ -2572,13 +2543,6 @@ type LabelChangeOperation implements Operation, Authored {
   removed: [Label!]!
 }
 
-union OperationUnion =
-    CreateOperation
-  | SetTitleOperation
-  | AddCommentOperation
-  | SetStatusOperation
-  | LabelChangeOperation
-
 # The connection type for Bug.
 type BugConnection {
   # A list of edges.

graphql/models/gen_models.go 🔗

@@ -42,10 +42,9 @@ type OperationConnection struct {
 	TotalCount int             `json:"totalCount"`
 }
 type OperationEdge struct {
-	Cursor string         `json:"cursor"`
-	Node   OperationUnion `json:"node"`
+	Cursor string    `json:"cursor"`
+	Node   Operation `json:"node"`
 }
-type OperationUnion interface{}
 type PageInfo struct {
 	HasNextPage     bool `json:"hasNextPage"`
 	HasPreviousPage bool `json:"hasPreviousPage"`

graphql/resolvers/bug.go 🔗

@@ -35,7 +35,7 @@ func (bugResolver) Comments(ctx context.Context, obj *bug.Snapshot, input models
 func (bugResolver) Operations(ctx context.Context, obj *bug.Snapshot, input models.ConnectionInput) (models.OperationConnection, error) {
 	edger := func(op bug.Operation, offset int) connections.Edge {
 		return models.OperationEdge{
-			Node:   op.(models.OperationUnion),
+			Node:   op.(models.Operation),
 			Cursor: connections.OffsetToCursor(offset),
 		}
 	}

graphql/schema.graphql 🔗

@@ -79,7 +79,7 @@ type OperationConnection {
 
 type OperationEdge {
   cursor: String!
-  node: OperationUnion!
+  node: Operation!
 }
 
 # An operation applied to a bug.
@@ -128,13 +128,6 @@ type LabelChangeOperation implements Operation, Authored {
   removed: [Label!]!
 }
 
-union OperationUnion =
-    CreateOperation
-  | SetTitleOperation
-  | AddCommentOperation
-  | SetStatusOperation
-  | LabelChangeOperation
-
 # The connection type for Bug.
 type BugConnection {
   # A list of edges.