Repository URL to install this package:
|
Version:
0.0.6 ▾
|
import { GraphQLModule } from './GraphQLModule'
import { toTest, ToTestArgs } from './server'
import { toMockExpressRequest, toMockExpressResponse } from './expressMock'
export type ToEndToEndServerArgs =
| (Partial<ToTestArgs> & {
graphqlModule: GraphQLModule<any, any, any>
})
| GraphQLModule<any, any, any>
// createEndToEndClient
export function toGraphqlModuleTest(args: ToEndToEndServerArgs) {
const { graphqlModule, ...remainingArgs } =
args instanceof GraphQLModule ? { graphqlModule: args } : args
const {
mergedContextBuilder,
context,
dataSources,
typeDefs,
resolvers,
schemaDirectives,
} = graphqlModule
const mockRequestInternals = {
headers: new Headers(),
}
return toTest({
mockRequestInternals,
typeDefs,
resolvers,
...remainingArgs,
options: {
schemaDirectives,
context: async apollo => {
const {
// @todo - not `any`
req = toMockExpressRequest(mockRequestInternals as any),
res = toMockExpressResponse(),
} = apollo
// @todo, we'll write a specific test for this
// const correlationId = toCorrelationId(req)
const correlationId =
req.get(
process.env.CORRELATION_ID_NAMESPACE ||
'x-missing-correlation-id-env'
) ||
req.get('x-trace') ||
req.get('x-correlation-id') ||
req.get('x-sk-session-id') ||
req.get('x-session-id')
req.correlationId = correlationId
const asyncContext = await context(apollo)
const baseContext = {
req,
res,
log: req.log,
correlationId,
...asyncContext,
}
const mergedContext = mergedContextBuilder(baseContext)
const merged = { ...baseContext, ...mergedContext }
return merged
},
dataSources: () => {
return dataSources()
},
...(remainingArgs.options || {})
},
})
}