The first thing to do would be to create a form to insert a new task in the database.
BlueBase provides a utility to create forms quickly by just writing a simple JSON-based schema. Let's get right to it.
Add the following plugin as a dependency to the project:
Then import and use this plugin to BlueBase:
Now we're going to write a GraphQL mutation to insert a new task in the database. Create the following file.
This tutorial does not cover how GraphQL works. If you are new to it, head over to this official tutorial.
We will now generate typescript interfaces for our GraphQL API. Execute the following command:
This will create a new file at: src/graphql-types.ts
.
Create the follwing files:
Here's what's happening in the file above:
Line 8: We import the JsonGraphqlForm
from the BlueBase context. This component was added to our app by the @bluebase/plugin-json-graphql-components
plugin.
Line 15: We create a callback function, that will be called once the form is successfully submitted to the API. Here we want to navigate to the main page after success.
Line 19: If the output data of the form is different from what the API expects, we can define a function to map it to the right format.
In this case, the form inputs a single task object, but the API expects an array. So we do the conversion here. This function is passed as a prop to the JsonGraphqlForm
component.
Line 26: We pass our mutation to the form. This will be called when the form is submitted.
Line 31: This is our JSON schema that generates the form. Check the plugin docs for more information on its API.
Finally, we create the index file to export the component:
Now that we have created our form, it's time to add it to our layout. Change the the CreateTaskScreen
component match the following code:
We're all done. Time to take our form for a test drive. Input some data into the form and submit it.
If all goes well, you should now be redirected to the task list screen. Go to Supabase admin panel, and then to the table editor to check if the data was in fact inserted into the database.
Lastly, we want to allow our users to be able to delete tasks.
Let's write a mutation to delete our task.
We will now generate typescript interfaces for our GraphQL API. Execute the following command:
This will update the file src/graphql-types.ts
.
Now lets an IconButton
to delete the task.
Modify the EditTaskScreen
to add the headerRight
option:
This should be the result:
Our users may want to edit their tasks or mark them as complete. We will have to create a form very similar to the one in Chapter 4.1. The only difference is that we need to query task data and prefill the form with it.
Let's start by creating an update mutation. This will be used when a user submits the form.
We will now generate typescript interfaces for our GraphQL API. Execute the following command:
This will update the file src/graphql-types.ts
.
Create the following component:
Explanation:
Line 22: We take task ID as a prop. This will be used as a query variable.
Line 28: A function that takes the query result as input (array of tasks) and returns a single task. This task is used as the initial value of the form.
Line 33: A function that takes form data and converts it into mutation variables. This function is called when a user submits the form.
Line 57: We tell the GraphQL client to fetch data for our list query when the mutation is successful. If we don't do this, even after we successfully update a task, going back to the list screen will show old data. This is because Apollo will show data from its local cache. By retching a query we update the local cache.
Let's add our form to the EditTaskScreen
component:
When you run the app, you should results similar to the screenshot below:
It's time to query our API for all pending and completed tasks and display them as a list.
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
.
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.
We will now generate typescript interfaces for our GraphQL API. Execute the following command:
This will update the file src/graphql-types.ts
.
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.
Create the index file:
Last but not the least, lets add this list to the PendingTasksScreen
and CompletedTasksScreen
screens:
Refresh the app, and see it come to life. Enjoy!