import { GraphQLError } from "graphql"; import { GraphQLExtension } from "graphql-extensions"; export function correlationIdExtension(): GraphQLExtension { return { willSendResponse(o: any): GraphQLError | any { const { context, graphqlResponse } = o; if (!graphqlResponse.errors || graphqlResponse.errors.length === 0) { return o; } graphqlResponse.errors = graphqlResponse.errors.map((e: GraphQLError) => { const ext = e.extensions || {}; ext.correlationId = context.req.headers['x-correlation-id']; return new GraphQLError( e.message, e.nodes, e.source, e.positions, e.path, e.originalError, ext, ); }); return o; }, } }