MCPcopy
hub / github.com/dgraph-io/dgraph / cantCoerceScalar

Function cantCoerceScalar

query/outputnode_graphql.go:1365–1395  ·  view source on GitHub ↗

cantCoerceScalar tells whether a scalar value can be coerced to its corresponding GraphQL scalar.

(val []byte, field gqlSchema.Field)

Source from the content-addressed store, hash-verified

1363
1364// cantCoerceScalar tells whether a scalar value can be coerced to its corresponding GraphQL scalar.
1365func cantCoerceScalar(val []byte, field gqlSchema.Field) bool {
1366 switch field.Type().Name() {
1367 case "Int":
1368 // Although GraphQL layer would have input coercion for Int,
1369 // we still need to do this as there can be cases like schema migration when Int64 was
1370 // changed to Int, or if someone was using DQL mutations but GraphQL queries. The GraphQL
1371 // layer must always honor the spec.
1372 // valToBytes() uses []byte(strconv.FormatInt(num, 10)) to convert int values to byte slice.
1373 // so, we should do the reverse, parse the string back to int and check that it fits in the
1374 // range of int32.
1375 if _, err := strconv.ParseInt(string(val), 0, 32); err != nil {
1376 return true
1377 }
1378 case "String", "ID", "Boolean", "Int64", "Float", "DateTime":
1379 // do nothing, as for these types the GraphQL schema is same as the dgraph schema.
1380 // Hence, the value coming in from fastJson node should already be in the correct form.
1381 // So, no need to coerce it.
1382 default:
1383 enumValues := field.EnumValues()
1384 // At this point we should only get fields which are of ENUM type, so we can return
1385 // an error if we don't get any enum values.
1386 if len(enumValues) == 0 {
1387 return true
1388 }
1389 // Lets check that the enum value is valid.
1390 if !x.HasString(enumValues, toString(val)) {
1391 return true
1392 }
1393 }
1394 return false
1395}
1396
1397// toString converts the json encoded string value val to a go string.
1398// It should be used only in scenarios where the underlying string is simple, i.e., it doesn't

Callers 1

encodeMethod · 0.85

Calls 5

HasStringFunction · 0.92
toStringFunction · 0.70
NameMethod · 0.65
TypeMethod · 0.65
EnumValuesMethod · 0.65

Tested by

no test coverage detected