类型
The fundamental unit of any GraphQL Schema is the type. There are eight kinds of types in GraphQL.
The most basic type is aScalar
. A scalar represents a primitive value, like a string or an integer. Oftentimes, the possible responses for a scalar field are enumerable. GraphQL offers anEnum
type in those cases, where the type specifies the space of valid responses.
Scalars and Enums form the leaves in response trees; the intermediate levels areObject
types, which define a set of fields, where each field is another type in the system, allowing the definition of arbitrary type hierarchies.
GraphQL supports two abstract types: interfaces and unions.
AnInterface
defines a list of fields;Object
types that implement that interface are guaranteed to implement those fields. Whenever the type system claims it will return an interface, it will return a valid implementing type.
AUnion
defines a list of possible types; similar to interfaces, whenever the type system claims a union will be returned, one of the possible types will be returned.
All of the types so far are assumed to be both nullable and singular: e.g. a scalar string returns either null or a singular string. The type system might want to define that it returns a list of other types; theList
type is provided for this reason, and wraps another type. Similarly, theNon-Null
type wraps another type, and denotes that the result will never be null. These two types are referred to as “wrapping types”; non‐wrapping types are referred to as “base types”. A wrapping type has an underlying “base type”, found by continually unwrapping the type until a base type is found.
Finally, oftentimes it is useful to provide complex structs as inputs to GraphQL queries; theInput Object
type allows the schema to define exactly what data is expected from the client in these queries.