Dgraph supports Apollo federation so that you can create a gateway GraphQL service that includes the Dgraph GraphQL API and other GraphQL services
@key
,
@extends
, @external
, @provides
, and @requires
.
@key
directive@key
field. There are
few limitations on how to use @key
directives:
@key
directive only once for a type@key
field acts as a foreign key to resolve entities from the
service where itβs extended, the field provided as an argument inside the
@key
directive should be of ID
type or have the @id
directive on it.@extends
directiveUser
type is defined in some other service, you can extend it in Dgraphβs
GraphQL service by using the @extends
directive, as follows:
extend
keyword. Either syntax to extend a
type into your Dgraph GraphQL service works: extend type User ...
or
type User @extends ...
.
@external
directiveid
field of the User
type.
@provides
directiveReview.product
from the review
service, and if the name
or
price
is also queried, the gateway fetches these from the review
service
itself. So, the review
service also resolves these fields, even though both
fields are @external
.
@requires
directiveuser.reviews
from the review
service, the gateway
gets user.email
from the User
service and provides it as an argument to the
_entities
query.
Using @requires
alone on a field doesnβt make much sense. In cases where you
need to use @requires
, you should also add some custom logic on that field.
You can add such logic using the @lambda
or @custom(http: {...})
directives.
Hereβs an example -
Astronaut
arenβt exposed to the gateway because they resolve
through the _entities
resolver. However, these queries are available on the
Dgraph GraphQL API endpoint.
extended
typesAstronaut
type which is extended in this
service. The mutation addAstronaut
takes AddAstronautInput
, which is
generated as follows:
id
field is of ID
type, which is usually generated internally by Dgraph.
But, In this case, itβs provided as an input. The user should provide the same
id
value present in the GraphQL service where the type Astronaut
is defined.
For example, letβs assume that the type Astronaut
is defined in some other
service, AstronautService
, as follows:
Astronaut
, you should first add it to the
AstronautService
service. Then, you can call the addAstronaut
mutation with
the value of id
provided as an argument that must be equal to the value in
AstronautService
service.