import nock from 'nock' ; import { HEALTH_CHECK_QUERY , SERVICE_DEFINITION_QUERY } from '../..' ; import { SUPERGRAPH_SDL_QUERY } from '../../supergraphManagers/UplinkFetcher/loadSupergraphSdlFromStorage' ; import { getTestingSupergraphSdl } from '../../__tests__/execution-utils' ; import { print } from 'graphql' ; import { Fixture , fixtures as testingFixtures } from 'apollo-federation-integration-testsuite' ; export const graphRef = 'federated-service@current' ; export const apiKey = 'service:federated-service:DD71EBbGmsuh-6suUVDwnA' ; const apiKeyHash = 'dd55a79d467976346d229a7b12b673ce' ; export const mockApolloConfig = { apollo : { key : apiKey , keyHash : apiKeyHash , graphRef , } , } ; function mockSdlQuery ( { url } ) { return nock ( url ) . post ( '/' , { query : SERVICE_DEFINITION_QUERY , } ) ; } export function mockSdlQuerySuccess ( service ) { return mockSdlQuery ( service ) . reply ( 200 , { data : { _service : { sdl : print ( service . typeDefs ) } } , } ) ; } export function mockAllServicesSdlQuerySuccess ( fixtures = testingFixtures ) { return fixtures . map ( ( fixture ) => mockSdlQuerySuccess ( fixture ) ) ; } export function mockServiceHealthCheck ( { url } ) { return nock ( url ) . post ( '/' , { query : HEALTH_CHECK_QUERY , } ) ; } export function mockServiceHealthCheckSuccess ( service ) { return mockServiceHealthCheck ( service ) . reply ( 200 , { data : { __typename : 'Query' } , } ) ; } export function mockAllServicesHealthCheckSuccess ( fixtures = testingFixtures ) { return fixtures . map ( ( fixture ) => mockServiceHealthCheck ( fixture ) . reply ( 200 , { data : { __typename : 'Query' } , } ) ) ; } function gatewayNock ( url ) { const { name , version } = require ( '../../../package.json' ) ; return nock ( url , { reqheaders : { 'apollographql-client-name' : name , 'apollographql-client-version' : version , 'user-agent' : `${ name }/${ version }` , 'content-type' : 'application/json' , } , } ) ; } export const mockCloudConfigUrl1 = 'https://example1.cloud-config-url.com/cloudconfig/' ; export const mockCloudConfigUrl2 = 'https://example2.cloud-config-url.com/cloudconfig/' ; export const mockCloudConfigUrl3 = 'https://example3.cloud-config-url.com/cloudconfig/' ; export const mockOutOfBandReporterUrl = 'https://example.outofbandreporter.com/monitoring/' ; export function mockSupergraphSdlRequestIfAfter ( ifAfter , url = mockCloudConfigUrl1 ) { return gatewayNock ( url ) . post ( '/' , { query : SUPERGRAPH_SDL_QUERY , variables : { ref : graphRef , apiKey : apiKey , ifAfterId : ifAfter , } , } ) ; } export function mockSupergraphSdlRequest ( ifAfter = null , url = mockCloudConfigUrl1 ) { return mockSupergraphSdlRequestIfAfter ( ifAfter , url ) ; } export function mockSupergraphSdlRequestSuccessIfAfter ( ifAfter = null , id = 'originalId-1234' , supergraphSdl = getTestingSupergraphSdl ( ) ) { if ( supergraphSdl == null ) { supergraphSdl = getTestingSupergraphSdl ( ) ; } return mockSupergraphSdlRequestIfAfter ( ifAfter ) . reply ( 200 , JSON . stringify ( { data : { routerConfig : { __typename : 'RouterConfigResult' , id , supergraphSdl , } , } , } ) ) ; } export function mockSupergraphSdlRequestIfAfterUnchanged ( ifAfter = null ) { return mockSupergraphSdlRequestIfAfter ( ifAfter ) . reply ( 200 , JSON . stringify ( { data : { routerConfig : { __typename : 'Unchanged' , } , } , } ) ) ; } export function mockSupergraphSdlRequestSuccess ( ) { return mockSupergraphSdlRequestSuccessIfAfter ( null ) ; } export function mockOutOfBandReportRequest ( ) { return gatewayNock ( mockOutOfBandReporterUrl ) . post ( '/' , ( ) => true ) ; } export function mockOutOfBandReportRequestSuccess ( ) { return mockOutOfBandReportRequest ( ) . reply ( 200 , JSON . stringify ( { data : { reportError : true } , } ) ) ; }