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.
Step 3: Generate Typings
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.
Step 4: Create Form
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:
Step 5: Use Form on Screen
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.
Web
iOS
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.