import { GraphQLFormattedError } from 'graphql' ; import { getExtensionId } from '@finch-graphql/browser-polyfill' ; import { queryApi } from '../client' ; import { FinchQueryOptions } from '@finch-graphql/types' ; import { FinchDocumentEventNames , FinchRequestEvent } from './types' ; param event param data param errors export const sendResponse = ( requestId , data , errors ) => { window . postMessage ( { type : FinchDocumentEventNames . Response , data , errors , requestId } , '*' ) ; } ; param options returns export const listenForDocumentQueries = ( options ? ) => { const onMessage = async ( event ) => { if ( ! event . data . type || event . data . type !== FinchDocumentEventNames . Request || event . data . extensionId !== getExtensionId ( ) ) { return true ; } const resp = await queryApi ( event . data . query , event . data . variables , { ... options , external : true } ) ; sendResponse ( event . data . requestId , resp . data , resp . errors ) ; } ; window . addEventListener ( 'message' , onMessage ) ; document . body . setAttribute ( 'data-finch-listener' , `${ Date . now ( ) }` ) ; return ( ) => { window . removeEventListener ( 'message' , onMessage ) ; document . body . removeAttribute ( 'data-finch-listener' ) ; } ; } ;