Search code examples
reactjsapolloreact-apolloapollo-client

apollo-client used to make multiple requests to make a real-time search


We're developing a search engine inside an app with Apollo and we do not know exactly how to develop a real-time search engine that makes a request to the server on every keyboard press.

On the documentation it says that we must use the new <Query /> component, but I see that this case mostly fits with firing a manual query: https://www.apollographql.com/docs/react/essentials/queries.html#manual-query

I don't know if I'm correct, or maybe we should use it in another way.

thanks!


Solution

  • As it is said at a link you shared, if you wanted to delay firing your query until the user performs an action (your case), such as clicking on a button, you want to use an ApolloConsumer component and directly call client.query() instead.

    Query component can't be used in this situation because when React mounts a Query component, Apollo Client automatically fires off your query during rendering.