Find a Doc, Japan - Server
Back-end Repository
Welcome to the back-end repository for Find a Doc, Japan! We're not currently looking for contributors in this particular repo, but you're free to have a look around. If you want to provide any suggestions or feedback, come have a chat with us in the #backend-team channel on Slack!
How to contribute
We love and welcome contributions to our front-end repository which can be found here!
Contributors
Prerequisites
- Node
- We recommend using nvm and running
nvm use
in this directory to sync with the project's Node version. However, if you wish to install Node your own way and ensure a consistent version with .nvmrc
, that's fine too
- Yarn Berry
- Docker Desktop
Setup
1. Install dependencies
npm install
2. Set up Husky Hooks 🐕️
This will automatically run linting before commits and reduce common contributor mistakes.
npm run prepare
3. Setting up the Database 🐘
For simplicity, we use firebase!
For security, we run a local database so we don't break production!
(optional) You can point to different database environments simply by changing the firebase url and variables in the .env.dev
file.
3.a Setup Firebase CLI
a.1 Install the Firebase CLI if you do not have it installed on your machine:
npm -g i firebase-tools
To be able to run the firestore emulator you need to have Java version 11 or later installed. If not installed, please follow the next step (a.2).
a.2 Install Java
on a mac, we recommend using homebrew
brew install openjdk
on a windows pc, we recommend using chocolatey
choco install openjdk
You might have to restart vs code for step a.2 and a.3 all of these to take effect in the project
a.3 Log into Firebase CLI:
firebase login
3.b Running the database locally
npm run dev:startlocaldb
3.c Starting the dev server and seeding the emulator
npm run dev
This will run until you shut down the instance hitting ctrl^+C
(Optional) Connecting to Production database
Setting Up Firebase Service Account
To set up the Firebase Service Account for this project, follow these steps:
- Visit Firebase console.
- Under "Project Overview", click on "Project Settings".
- Navigate to the "Service Accounts" tab.
- Click the "Generate new private key" button. Ensure that Node.js is selected.
- Download the JSON file and add it to the root directory of this project. Rename the file to
firebaseServiceAccountKey.json
.
- In your
.env
file, create an environment variable called SERVICE_ACCOUNT_PATH
with the value ./firebaseServiceAccountKey.json
.
4. Run the API
npm run dev:startserver
That's it!
(Optional) Running in production mode
In production, we use docker to run the app. You can validate your code works in production by running
Make sure that you have docker running beforehand.
npm run prod:build
npm run prod
How to Debug your code
This runs locally and can easily be debugged with vscode!
Click on the Run and Debug
vscode tab, and then choose Javascript Debug Terminal
, then run npm run dev
in there and the debugger will automatically connect.
Then, just add breakpoints by clicking on the line number of your code.
How to Test your code
Testing with Vitest
1. Start the Docker container:
```sh
npm run test:dockerstart
```
2. Run the tests:
```sh
npm run test
```
Testing GraphQL
1. Run `npm run generate` to generate the types locally
2. Run `npm run dev` to start the local server
3. Open your browser to
4. Navigate to the Explorer section from the menu in the left pane.
5. Click `query: Query` under "Root Types
![image](./docs/root-types.png)
6. Click the `+` button to see the fields a **type** has that can be added to the query
![image](./docs/add-to-query.png)
7. Select the desired fields and they'll automatically get added to the query builder
![image](./docs/query-builder.png)
8. If you select a type that requires an ID (such as `Facility` or `HealthcareProfessional`) then add the ID in the "Variables" window at the bottom _as a string_.
![image](./docs/query-by-id.png)
9. If you'd like to share the query you built, such as demonstrating how you tested your code, check out [Apollo Explorer's sharing features](https://www.apollographql.com/blog/announcement/platform/save-and-share-your-graphql-operations-in-apollo-explorer/#sharing-a-collection).
Troubleshooting
Firestore Indexing Error: "The query requires an index..."
When you running query using the `getSubmissions` method that requires ordering by a specific field, if an index hasn't been created for the combination of that field and the order direction, you might receive an error response. This response will typically contain a direct link to create the required index in the **Firebase Console**. Here's how you can proceed:
1. Click on the link in the `Error Response`: This link will redirect you to the Firebase Console, specifically to the Firestore section where you can create indices.
![image](docs/gql-studio-error-message.png)
2. In the Firebase Console, you should see a window labeled `Create or update indexes`. Click the `Save` button: This will initiate the process of creating the index. Index creation might take a few minutes.
![image](docs/firebase-create-index.jpeg)
3. Wait for the Index to be ready: Firestore will show the status of the index. Once it changes from `Building` to `Enabled`, you can proceed to run your GraphQL query again.
![image](docs/firebase-building-index.png)
4. Run Your GraphQL Query Again: With the index in place, your query should now execute without any errors related to indexing.