Skip to content

React-query can't handle return data that's undefined (From 204 response returned by openapi-fetch) #2234

Open
@wheelebin

Description

@wheelebin

openapi-react-query version

0.2.5

Description

Summary

I've set up a "standard" implementation of openapi-fetch with openapi-react-query. I have an endpoint that returns a 204 response with no data in the body, and this causes react-query to throw an error due to the data returned from openapi-fetch being undefined.

Caution

Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ["get","/some-endpoint-with-empty-response",{"params":{"query":{"key":"test-widget"}}}]

Workaround

I'm able to work around this by reconfiguring our endpoint to return status 200 instead of 204 and then setting the openapi-fetch parseAs to "stream" via the openapi-react-query options.

This seems to work since the 204 early return conditional that returns undefined gets skipped, and the parseAs: stream skips any data parsing while handling the "200" response.

const testQuery = $api.useQuery("get", "/some-endpoint-with-empty-response", {
    params: { query: { key: "test-widget" } },
    parseAs: "stream"
  });

However, I'm not a fan of this workaround and would like to know if openapi-fetch & openapi-react-query could handle these 204 responses more elegantly.

Research

I found a discussion on GitHub about this issue and also located the commit that changed the return data from an empty object to undefined.

The empty data object would most likely fix my problem. However, since the above commit replaced the empty data object with undefined, I'm curious to know:

  • Is this behavior intended to work with openapi-react-query/react-query?
  • Are there any configurations I might have missed that would prevent undefined values from being returned to react-query?

I was looking around in the source code to see if I could "hook" into the query function before it returns anything (similar to how the onResponse middleware works), but I could not find anything that would support this behavior.

Thanks in advance! :)

Reproduction

  • Setup openapi-fetch, openapi-react-query as the docs state.
  • Setup an API endpoint that returns 204 status code.
  • Call the API endpoint with openapi-react-query
  • This should cause react-query to throw an error due to the return data being undefined once the request has finished.

Expected result

When dealing with a 204 status code, openapi-fetch should return data that is supported by react-query (such as an empty object or null?)

Extra

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-react-queryRelevant to openapi-react-query

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions