How to use GraphQL types to set a GraphQL schema for the Dgraph database. Includes scalars, enums, types, interfaces, union, password, & geolocation types.
Int
, Float
, String
, Boolean
and ID
. There’s also an Int64
scalar,
and a DateTime
scalar types represented as a string in RFC3339 format.
Scalar types, including Int
, Int64
, Float
, String
and DateTime
, can be
used in lists. Lists behave like an unordered set in Dgraph. For example:
["e1", "e1", "e2"]
may get stored as ["e2", "e1"]
, so duplicate values are
not stored and order might not be preserved. All scalars may be nullable or
non-nullable.
Int64
type introduced in release v20.11 represents a signed integer
ranging between -(2^63)
and (2^63 -1)
. Signed Int64
values in this range
are parsed correctly by Dgraph as long as the client can serialize the number
correctly in JSON. For example, a JavaScript client might need to use a
serialization library such as
json-bigint
to correctly write
an Int64
value in JSON.ID
type is special. IDs are auto-generated, immutable, and can be treated
as strings. Fields of type ID
can be listed as nullable in a schema, but
Dgraph never returns null.
ID
lists aren’t allowed. For example, tags: [String]
is
valid, but ids: [ID]
isn’t valid.ID
.
That includes IDs implemented through interfaces.tags: [String]
would always
contain unique tags. Similarly, recentScores: [Float]
could never contain
duplicate scores.
@embedding
directive. It denotes a
vector of floating point numbers, i.e an ordered array of float32. A type can
contain more than one vector predicate.
Vectors are normally used to store embeddings obtained from a language model.
When a Float vector is indexed, the GraphQL querySimilar<type name>ByEmbedding
and querySimilar<type name>ById
functions can be used for
similarity search.
A simple example of adding a vector embedding on name
to User
type is shown
below.
name_v
is an embedding on which the
@search directive
for vector embeddings is used.
ID
typeID
type. An ID
is auto-generated, immutable and never
reused. Each type can have at most one ID
field.
The ID
type works great when you need to use an identifier on nodes and don’t
need to set that identifier externally (for example, posts and comments).
For example, you might set the following type in a schema:
http://.../posts/0x123
when a user clicks to view the post with ID
0x123. Your app can then use a
getPost(id: "0x123") { ... }
GraphQL query to fetch the data used to generate
the page.
For input and output, ID
s are treated as strings.
You can also update and delete posts by ID
.
multiTags: [[Tag!]]
isn’t valid.@custom
directive.@search
then it is inherited by the implementing type’s field.
For example:
Question
and Comment
types to make the full
GraphQL types.
Question
and
Comment
get expanded as:
ID
field.ID
type fields since the ID
type
field isn’t a predicate. Note that in both interfaces and the implementing type,
the nullable condition and type (list or scalar) for the ID
field should be
the same. For example:
Union
type must include one or more unique member types.Union
type must all be Object base types;
Scalar, Interface and Union
types must not be
member types of a Union. Similarly, wrapping types must not be member types of
a Union.HomeMember
union type:
Home
, you can submit a GraphQL query
like this:
@secret
directive. Passwords can’t be queried directly, only checked for a
match using the checkTypePassword
function where Type
is the node type. The
passwords are encrypted using Bcrypt
.
@secret
fields.schema.graphql
graphql
endpoint
(http://localhost:8080/graphql):
Point
, Polygon
and MultiPolygon
. These types are useful in
scenarios like storing a location’s GPS coordinates, representing a city on the
map, etc.
For example: