We are working on a new batch of GraphQL features. One of those is the ability to extend the generated GraphQL API with custom logic.
There’s a first version built that we are moving through testing and into a beta release. It’ll be part of beta releases until the release of 20.07.0. We are really keen to get feedback and make this the most useful feature it can be.
Docs and examples will also land pretty soon. But seeing as there’s been lots of interest in this sort of feature I’m putting up this page as a way of recording interest, discussion, feature requests etc.
Custom Logic
Initially, custom logic is available by calling out to a http endpoint. First versions will support both REST endpoints and GraphQL.
Support is provided to define your own types, as well as the url of the endpoint and the body/graphql-query patterned from the arguments.
For example, you can define a custom query as follows:
type Query {
myCustomQuery(arg1: MyInputType, arg2: String): [MyResult] @custom(
http: {
url:"http://my.api.com/endpoint/$arg2",
method:POST,
body:"{ data: $input }"},
forwardHeaders: [ ... ] )
}
input MyInputType { ... }
type MyResult @remote { ... }
This works for queries, mutations and fields within types. You can also pass on headers from the incoming request to the backend (@remote
means the data is in the remote service, not Dgraph).
This allows you knit together Dgraph data and external data.
As a simple example, you can take an external data service, like a GraphQL service that just lists movies, and start to build a GraphQL server that provides reviews linking local and remote data, etc.
type User {
name: String! @id
...
}
type Movie @remote { ... }
type Review {
id: ID!
text: String @search(by: [term])
rating: Int @search
byUser: User
...
ofMovie: String!
movie: Movie @custom(http: {
url: "http://movie.server/graphql",
graphql: "query { movie(id: $ofMovie) }"
})
}
At query time, Dgraph takes a query like:
query {
queryReview(filter: { text: { anyofterms: "brilliant" } }, order: { asc: rating }, first: 10) {
text
rating
byUser {
username
...
}
movie { ... }
}
and works out what part it can resolve locally (including some query planning to make sure it gets the Review’s ofMovie
field so it can make the custom call) resolves that against Dgraph, and then uses the resolved data to resolve any further custom requirements (in this case finding the movie details) before building the response.
There’s a world of possibility here from knitting together existing GraphQL services to adding bespoke custom logic to your Dgraph GraphQL instance.
We’ll post more examples and links to documentation as it comes online. However, if you’ve got questions, feature requests, etc please feel to be involved in the conversation.