Last active
May 7, 2019 14:56
-
-
Save MikeyBurkman/6caf93c0c887c7aad420f7cabf4b5460 to your computer and use it in GitHub Desktop.
Graphql Remote Schema Stitching TS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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