It's time to query our API for all pending and completed tasks and display them as a list.
Step 1: Task List Item
The first step is to create a list item that will render a single task.
Let's create a new component called TaskListItem . This component will have 2 states:
Loading State: When data is loading from the database. The modern way is to show a placeholder component.
To achieve this, we use the @bluebase/plugin-rn-placeholder plugin. This will add the components from the rn-placeholder library in BlueBase. Make sure you install and add this plugin to your project.
Data State: This will render the task data after it is loaded from the database. For this, we will use List.Item component.
See the following code for a reference implementation:
We also need to create a component that will be rendered when a list is empty. This component will show an empty message, as well as a call to action button to create a new task:
Now with these views out of the way, it's time to start creating the actual list. The first step is to write our GraphQL query.
We intend to create our list with infinite scrolling enabled. So, whenever new data is loaded, we need to merge it with the existing data, to show it as one list. To achieve this, we also create a function in this file called TasksCollectionQueryUpdateQueryFn .
import { FetchMoreOptions } from'@apollo/client';import gql from'graphql-tag';import { TasksCollectionQueryQuery } from'../../graphql-types';exportconstTasksCollectionQuery=gql` query TasksCollectionQuery( $filter: tasksFilter $first: Int # $last: Int # $before: Cursor $after: Cursor ) { tasksCollection( filter: $filter first: $first # last: $last # before: $before after: $after ) { edges { cursor node { id title completed } } pageInfo { endCursor hasNextPage hasPreviousPage startCursor } } }`;exportconstTasksCollectionQueryUpdateQueryFn:FetchMoreOptions<TasksCollectionQueryQuery>['updateQuery'] = ( previousResult, { fetchMoreResult }) => {if (!fetchMoreResult) {return previousResult; }constprevEdges=previousResult.tasksCollection?.edges || [];constnewEdges=fetchMoreResult.tasksCollection?.edges || [];return {// Put the new items at the end of the list and update `pageInfo`// so we have the new `endCursor` and `hasNextPage` values tasksCollection: {...previousResult.tasksCollection, edges: [...prevEdges,...newEdges], pageInfo: {...previousResult.tasksCollection?.pageInfo, endCursor:fetchMoreResult.tasksCollection?.pageInfo?.endCursor, hasNextPage:!!fetchMoreResult.tasksCollection?.pageInfo?.hasNextPage, hasPreviousPage:!!fetchMoreResult.tasksCollection?.pageInfo?.hasPreviousPage, startCursor:fetchMoreResult.tasksCollection?.pageInfo?.startCursor, }, }, };};
There is a bug in Supabase that doesn't let us send "last" and "before" params. When it is fixed, these lines should be uncommented.
Step 4: Generate Typings
We will now generate typescript interfaces for our GraphQL API. Execute the following command:
yarn graphql-codegen
This will update the file src/graphql-types.ts.
Step 5: Task List
With all the prep work now complete, let's finally create our list. For this we will use the GraphqlList component from the @bluebase/plugin-json-graphql-components plugin. This component takes care of all the heavy lifting.
We create this component with a complete prop so that we can filter task based on this value. This will be passed as a variable to the GraphQL query.