import { SubscriptionHandshakeLink , CONTROL_EVENTS_KEY } from "./subscription-handshake-link" ; import { ApolloLink , Observable } from "@apollo/client/core" ; import { createHttpLink } from "@apollo/client/link/http" ; import { getMainDefinition } from "@apollo/client/utilities" ; import { NonTerminatingLink } from "./non-terminating-link" ; import type { OperationDefinitionNode } from "graphql" ; import { AppSyncRealTimeSubscriptionHandshakeLink } from "./realtime-subscription-handshake-link" ; import { UrlInfo } from "./types" ; function createSubscriptionHandshakeLink ( args , resultsFetcherLink ? ) ; function createSubscriptionHandshakeLink ( url , resultsFetcherLink ? ) ; function createSubscriptionHandshakeLink ( infoOrUrl , theResultsFetcherLink ? ) { let resultsFetcherLink , subscriptionLinks ; if ( typeof infoOrUrl === "string" ) { resultsFetcherLink = theResultsFetcherLink || createHttpLink ( { uri : infoOrUrl } ) ; subscriptionLinks = ApolloLink . from ( [ new NonTerminatingLink ( "controlMessages" , { link : new ApolloLink ( ( operation , _forward ) => new Observable < any > ( observer => { const { variables : { [ CONTROL_EVENTS_KEY ] : controlEvents , ... variables } } = operation ; if ( typeof controlEvents !== "undefined" ) { operation . variables = variables ; } observer . next ( { [ CONTROL_EVENTS_KEY ] : controlEvents } ) ; return ( ) => { } ; } ) ) } ) , new NonTerminatingLink ( "subsInfo" , { link : resultsFetcherLink } ) , new SubscriptionHandshakeLink ( "subsInfo" ) ] ) ; } else { const { url } = infoOrUrl ; resultsFetcherLink = theResultsFetcherLink || createHttpLink ( { uri : url } ) ; subscriptionLinks = new AppSyncRealTimeSubscriptionHandshakeLink ( infoOrUrl ) ; } return ApolloLink . split ( operation => { const { query } = operation ; const { kind , operation : graphqlOperation } = getMainDefinition ( query ) as OperationDefinitionNode ; const isSubscription = kind === "OperationDefinition" && graphqlOperation === "subscription" ; return isSubscription ; } , subscriptionLinks , resultsFetcherLink ) ; } export { CONTROL_EVENTS_KEY , createSubscriptionHandshakeLink } ;