import { Readable , readable } from 'svelte/store' ; import { OperationType , GraphQLTaggedNode , createOperationDescriptor , getRequest , Disposable , } from 'relay-runtime' ; import { getRelayEnvironment } from './context' ; import createStore from './createStore' ; export interface QueryResult < TQuery extends OperationType > extends Readable < Promise < TQuery [ 'response' ] > > { } export function getQuery < TQuery extends OperationType > ( queryGql , variables = { } ) { const environment = getRelayEnvironment ( ) ; const dataStore = createStore < Promise < TQuery [ 'response' ] > > ( ( set ) => { const subscriptions = new Set ( ) ; const request = getRequest ( queryGql ) ; const operation = createOperationDescriptor ( request , variables ) ; const promise = environment . execute ( { operation } ) . toPromise ( ) . then ( ( ) => { const snapshot = environment . lookup ( operation . fragment ) ; subscriptions . add ( environment . subscribe ( snapshot , ( newSnapshot ) => { set ( Promise . resolve ( newSnapshot . data ) ) ; } ) ) ; return snapshot . data ; } ) ; set ( promise ) ; subscriptions . add ( environment . retain ( operation ) ) ; return ( ) => { subscriptions . forEach ( ( sub ) => { sub . dispose ( ) ; } ) ; subscriptions . clear ( ) ; } ; } ) ; dataStore . then = ( ) => { throw new Error ( 'It looks like you tried to use the result of `getQuery()` directly in an #await block. Ensure that the `getQuery()` result is prefixed with `$` so that you subscribe to the latest store value. Further reading: https://svelte.dev/tutorial/auto-subscriptions' ) ; } ; return dataStore ; }