Skip to content

Commit

Permalink
Merge pull request #117 from relay-tools/relay-offline-load-query
Browse files Browse the repository at this point in the history
Better compatibility with react-relay-offline
  • Loading branch information
morrys authored Oct 4, 2020
2 parents 063d19b + ca60f6b commit 2b2e325
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
7 changes: 3 additions & 4 deletions src/RelayEnvironmentProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<ReactRelayContext.Provider value={context}>{props.children}</ReactRelayContext.Provider>
Expand Down
9 changes: 6 additions & 3 deletions src/RelayHooksType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,17 @@ export type PaginationData = {
getFragmentVariables: Function;
};

export type LoadQuery<TOperationType extends OperationType = OperationType> = {
export type LoadQuery<
TOperationType extends OperationType = OperationType,
TEnvironment extends IEnvironment = IEnvironment
> = {
next: (
environment: IEnvironment,
environment: TEnvironment,
gqlQuery: GraphQLTaggedNode,
variables?: TOperationType['variables'],
options?: QueryOptions,
) => Promise<void>;
subscribe: (callback: (value: any) => any) => () => void;
getValue: (environment?: IEnvironment) => RenderProps<TOperationType> | Promise<any>;
getValue: (environment?: TEnvironment) => RenderProps<TOperationType> | Promise<any>;
dispose: () => void;
};
20 changes: 17 additions & 3 deletions src/loadQuery.ts
Original file line number Diff line number Diff line change
@@ -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 = <TOperationType extends OperationType = OperationType>(
export const internalLoadQuery = <TOperationType extends OperationType = OperationType>(
promise = false,
queryExecute = (
queryFetcher: QueryFetcher<TOperationType>,
environment: IEnvironment,
query: OperationDescriptor,
options: QueryOptions,
retain?: (environment, query) => Disposable,
): RenderProps<TOperationType> => queryFetcher.execute(environment, query, options, retain),
): LoadQuery<TOperationType> => {
let data: RenderProps<TOperationType> | null | Promise<any> = null;
let listener = undefined;
Expand Down Expand Up @@ -47,7 +61,7 @@ const internalLoadQuery = <TOperationType extends OperationType = OperationType>
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);
};

Expand Down
4 changes: 3 additions & 1 deletion src/useRelayEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 2b2e325

Please sign in to comment.