Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/graphql-toolset / src / toGraphqlModuleTest.ts
Size: Mime:
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 || {})
    },
  })
}