import { DocumentNode , GraphQLFormattedError } from 'graphql' ; import { FinchContextObj } from '../types' ; import { FinchStartMessage , FinchResponseMessage , FinchDevToolsMessageType , } from './types' ; import { FinchPortConnection } from './FinchPortConnection' ; import { AnyFinchMessage , FinchConnectionType } from '@finch-graphql/types' ; /**
 * FinchDevtools is a class that manages external connections from the Finch devtools
 * extension. This has the ability to listen for incoming connections add cleanup of those
 * connections.
 */ export class FinchDevtools extends FinchPortConnection { /**
     * `FinchDevtools.portName` is the exposed port name that the devtools will use for connections
     * to the Finch GraphQL API.
     */ static portName = '_finchDevtools' ; constructor ( { connectionType , messagePortName , messageKey , } ) { super ( { messagePortName : FinchDevtools . portName , external : true } ) ; this . messageListener = async ( msg ) => { switch ( msg . type ) { case FinchDevToolsMessageType . RequestMessageKey : if ( messageKey ) { return { type : FinchDevToolsMessageType . MessageKey , messageKey } ; } break ; case FinchDevToolsMessageType . HealthCheck : return { type : FinchDevToolsMessageType . HealthCheckOk } ; case FinchDevToolsMessageType . RequestConnectionInfo : return { type : FinchDevToolsMessageType . ConnectionInfo , messageKey , messagePortName , connectionType } ; default : break ; } return { } ; } ; } param message broadcast < T extends { } > ( message ) { this . messagePorts . forEach ( port => { port . postMessage ( message ) ; } ) ; } param info returns public startQuery ( { id , query , operationName , variables , context , } ) { this . broadcast < FinchStartMessage > ( { type : FinchDevToolsMessageType . Start , id , query , operationName : operationName ?? 'unknown' , initializedAt : Date . now ( ) , variables , context } ) ; return id ; } param info public queryResponse = async ( { id , timeTaken , response , } ) => { this . broadcast < FinchResponseMessage > ( { type : FinchDevToolsMessageType . Response , id , timeTaken , response } ) ; } ; }