diff --git a/src/RelayEnvironmentProvider.tsx b/src/RelayEnvironmentProvider.tsx index 127fd681..f16630c5 100644 --- a/src/RelayEnvironmentProvider.tsx +++ b/src/RelayEnvironmentProvider.tsx @@ -2,10 +2,9 @@ import * as React from 'react'; import { Environment } from 'relay-runtime'; import { ReactRelayContext } from './ReactRelayContext'; // eslint-disable-line @typescript-eslint/no-unused-vars -export const RelayEnvironmentProvider = function(props: { - children: React.ReactNode; - environment: Environment; -}): JSX.Element { +export const RelayEnvironmentProvider = function< + TEnvironment extends Environment = Environment +>(props: { children: React.ReactNode; environment: TEnvironment }): JSX.Element { const context = React.useMemo(() => ({ environment: props.environment }), [props.environment]); return ( {props.children} diff --git a/src/RelayHooksType.ts b/src/RelayHooksType.ts index 58cf307d..a8855716 100644 --- a/src/RelayHooksType.ts +++ b/src/RelayHooksType.ts @@ -162,14 +162,17 @@ export type PaginationData = { getFragmentVariables: Function; }; -export type LoadQuery = { +export type LoadQuery< + TOperationType extends OperationType = OperationType, + TEnvironment extends IEnvironment = IEnvironment +> = { next: ( - environment: IEnvironment, + environment: TEnvironment, gqlQuery: GraphQLTaggedNode, variables?: TOperationType['variables'], options?: QueryOptions, ) => Promise; subscribe: (callback: (value: any) => any) => () => void; - getValue: (environment?: IEnvironment) => RenderProps | Promise; + getValue: (environment?: TEnvironment) => RenderProps | Promise; dispose: () => void; }; diff --git a/src/loadQuery.ts b/src/loadQuery.ts index 54c71311..e080afb8 100644 --- a/src/loadQuery.ts +++ b/src/loadQuery.ts @@ -1,11 +1,25 @@ import * as areEqual from 'fbjs/lib/areEqual'; -import { GraphQLTaggedNode, OperationType, IEnvironment, isPromise } from 'relay-runtime'; +import { + GraphQLTaggedNode, + OperationType, + IEnvironment, + isPromise, + OperationDescriptor, + Disposable, +} from 'relay-runtime'; import { QueryFetcher } from './QueryFetcher'; import { RenderProps, QueryOptions, LoadQuery } from './RelayHooksType'; import { createOperation } from './Utils'; -const internalLoadQuery = ( +export const internalLoadQuery = ( promise = false, + queryExecute = ( + queryFetcher: QueryFetcher, + environment: IEnvironment, + query: OperationDescriptor, + options: QueryOptions, + retain?: (environment, query) => Disposable, + ): RenderProps => queryFetcher.execute(environment, query, options, retain), ): LoadQuery => { let data: RenderProps | null | Promise = null; let listener = undefined; @@ -47,7 +61,7 @@ const internalLoadQuery = prev.query = createOperation(gqlQuery, prev.variables); } const execute = (): void => { - data = queryFetcher.execute(prev.environment, prev.query, prev.options); + data = queryExecute(queryFetcher, prev.environment, prev.query, prev.options); listener && listener(data); }; diff --git a/src/useRelayEnvironment.ts b/src/useRelayEnvironment.ts index 6668c2a2..828529e0 100644 --- a/src/useRelayEnvironment.ts +++ b/src/useRelayEnvironment.ts @@ -2,7 +2,9 @@ import * as React from 'react'; import { IEnvironment } from 'relay-runtime'; import { ReactRelayContext } from './ReactRelayContext'; -export function useRelayEnvironment(): IEnvironment { +export function useRelayEnvironment< + TEnvironment extends IEnvironment = IEnvironment +>(): TEnvironment { const { environment } = React.useContext(ReactRelayContext); return environment; }