Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Reload of gateway for schema changes in federated service apollo GraphQL

Tags:

In Apollo Federation, I am facing this problem: The gateway needs to be restarted every time we make a change in the schema of any federated service in service list. I understand that every time a gateway starts and it collects all the schema and aggregates the data graph. But is there a way this can be handled automatically without restarting the Gateway as it will down all other unaffected GraphQL Federated services also

Apollo GraphQL , @apollo/gateway

like image 951
A.K Avatar asked Sep 27 '19 18:09

A.K


People also ask

How do you refresh a GraphQL schema?

Select the database you created in previous articles and go to the GRAPHQL section from the left menu bar. Click on the UPDATE SCHEMA button and select the file containing the updated schema. At the database layer, the update process creates any missing collections, indexes, and functions.

What is Federated schema GraphQL?

Schema federation is an approach for consolidating many GraphQL APIs services into one. This is helpful for companies with multiple teams that contribute to different portions of a single API or to enforce domain boundaries with separate services.

What is federated gateway?

A common architecture is the so-called federation gateway. In this approach IdentityServer acts as a gateway to one or more external identity providers.

How does GraphQL federation work?

GraphQL federation allows you to set up a single GraphQL API, or a gateway, that fetches from all your other APIs. Your mission service and your astronaut service are now *subgraphs*. Your frontend only needs to query the gateway no matter how the services are split up behind it.


2 Answers

There is an experimental poll interval you can use:

const gateway = new ApolloGateway({
  serviceList: [
    { name: "products", url: "http://localhost:4002" },
    { name: "inventory", url: "http://localhost:4001" },
    { name: "accounts", url: "http://localhost:4000" }
  ],
    debug: true,
    experimental_pollInterval:3000
})

the code above will pull every 3 seconds

like image 75
Olivier Refalo Avatar answered Oct 20 '22 14:10

Olivier Refalo


I don't know other ways to automatically reload the gateway other than polling. I made a reusable docker image and i will keep updating it if new ways to reload the service emerge. For now you can use the POLL_INTERVAL env var to periodically check for changes. Here is an example using docker-compose:

version: '3'

services:
    a:
        build: ./a # one service implementing federation
    b:
        build: ./b
    gateway:
        image: xmorse/apollo-federation-gateway
        ports:
            - 8000:80
        environment: 
            CACHE_MAX_AGE: '5' # seconds
            POLL_INTERVAL: '30' # seconds
            URL_0: "http://a"
            URL_1: "http://b"
like image 23
Morse Avatar answered Oct 20 '22 16:10

Morse