git.sr.ht/~sircmpwn/gqlgen@v0.0.0-20200522192042-c84d29a1c940/graphql/context_operation.go (about) 1 package graphql 2 3 import ( 4 "context" 5 "errors" 6 7 "github.com/vektah/gqlparser/v2/ast" 8 ) 9 10 // Deprecated: Please update all references to OperationContext instead 11 type RequestContext = OperationContext 12 13 type OperationContext struct { 14 RawQuery string 15 Variables map[string]interface{} 16 OperationName string 17 Doc *ast.QueryDocument 18 19 Operation *ast.OperationDefinition 20 DisableIntrospection bool 21 Recover RecoverFunc 22 ResolverMiddleware FieldMiddleware 23 24 Stats Stats 25 } 26 27 func (c *OperationContext) Validate(ctx context.Context) error { 28 if c.Doc == nil { 29 return errors.New("field 'Doc'is required") 30 } 31 if c.RawQuery == "" { 32 return errors.New("field 'RawQuery' is required") 33 } 34 if c.Variables == nil { 35 c.Variables = make(map[string]interface{}) 36 } 37 if c.ResolverMiddleware == nil { 38 return errors.New("field 'ResolverMiddleware' is required") 39 } 40 if c.Recover == nil { 41 c.Recover = DefaultRecover 42 } 43 44 return nil 45 } 46 47 const operationCtx key = "operation_context" 48 49 // Deprecated: Please update all references to GetOperationContext instead 50 func GetRequestContext(ctx context.Context) *RequestContext { 51 return GetOperationContext(ctx) 52 } 53 54 func GetOperationContext(ctx context.Context) *OperationContext { 55 if val, ok := ctx.Value(operationCtx).(*OperationContext); ok && val != nil { 56 return val 57 } 58 panic("missing operation context") 59 } 60 61 func WithOperationContext(ctx context.Context, rc *OperationContext) context.Context { 62 return context.WithValue(ctx, operationCtx, rc) 63 } 64 65 // HasOperationContext checks if the given context is part of an ongoing operation 66 // 67 // Some errors can happen outside of an operation, eg json unmarshal errors. 68 func HasOperationContext(ctx context.Context) bool { 69 _, ok := ctx.Value(operationCtx).(*OperationContext) 70 return ok 71 } 72 73 // This is just a convenient wrapper method for CollectFields 74 func CollectFieldsCtx(ctx context.Context, satisfies []string) []CollectedField { 75 resctx := GetFieldContext(ctx) 76 return CollectFields(GetOperationContext(ctx), resctx.Field.Selections, satisfies) 77 } 78 79 // CollectAllFields returns a slice of all GraphQL field names that were selected for the current resolver context. 80 // The slice will contain the unique set of all field names requested regardless of fragment type conditions. 81 func CollectAllFields(ctx context.Context) []string { 82 resctx := GetFieldContext(ctx) 83 collected := CollectFields(GetOperationContext(ctx), resctx.Field.Selections, nil) 84 uniq := make([]string, 0, len(collected)) 85 Next: 86 for _, f := range collected { 87 for _, name := range uniq { 88 if name == f.Name { 89 continue Next 90 } 91 } 92 uniq = append(uniq, f.Name) 93 } 94 return uniq 95 } 96 97 // Errorf sends an error string to the client, passing it through the formatter. 98 // Deprecated: use graphql.AddErrorf(ctx, err) instead 99 func (c *OperationContext) Errorf(ctx context.Context, format string, args ...interface{}) { 100 AddErrorf(ctx, format, args...) 101 } 102 103 // Error sends an error to the client, passing it through the formatter. 104 // Deprecated: use graphql.AddError(ctx, err) instead 105 func (c *OperationContext) Error(ctx context.Context, err error) { 106 AddError(ctx, err) 107 }