Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you dynamically control react apollo-client query initiation?

Tags:

react-apollo

A react component wrapped with an apollo-client query will automatically initiate a call to the server for data.

I would like to fire off a request for data only on a specific user input.

You can pass the skip option in the query options - but this means the refetch() function is not provided as a prop to the component; and it appears that the value of skip is not assessed dynamically on prop update.

My use is case is a map component. I only want data for markers to be loaded when the user presses a button, but not on initial component mount or location change.

A code sample below:

// GraphQL wrapping
Explore = graphql(RoutesWithinQuery, {
  options: ({ displayedMapRegion }) => ({
    variables: {
      scope: 'WITHIN',
      targetRegion: mapRegionToGeoRegionInputType(displayedMapRegion)
    },
    skip: ({ targetResource, searchIsAllowedForMapArea }) => {
      const skip = Boolean(!searchIsAllowedForMapArea || targetResource != 'ROUTE');
      return skip;
    },
  }),
  props: ({ ownProps, data: { loading, viewer, refetch }}) => ({
    routes: viewer && viewer.routes ? viewer.routes : [],
    refetch,
    loading
  })
})(Explore);
like image 747
Nick McIntosh Avatar asked Oct 28 '16 01:10

Nick McIntosh


People also ask

How do you connect Apollo Client to React?

To connect Apollo Client to React, you will need to use the ApolloProvider component exported from @apollo/react-hooks . The ApolloProvider is similar to React's Context. Provider . It wraps your React app and places the client on the context, which allows you to access it from anywhere in your component tree.

How do you run GraphQL query in React?

To run a query within a React component, call useQuery and pass it a GraphQL query string. When your component renders, useQuery returns an object from Apollo Client that contains loading , error , and data properties you can use to render your UI.

What are key args in Apollo GraphQL for?

keyArgs function (advanced) You can define a completely different format for a field's storage key by providing a custom function to keyArgs . This function takes the field's arguments and other context as parameters, and it can return any string to use as the storage key (or a dynamically-generated keyArgs array).


1 Answers

To include an HoC based on a condition affected by a props change, you could use branch from recompose.

branch(
  test: (props: Object) => boolean,
  left: HigherOrderComponent,
  right: ?HigherOrderComponent
): HigherOrderComponent

check: https://github.com/acdlite/recompose/blob/master/docs/API.md#branch

For this specific example, would look something like:

const enhance = compose(
  branch(

    // evaluate condition

    ({ targetResource, searchIsAllowedForMapArea }) =>
      Boolean(!searchIsAllowedForMapArea || targetResource != 'ROUTE'),

    // HoC if condition is true

    graphql(RoutesWithinQuery, {
      options: ({ displayedMapRegion }) => ({
        variables: {
          scope: 'WITHIN',
          targetRegion: mapRegionToGeoRegionInputType(displayedMapRegion)
        },
      }),
      props: ({ ownProps, data: { loading, viewer, refetch } }) => ({
        routes: viewer && viewer.routes ? viewer.routes : [],
        refetch,
        loading
      })
    })
  )
);

Explore = enhance(Explore);
like image 122
Pedro Baptista Afonso Avatar answered Oct 07 '22 01:10

Pedro Baptista Afonso