Im currently working on a project which will be a mobile app for parents to swap unused items. To do this im using Expo with a FaunaDB database. I had already setup a project with Fauna and Gatsby so thought the knowledge transfer would be quite simple. And it was, until it wasn't.
The problem with trying to access the Fauna database from a React Native (RN) app is that RN doesn't come with or support the Node Standard Library, which Fauna uses. The solution, or the one I found, is to create a separate API to handle the Fauna side of things and access that API form the app. Let's go through a simple setup to see how this is done.
Make sure you have installed the expo-cli and now-cli.
Create a new expo project, follow the instructions (you can choose a blank or tabs template) and open your code editor
Ok, leave the app for a minute and login to your Fauna account, create a new database and name it whatever you like. Perhaps MySuperDB? Create a new key with a server role. Name it FAUNA_SERVER. Make a note of it as it wont be shown again.
Back to our app we now need to add some packages. Its a long list so just do it all in one.
First we want to setup our Apollo Provider which will wrap our root component, App.js. Create a new file in the constants folder and name it apollo.js
The above code can be boiled down to:
Create a new .env file at the projects root and add your fauna server key, using the name we gave it in our fauna dashboard.
Next head over to App.js and import and wrap the App with our new Apollo component. (I had used the tab template so yours will look a bit different if you chose the blank templete)
Now we can add a schema, which we will import into our Fauna database via their dashboard. Inside the constants folder create a new file named schema.gql.
Back on the Fauna dashboard click the graphql tab on the left and import the schema. Once done you will be presented with a graphql playground. Add some users to our database.
Ok cool, now we can setup our serverless function. Create a new project and initialize it.
Vercel expects us to have an api folder so create that. We will also want to install some packages here.
Inside the api folder create a new folder that begins with an underscore, this is so that vercel knows not to build it's content as a functions. Inside that folder create a new file named config.js.
Create a .env file at the root and add the Fauna server key. Then in the new config.js file add the following. This is basically a short cut for us having to type out process.env.MY_KEY. Right now we only have one key but we might want to add more later. I find this a handy little file to use.
Back to the api folder create a new file called graphql.js and add the following.
Run now from the mySuperAPI root and deploy the API. Head back over to the app and open the HomeScreen.js file located in the screens folder. Import gql and the useQuery hook from Apollo. Add a query to get all the users you just created and use the useQuery hook to display the returned data.
Now its time to run the app and bask in your glory!
You have multiple options to view your app, the easiest is to just scan the QR code on the metro bundler browser screen.
You now have a working mobile app which is pulling data with Apollo from FaunaDB via a serverless API hosted on Vercel. Yay for technology! 🤩