1
votes

Having a microservice architecture I want an API to be complete even if some microservices are down. I know that this issue can be resolved with a proper orchestration, but I'm interested whether it's possible to handle this situation using only the code.

The problem:

I have 3 microservices: core, A, B. If I start core and A is down I need core to boot successfully and contain A's api. To achieve this I've tried to get type definitions from each remote GraphQL schema and store them in etcd or elsewhere and use to create executable schema if the service is not reachable and thus its schema.

How do I do this if in core I can only get executable schema via apollo link.

const getRemoteSchema = async ({ uri, name }) => {
  const link = setContext((request, previousContext) => ({
    headers: {
      context: `${JSON.stringify(previousContext.graphqlContext || {})}`,
    },
  })).concat(new HttpLink({ uri, fetch }));

  let schema = await introspectSchema(link).catch(error => new ApolloError(error.message, 'INTERNAL_SERVER_ERROR', error));

  if (schema instanceof ApolloError) // do something

If I'm not mistaking I can only call the following methods on the fetched schema: getQueryType, getTypeMap, etc... these return an object not a string definitions. Is it possible to get typeDefs and store them. I don't want to store the whole executable schema because that may pose security issues as resolvers will be stored in the db.

I don't want to use introspection as it will be disabled in production and also don't want each microservice to send it's typeDefs independently

1

1 Answers

2
votes

Ok, after some digging I found out the solution which is to use printSchema from graphql/utilities

import { printSchema } from 'graphql/utilities';
const typeDefs = printSchema(schema);