Skip to content

Instantly share code, notes, and snippets.

@MikeyBurkman
Last active May 7, 2019 14:56
Show Gist options
  • Save MikeyBurkman/6caf93c0c887c7aad420f7cabf4b5460 to your computer and use it in GitHub Desktop.
Save MikeyBurkman/6caf93c0c887c7aad420f7cabf4b5460 to your computer and use it in GitHub Desktop.
Graphql Remote Schema Stitching TS
import { readFileSync } from 'fs';
import { mergeSchemas, makeExecutableSchema } from 'graphql-tools';
import { getRemoteSchemas } from './remoteSchemas';
import { Resolvers } from './resolvers';
export const createSchema = async () => {
const resolvers = [Resolvers];
const localSchema = readFileSync(require.resolve('./schema.gql'), 'utf8');
const remoteSchemas = await Promise.all(Object.values(getRemoteSchemas()));
return mergeSchemas({
schemas: [...remoteSchemas, localSchema],
resolvers
});
};
import {
makeRemoteExecutableSchema,
introspectSchema,
transformSchema,
FilterRootFields
} from 'graphql-tools';
import config from 'config';
import { HttpLink } from 'apollo-link-http';
import fetch from 'node-fetch';
const tripsServiceUrl = config.get<string>('services.trips');
const fetchSchema = async (uri: string) => {
const link = new HttpLink({ uri, fetch });
const schema = await makeRemoteExecutableSchema({
schema: await introspectSchema(link),
link
});
// Remove all query/mutations from the schema -- we only care about the types
return transformSchema(schema, [new FilterRootFields(() => false)]);
};
export const getRemoteSchemas = () => ({
trips: fetchSchema(tripsServiceUrl) // No schema stitching with trips service for now
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment