We’re overhauling Dgraph’s docs to make them clearer and more approachable. If
you notice any issues during this transition or have suggestions, please
let us know.
Scalars
Dgraph’s GraphQL implementation comes with the standard GraphQL scalar 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.
The
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.
- Schema rule:
IDlists aren’t allowed. For example,tags: [String]is valid, butids: [ID]isn’t valid. - Schema rule: Each type you define can have at most one field with type
ID. That includes IDs implemented through interfaces.
tags: [String] would always
contain unique tags. Similarly, recentScores: [Float] could never contain
duplicate scores.
Vectors
A Float array can be used as a vector using@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.
The ID type
In Dgraph, every node has a unique 64-bit identifier that you can expose in
GraphQL using the ID 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, IDs are treated as strings.
You can also update and delete posts by ID.
Enums
You can define enums in your input schema. For example:Types
From the built-in scalars and the enums you add, you can generate types in the usual way for GraphQL. For example:- Schema rule: Lists of lists aren’t accepted. For example:
multiTags: [[Tag!]]isn’t valid. - Schema rule: Fields with arguments aren’t accepted in the input schema unless
the field is implemented using the
@customdirective.
Interfaces
GraphQL interfaces allow you to define a generic pattern that multiple types follow. When a type implements an interface, that means it has all fields of the interface and some extras. According to GraphQL specifications, you can have the same fields in implementing types as the interface. In such cases, the GraphQL layer generates the correct Dgraph schema without duplicate fields. If you repeat a field name in a type, it must be of the same type (including list or scalar types), and it must have the same nullable condition as the interface’s field. Note that if the interface’s field has a directive like@search then it is inherited by the implementing type’s field.
For example:
GraphQL generates the correct Dgraph schema where fields occur only once.
Question and Comment types to make the full
GraphQL types.
Question and
Comment get expanded as:
If you have a type that implements two interfaces, Dgraph won’t allow a field
of the same name in both interfaces, except for the
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
GraphQL Unions represent an object that could be one of a list of GraphQL Object types, but provides for no guaranteed fields between those types. So no fields may be queried on this type without the use of type refining fragments or inline fragments. Union types have the potential to be invalid if incorrectly defined:- A
Uniontype must include one or more unique member types. - The member types of a
Uniontype must all be Object base types; Scalar, Interface andUniontypes 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:
Password type
A password for an entity is set with setting the schema for the node type with@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.
For security reasons, Dgraph enforces a minimum password length of 6
characters on
@secret fields.-
Cut-and-paste the following schema into a file called
schema.graphql -
Run the following curl request:
-
Set the password by pointing to the
graphqlendpoint (http://localhost:8080/graphql):
Geolocation types
Dgraph GraphQL comes with built-in types to store Geolocation data. Currently, it supportsPoint, 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: