Last active
March 26, 2019 10:26
-
-
Save szaza/12b079129283197036bcf96124b0af10 to your computer and use it in GitHub Desktop.
Introspection example to resolve GraphQL schema stitching error swallowing.
This file contains 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 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