Skip to content

Add Support for Request cancellation #251

@wgottschalk

Description

@wgottschalk

I'm implementing autocomplete right now using apollo-link-rest. Everytime the search term changes, apollo client (useQuery) will kick off a new request. However, I don't want to have my server keep doing expensive search ranking for a result that I'm going to ignore so I'd like to cancel the request.

maybe something like?

const MyComponent  = () => {
  const abortController = useRef(new AbortController());
  const [searchTerm, setSearchTerm] = useState("");
  useEffect(() => {
    abortController.current.abort()
    abortController.current = new AbortController()
  }, [searchTerm])

  const { error, loading, data } = useQuery(SearchQuery, {
    variables: { searchTerm }
    context: { fetchOptions: { signal: abortController.current.signal } }
  })
}

All you really have to do is grab this fetchOptions field from context and make it available in requestOptions and everything should workout correctly

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement💡featureFeature: new addition or enhancement to existing solutions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions