Documentation
¶
Overview ¶
Package graphapi provides a GraphQL schema generator using reflection.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Any = graphql.NewScalar(graphql.ScalarConfig{ Name: "Any", Serialize: func(value interface{}) interface{} { return value }, ParseValue: func(value interface{}) interface{} { return value }, ParseLiteral: func(value ast.Value) interface{} { return value.GetValue() }, })
var Data = graphql.NewScalar(graphql.ScalarConfig{ Name: "Data", Serialize: func(value interface{}) interface{} { return base64.StdEncoding.EncodeToString(value.([]byte)) }, ParseValue: func(value interface{}) interface{} { raw, err := base64.StdEncoding.DecodeString(value.(string)) if err != nil { log.Errorf("graphapi: failed to decode base64: %w", err) return nil } return raw }, ParseLiteral: func(value ast.Value) interface{} { return nil }, })
var Int64 = graphql.NewScalar(graphql.ScalarConfig{ Name: "Int64", Description: "A whole number that may need all 64 bits.", Serialize: func(value interface{}) interface{} { number, ok := asInt64(value) if !ok { return nil } return number }, ParseValue: func(value interface{}) interface{} { number, ok := asInt64(value) if !ok { return nil } return number }, ParseLiteral: func(value ast.Value) interface{} { literal, ok := value.(*ast.IntValue) if !ok { return nil } number, err := strconv.ParseInt(literal.Value, 10, 64) if err != nil { return nil } return number }, })
Int64 is a whole number too big for the 32 bits a GraphQL Int promises.
The library serializes Int through int32 and returns nil for anything past 2147483647. A summed token count is an int64 and is not nullable, so once the total passed two billion the whole query failed with "Cannot return null for non-nullable field" instead of returning the number. Fields and arguments declared int64, uint64 or uint use this scalar, which goes on the wire as a plain JSON number like Int does.
var JSON = graphql.NewScalar(graphql.ScalarConfig{ Name: "JSON", Serialize: func(value interface{}) interface{} { raw, ok := value.(json.RawMessage) if !ok { if bytes, isBytes := value.([]byte); isBytes { raw = bytes } else { return value } } if len(raw) == 0 { return nil } var decoded interface{} if err := json.Unmarshal(raw, &decoded); err != nil { log.Errorf("graphapi: a JSON field does not hold JSON: %s", err) return nil } return decoded }, ParseValue: func(value interface{}) interface{} { raw, err := json.Marshal(value) if err != nil { log.Errorf("graphapi: cannot read a JSON field: %s", err) return nil } return json.RawMessage(raw) }, ParseLiteral: func(value ast.Value) interface{} { return nil }, })
JSON is a value that is already JSON: a json.RawMessage, sent as what it says rather than as the bytes that spell it.
It matters because reflect does not see a named type as its underlying one, so json.RawMessage — which is a []byte — did not match the []byte case above and fell through to the generic slice branch. An audit event's "before" and "after" came out as arrays of byte values: [123, 34, 105 ...], which is "{"i..." spelled one number at a time.
var Void = graphql.NewScalar(graphql.ScalarConfig{ Name: "Void", Serialize: func(interface{}) interface{} { return nil }, })