Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usePreloadedQuery, loadQuery & loadLazyQuery #90

Merged
merged 9 commits into from
May 23, 2020
Merged

Conversation

morrys
Copy link
Member

@morrys morrys commented May 12, 2020

this PR introduces usePreloadedQuery, useLoad & useLazyLoad

loadQuery

  • input parameters

same as useQuery + environment

  • output parameters
    • next: ( environment: IEnvironment, gqlQuery: GraphQLTaggedNode, variables?: TOperationType['variables'], options?: QueryOptions, ) => Promise<void>: fetches data. A promise returns to allow the await in case of SSR
    • dispose: () => void: cancel the subscription and dispose of the fetch
    • subscribe: (callback: (value: any) => any) => () => void: used by the usePreloadedQuery
    • getValue: (environment?: IEnvironment) => RenderProps<TOperationType> | Promise<any>: used by the usePreloadedQuery
import {graphql, loadQuery} from 'relay-hooks';
import {environment} from ''./environment';

const query = graphql`
  query AppQuery($id: ID!) {
    user(id: $id) {
      name
    }
  }
`;

const prefetch = loadQuery();
prefetch.next(
  environment,
  query,
  {id: '4'},
  {fetchPolicy: 'store-or-network'},
);
// pass prefetch to usePreloadedQuery()

loadLazyQuery

is the same as loadQuery but must be used with suspense

usePreloadedQuery

  • input parameters

    • loadQuery | loadLazyQuery
  • output parameters

    • same as useQuery
function Component(props) {
  data = usePreloadedQuery(props.prefetched);
  return data;
}

Both the implementation and its use differs from the current implementation in relay-experimental.

In summary, it implements the logic of the BehaviorSubject of rxjs including the subscribe, getValue and the next but with the difference that the next function it is asynchronous and introduces the "dispose".

Through this management it is sufficient to create a single loadQuery to manage multiple routes and avoid using cache maps within the library.

While the dispose function has been designed to manage error cases in order to correctly delete the last route performed.

@morrys morrys merged commit 5b5efb4 into master May 23, 2020
@morrys morrys deleted the use-preloaded-query branch May 23, 2020 19:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant