Skip to content

Latest commit

 

History

History
 
 

env-variables-in-functions

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Environment Variables

Overview

This directory contains the service definition and file structure for a simple Graphcool service that demonstrates how to use environment variables inside functions. Read the last section of this README to learn how the different components fit together.

.
├── README.md
├── graphcool.yml
├── src
│   ├── hello.graphql
│   └── hello.js
└── types.graphql

Read more about service configuration in the docs.

Get started

1. Download the example

Clone the full framework repository and navigate to this directory or download only this example with the following command:

curl https://codeload.github.com/graphcool/framework/tar.gz/master | tar -xz --strip=2 framework-master/examples/env-variables
cd env-variables

Next, you need to create your GraphQL server using the Graphcool CLI.

2. Install the Graphcool CLI

If you haven't already, go ahead and install the CLI first:

npm install -g graphcool

3. Create the GraphQL server

The next step will be to deploy the Graphcool service that's defined in this directory.

To deploy the service and actually create your GraphQL server, invoke the following command:

graphcool deploy

When prompted which cluster you'd like to deploy, choose any of the Shared Clusters (shared-eu-west-1, shared-ap-northeast-1 or shared-us-west-2) rather than local.

Note: Whenever you make changes to files in this directory, you need to invoke graphcool deploy again to make sure your changes get applied to the "remote" service.

Testing the service

The easiest way to test the deployed service is by using a GraphQL Playground.

Open a Playground

You can open a Playground with the following command:

graphcool playground

Send the hello query with the name argument

To test the resolver function, you can send the following query:

{
  hello(name: "Sarah") {
    message
  }
}

The message that's returned in the payload will be: Hello Sarah.

Send the hello query without the name argument

To test the resolver function, you can send the following query:

{
  hello {
    message
  }
}

The message that's returned in the payload will be: Hello Alice. That's because the NAME environment variable that's referenced in hello.js is currently set to Alice inside graphcool.yml. If no name argument is passed in the query, the function will fall back to the value of the environment variable NAME.

What's in this example?

Setup

This function contains implementations for one resolver function called hello. The corresponding implementation can be found in hello.js.

The schema which defines the API of the resolver is defined in hello.graphql.

Both files are referenced from graphcool.yml:

hello:
  type: resolver
  schema: ./src/hello.graphql
  handler:
    code:
      src: ./src/hello.js
      environment:
        NAME: Alice

Referencing environment variables in functions at runtime

Notice that inside the function definition of hello in graphcool.yml, there's the hello.handler.code.environment property that let's you specify environment variables which can be accessed by your functions at runtime.

In this case, we're setting the value Alice for the environment variable NAME which is accessed by hello.js.