import { DocumentNode , GraphQLFormattedError } from 'graphql' ; import gql from 'graphql-tag' ; import { sendMessage } from '@finch-graphql/browser-polyfill' ; import { isListeningOnDocument , queryApiFromDocument } from '../external' ; import { GenericVariables , FinchMessageKey , FinchQueryOptions , } from '@finch-graphql/types' ; import { isDocumentNode } from '../utils' ; param query param variables param messageKey returns export const messageCreator = < Variables extends GenericVariables = { } > ( query , variables , messageKey ? , external ? ) => { return { type : messageKey ?? FinchMessageKey . Generic , query : isDocumentNode ( query ) ? query : gql ( query ) , variables , external } ; } ; param query param variables param options param options . messageKey param options . id param options . external returns export const queryApi = async < Query extends { } = { } , Variables extends GenericVariables = { } > ( query , variables ? , options = { } ) => { const { id : extensionId , messageKey } = options ; const args = [ messageCreator < Variables > ( query , variables , messageKey , options . external ) , ] ; if ( extensionId ) { args . unshift ( extensionId ) ; if ( isListeningOnDocument ( ) ) { return queryApiFromDocument < Query , Variables > ( query , variables , { extensionId } ) ; } } return sendMessage < { data : Query | null ; errors ? : GraphQLFormattedError [ ] ; } > ( ... args ) ; } ;