API Testing with Insomnia - CDCgov/prime-simplereport GitHub Wiki

Intro

This page explains how to make requests to the GraphQL API using Insomnia.

Prerequisites

Setup

Add a new GraphQL request

  1. Click the + dropdown
  2. Click "GraphQL Request"
  3. Update the endpoint to be whatever your backend URL is + /graphql
  • e.g., if running the backend on http://localhost:8080, it would be http://localhost:8080/graphql
  • if running the backend on https://localhost.simplereport.gov/api, it would be https://localhost.simplereport.gov/api/graphql
  • if trying to make a GraphQL request against production, it would be https://www.simplereport.gov/api/graphql
  1. Click the "Header" tab
  2. Add a new header called Content-Type with a corresponding value of application/json
  3. Add another new header called Authorization with a corresponding value of Bearer ${access_token} (follow these steps to get the ${access_token} value)
  4. Click on the "GraphQL" tab
  5. Ensure it is a "POST" request
  1. Paste the following query and ensure you get a 200 response:
query {
	whoami {
		id
	}
}
  1. You are ready to make requests to the GraphQL API!

Get access_token from browser's local storage

  • NOTE: You'll need to update the Bearer token periodically, as SimpleReport re-generates access tokens regularly for security purposes.

  • Load the simplereport app in a web browser

    • The following will be instructions for a Chrome browser, but other browsers should be similar
  • Open the dev tools for your browser (CMD + shift + I, or right click -> "Inspect")

  • Follow steps 1 OR 2 below

1. via the "Console" tab

  • Click the "Console" tab
  • Run localStorage.getItem("access_token")
  • Copy the returned value without the beginning and ending quotes

2. via the "Application" tab

  • Click the "Application" tab
  • Click "Local Storage" and click the URL where the frontend is running
  • Get the value of the key called access_token

Common Issues

  • If you see any errors saying it had issues loading the schema or something of that nature, try clicking the small "schema" with a wrench button and then click "Refresh Schema"

Tips

Environment Overrides

You may want to create "Environment Overrides" for your collection of requests, so you do not have to update the bearer token on each request manually. To do so, do the following:

  1. Right-click your collection of requests in the sidebar, and that should open a dropdown
  2. Click "Environment"
  1. That should open up a pop-up called "Environment Overrides (JSON Format)"
  1. Here you can add any variables, for example
{
  "bearer": "[YOUR_TOKEN_HERE]"
}
  1. Now you can use the variable in your requests.