# graphql-gateway

**Please familiarize yourself with [GraphQL at The Orchard](https://github.com/theorchard/docs/tree/master/graphql) before contributing!**

## Front end to implementing services

### Running Gateway Locally (Unmanaged Mode)

It is possible to run the Gateway locally in unmanaged mode e.g. for testing a service locally. First: 

    cp .env.shadow .env

Comment out APOLLO_ENGINE_API_KEY and ENGINE_API_KEY in `.env`

Comment out APOLLO_ENGINE_SCHEMA_TAG ENGINE_SCHEMA_TAG in `.env`

Set managed to be false, and the environment variable for the service url you are working on e.g. in `.env`

    managed=false
    GRAPHQL_PRODUCT_URL=http://localhost:8081/graphql

Ensure you have the following headers:
    apollographql-client-name
    orchard-profile-type
    orchard-profile-id
    orchard-identity-id

Start the gateway

    yarn start

### Running Gateway Locally (Managed Mode)

Remove environment var `managed`
Set the environment var `APOLLO_ENGINE_API_KEY` to be the key of the graph you want. 

    yarn start 
    
    
### Testing

#### Unit

    yarn test:unit
    
    
#### Integration

 Write integration tests here for implementing services that are federated, and would therefore be unpractical to have 
 in the service repos themselves. 
 Run the service in 'unmanaged mode' then run:
    
    yarn test:integration
    

### Moving your graphQL calls to the gateway
All calls to a graphql service should now go via Gateway. 
Your calls should now query `graphql-gateway` and it will route your queries to the appropriate graphQL service.

Here is what needs to change in order for your queries to comply with the federated graphql model:

1. **Change your graphQL URL to either one of these depending on your environment:**

    ```
    https://ows-grass.theorchard.io/graphql-gateway/graphql
    https://qa-ows-grass.theorchard.io/graphql-gateway/graphql
    ```


    Example:
    Previously your graphQL url might have been `https://qa-graphql-product.theorchard.io/graphql`

    but now it's `https://qa-ows-grass.theorchard.io/graphql-gateway/graphql` and the gateway will
    handle routing your request to the proper graphQL service.


2. **Add a field in the headers of your query request:**

    `apollographql-client-name`

    If that header field is not provided then `graphql-gateway` will reject your request.

    Code for the header check here:

    https://github.com/theorchard/graphql-gateway/blob/master/src/server.ts#L89-L90
