Preflight Scripts
These scripts allow you to automatically run custom authentication processes before executing your GraphQL operations. They’re especially useful for handling authentication flows like OAuth, where you may need to refresh an access token. Let’s explore how it works.
Configuring Preflight Script
To create a script click on the command line icon (right after Operation Collections plugin icon) in GraphiQL sidebar section.
You will see Script editor (JavaScript language) which is read-only and present for a quick view of your saved script and Environment variables editor (JSON language) which is persistent in localStorage.
Editing Preflight Script
Clicking on the Edit
button will open Modal where you can edit, test and save your script in
database.
Note: Your script will stored as plain text in our database, don’t put any secrets there, use Environment variables editor for it! The preflight script is accessible to all members of your organization, but only users with access to target Settings can edit the script code.
You can use any JavaScript syntax (including top-level await
) in the Script editor. Getting and
Setting environment variables is done by accessing the environment
property on the lab
global
variable.
// get myKey variable from the Environment variables editor
lab.environment.get('myKey')
// set myKey variable to the Environment variables editor (persistent in localStorage)
lab.environment.set('myKey', myValue)
CryptoJS
Additionally, you can access the CryptoJS library by accessing
the CryptoJS
property on the lab
global variable.
Global Variables and Errors
Access to global variables such as this
, window
or globalThis
is restricted. Errors thrown by
the script will be displayed in Console Output.
Using Environment Variables
To use your environment variables in GraphiQL headers editor wraps environment keys with double-curly braces, e.g.:
{
"Authorization": "Bearer {{myEnvVar}}"
}