GraphQL Queries
In this step, you can move on to the GraphQL queries that get the data to render the main pages, thanks to Apollo Client, Dgraph Cloud, and React routing.
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.
With Apollo Client set up and connected to your Dgraph Cloud backend, and React routing set up, you can move on to the GraphQL queries that get the data to render the main pages.
You’ll use GraphQL Code Generator to generate typed React hooks that help contact the GraphQL endpoint, and then use those hooks in the React components to get the data.
GraphQL Code Generator
The Apollo Client libraries give you generic React hooks to contact GraphQL backends, but GraphQL Code Generator takes that to the next level by using GraphQL introspection to generate types with hooks specific to the API you are using. That means all your GraphQL calls are typed and if anything ever changes, you’ll know at development time.
Firstly, add all the GraphQL Code Generator dependencies as development dependencies to the project with:
You can then run the following command to set up GraphQL Code Generator for the project:
However, you can skip the setup steps and jump straight to using it by adding a
file, codegen.yml
, in the top-level project directory. The following is the
configuration needed for this project. Remember to replace
<<Dgraph Cloud-GraphQL-URL>>
with the URL or your Dgraph Cloud endpoint.
That configuration tells GraphQL Code Generator to introspect the schema of your
GraphQL API, generate using the typescript
plugin and place the generated code
near-operation-file
(we’ll see what that means just below).
Then, add "generate-types": "graphql-codegen --config codegen.yml"
to the
scripts key in package.json, so it now looks like:
Now, whenever the schema of your GraphQL database changes, you can regenerate the project’s types with:
Running that now, won’t do anything though, because you have to start your GraphQL development first.
GraphQL operations
You can layout the source of a Dgraph Cloud project however you wish. For this tutorial you’ll use the following project structure.
You’ll write GraphQL queries and mutations in the operations.graphql
file.
Then, run GraphQL Code Generator and it generates the src/types/graphql.ts
file with global types for the things that make sense globally and
src/components/types/operations.ts
for things that are local to the
components.
Having operations.graphql
file in the directory for the components that it
applies to makes it really easy to find the GraphQL (rather than it being split
as strings in a number of javascript files) while still making it clear what
components the GraphQL applies to. If your project gets larger, you might end up
with more project structure and more operations files, but the general process
still works.
Start by creating the scr/components/operations.graphql
file and add a query
to find the data for home page’s list of posts.
Then run:
…and GraphQL Code Generator will create the src/types/graphql.ts
and
src/components/types/operations.ts
files. If your interested in what was
generated, open up those files and you’ll see how much the code generator did.
If you want to use that to build a UI, read on.
GraphQL React hooks
Of the things that GraphQL Code Generator built after introspecting your GraphQL endpoint, it’s the React hooks you’ll use most in building a UI.
From the allPosts
query in the operations.graphql
file, GraphQL Code
Generator built a hook useAllPostsQuery
with everything you need to make that
GraphQL query.
In general, you’ll use it like this
The data
result will have exactly the same structure as the allPosts
operation, and it’s typed, so you can layout with confidence by for example
using map
on the post list returned by queryPost
and then indexing into each
post.
Because of the types, you really can’t go wrong.
Layout with GraphQL - post list
Now that you have GraphQL to help write queries and get data and GraphQL Code Generator to turn that into typed Javascript, you can now layout your data and be sure you won’t make a mistake because GraphQL and types will catch you.
Let’s make a PostFeed
component that uses the useAllPostsQuery
and renders
the data into a Semantic React UI Table
.
There’s some layout and CSS styling in there, but the actual data layout is just
indexing into the queried data with post?.title
, post?.author.displayName
,
etc. Note that the title of the post is made into a link with the following:
When clicked, this link will go through the React router to render the post component.
You can add whatever avatar links you like into the data, and you’ll do that
later in the tutorial after you add authorization and logins; but for now, make
a file src/components/avatar.ts
and fill it with this function that uses
random avatars we’ve supplied with the app boilerplate, as follows:
Then, update the src/components/home.tsx
component to render the post list, as
follows:
With this much in place, you will see a home screen (start the app with
yarn start
if you haven’t already) with a post list of the sample data you
have added to your Dgraph Cloud database.
Each post title in the post list is a link to /post/0x...
for the id of the
post. At the moment, those like won’t work because there’s not component to
render the post. Let’s add that component now.
Layout of a post with GraphQL
Adding a new component that relies on different data is a matter of adding the
right query to src/components/operations.graphql
, regenerating with GraphQL
Code Generator, and then using the generated hook to layout a component.
Add a GraphQL query that gets a particular post by it’s id to
src/components/operations.graphql
with this GraphQL query.
Then, regenerate with:
…and you’ll be able to use the useGetPostQuery
hook in a component. The
difference with the previous hook is that useGetPostQuery
relies on a variable
id
to query for a particular post. You’ll use React router’s useParams
to
get the id passed to the route and then pass that to useGetPostQuery
like
this:
Laying out the post component is then a matter of using the data
from the hook
to layout an interesting UI. Edit the src/components/post.tsx
component, so it
lays out the post’s data like this:
Now you can click on a post from the home screen and navigate to its page.
This Step in GitHub
This step is also available in the tutorial GitHub repo with the graphql-queries tag and is this code diff.
If you have the app running (yarn start
) you can navigate to
http://localhost:3000
to see the post list on the home screen and click on a
post’s title to navigate to the post’s page. In the diff, we’ve added a little
extra, like the Diggy logo, that’s also a link to navigate you home.
Now on to the mutations!
Was this page helpful?