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.
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.
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:
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.
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.
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
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.
PostFeed
component that uses the useAllPostsQuery
and renders
the data into a Semantic React UI Table
.
post?.title
, post?.author.displayName
,
etc. Note that the title of the post is made into a link with the following:
src/components/avatar.ts
and fill it with this function that uses
random avatars we’ve supplied with the app boilerplate, as follows:
src/components/home.tsx
component to render the post list, as
follows:
yarn start
if you haven’t already) with a post list of the sample data you
have added to your Dgraph Cloud database.
/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.
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.
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:
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:
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!