Quickstart
Go from an empty Dgraph database to a running GraphQL API in just one step; just define the schema of your graph and how you’d like to search it; Dgraph does the rest.
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.
When you write an app that implements GraphQL over a REST endpoint or maybe over a relational database, you know that GraphQL issues many queries to translate the REST/relational data into something that looks like a graph. You also have to be familiar with the GraphQL types, fields, and resolvers. However, with Dgraph you can generate a running GraphQL API with the associated graph backend just by deploying the GraphQL schema of your API. Dgraph does the rest!
Step 1: Run Dgraph
The easiest way to get Dgraph up and running is on Hypermode.
Step 2: Deploy a GraphQL Schema
-
In the Schema tab of the console, paste the following schema:
-
Click
deploy
You now have a GraphQL API up and running and a graph database as a backend.
Step 3: Test your GraphQL API
You can access the GraphQL endpoint
with any GraphQL clients such as
GraphQL Playground,
Insomnia,
GraphiQL,
Altair or Postman.
If you want to use those clients, copy the GraphQL endpoint
from the
Cloud dashboard.
You may want to use the introspection capability of the client to explore the schema, queries, and mutations that were generated by Dgraph.
A first GraphQL mutation
To populate the database,
-
Open the API Explorer tab
-
Paste the following code into the text area:
-
Click Execute Query .
The GraphQL server returns a JSON response similar to this:
A second GraphQL mutation
Because the schema defined Customer with the field username: String! @id
, the
username
field acts like an ID, so we can identify customers just with their
names.
Products, on the other hand, had productID: ID!
, so they’ll get an
auto-generated ID which are returned by the mutation.
-
Paste the following mutation in the text area of the API Explorer tab.
-
Your ID for the product might be different than
0x2
. Make sure to replace the product ID with the ID from the response of the previous mutation. -
Execute the mutation
This time, the mutation result queries for the author making the review and the product being reviewed, so it’s gone deeper into the graph to get the result than just the mutation data.
GraphQL queries
With Dgraph, you get powerful graph search built into your GraphQL API. The schema for search is generated from the schema document that we started with and automatically added to the GraphQL API for you.
Remember the definition of a review.
The directive @search(by: [fulltext])
tells Dgraph we want to be able to
search for comments with full-text search.
Dgraph took that directive and the other information in the schema, and built queries and search into the API.
Let’s find all the products that were easy to install.
-
Paste the following query in the text area of the API Explorer tab.
-
Execute the mutation
What reviews did you get back? It’ll depend on the data you added, but you’ll at least get the initial review we added.
Maybe you want to find reviews that describe best GraphQL products and give a high rating.
How about we find the customers with names starting with “Mich” and the five products that each of those liked the most.
Conclusion
Dgraph allows you to have a fully functional GraphQL API in minutes with a highly scalable graph backend to serve complex nested queries. Moreover, you can update or change your schema freely and just re-deploy new versions. For GraphQL in Dgraph, you just concentrate on defining the schema of your graph and how you’d like to search that graph. Dgraph does the rest.
What’s next
Learn more about GraphQL schema and Dgraph directives.
Was this page helpful?