Skip to content

Instantly share code, notes, and snippets.

@szaza
Last active March 26, 2019 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szaza/12b079129283197036bcf96124b0af10 to your computer and use it in GitHub Desktop.
Save szaza/12b079129283197036bcf96124b0af10 to your computer and use it in GitHub Desktop.
Introspection example to resolve GraphQL schema stitching error swallowing.
import fetch from "cross-fetch";
import {
makeRemoteExecutableSchema,
introspectSchema,
mergeSchemas
} from "graphql-tools";
import { createHttpLink } from "apollo-link-http";
import { onError } from "apollo-link-error";
import { GraphQLSchema, GraphQLError } from "graphql";
const errorLink = onError(args => {
if (args.graphQLErrors && args.graphQLErrors.length === 1) {
args.response.errors = args.graphQLErrors.concat(new GraphQLError(""));
}
});
export class Introspection {
public static async getRootSchema(endpoints: string[]) {
return mergeSchemas({
schemas: await Promise.all(endpoints.map(this.getIntrospectSchema))
});
}
public static async getIntrospectSchema(
url: string
): Promise<GraphQLSchema> {
// Create a link to a GraphQL instance by passing fetch instance and url
const makeServiceLink = () =>
createHttpLink({
uri: url,
fetch
});
// Fetch our schema
const serviceSchemaDefinition = await introspectSchema(
makeServiceLink()
);
// make an executable schema
return makeRemoteExecutableSchema({
schema: serviceSchemaDefinition,
link: errorLink.concat(makeServiceLink())
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment