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

TanStack Query: Surface custom errors for useQuery() #1508

Open
ryan-rushton opened this issue Dec 24, 2024 · 2 comments
Open

TanStack Query: Surface custom errors for useQuery() #1508

ryan-rushton opened this issue Dec 24, 2024 · 2 comments
Assignees
Labels
feature 🚀 New feature or request prioritized 🚚 This issue has been prioritized and will be worked on soon

Comments

@ryan-rushton
Copy link

ryan-rushton commented Dec 24, 2024

Description

In setting up auth for my application I have noticed that my explicitly defined auth errors are surfaced in queries, but they are in mutations.

This should be solvable as there is a UseQueryOptions type that takes custom errors similar to UseMutationOptions

From the react query types

interface UseQueryOptions<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey> extends OmitKeyof<UseBaseQueryOptions<TQueryFnData, TError, TData, TQueryFnData, TQueryKey>, 'suspense'> {

Examples

Post spec

"/api/auth/{provider}/authenticate": {
      "post": {
        "parameters": [
          {
            "name": "provider",
            "in": "path",
            "description": "The name of the auth provider, should be 'steam'",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/AuthProviderAuthenticationRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK"
          },
          "401": {
            "description": "Unauthorized Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnauthorizedError"
                }
              }
            }
          },
          "500": {
            "description": "Server Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GeneralError"
                }
              }
            }
          }
        }
      }
    },

Generated post

Also note the query here doesn't surface them

export const postApiAuthByProviderAuthenticateOptions = (options: Options<PostApiAuthByProviderAuthenticateData>) => {
  return queryOptions({
    queryFn: async ({ queryKey, signal }) => {
      const { data } = await postApiAuthByProviderAuthenticate({
        ...options,
        ...queryKey[0],
        signal,
        throwOnError: true,
      });
      return data;
    },
    queryKey: postApiAuthByProviderAuthenticateQueryKey(options),
  });
};

export const postApiAuthByProviderAuthenticateMutation = (
  options?: Partial<Options<PostApiAuthByProviderAuthenticateData>>,
) => {
  const mutationOptions: UseMutationOptions<
    unknown,
    AxiosError<PostApiAuthByProviderAuthenticateError>,
    Options<PostApiAuthByProviderAuthenticateData>
  > = {
    mutationFn: async (localOptions) => {
      const { data } = await postApiAuthByProviderAuthenticate({
        ...options,
        ...localOptions,
        throwOnError: true,
      });
      return data;
    },
  };
  return mutationOptions;
};

Get spec

"/api/user": {
      "get": {
        "security": [{ "BearerTokenCookie": [] }],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GetUserResponse"
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/UnauthorizedError"
                }
              }
            }
          },
          "500": {
            "description": "Server Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/GeneralError"
                }
              }
            }
          }
        }
      }
    },

Generated get

export const getApiUserOptions = (options?: Options<GetApiUserData>) => {
  return queryOptions({
    queryFn: async ({ queryKey, signal }) => {
      const { data } = await getApiUser({
        ...options,
        ...queryKey[0],
        signal,
        throwOnError: true,
      });
      return data;
    },
    queryKey: getApiUserQueryKey(options),
  });
};
@ryan-rushton ryan-rushton added the feature 🚀 New feature or request label Dec 24, 2024
@mrlubos mrlubos added the prioritized 🚚 This issue has been prioritized and will be worked on soon label Dec 24, 2024
@mrlubos
Copy link
Member

mrlubos commented Dec 24, 2024

Thanks Ryan, this is a completely valid request, will fix

@ryan-rushton
Copy link
Author

Thanks mate! I might have a go at some issues when I become more familiar but I've just started using it on a hobby project 😁

@mrlubos mrlubos changed the title React query: Surface custom errors for useQuery TanStack Query: Surface custom errors for useQuery() Dec 25, 2024
@mrlubos mrlubos self-assigned this Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature 🚀 New feature or request prioritized 🚚 This issue has been prioritized and will be worked on soon
Projects
None yet
Development

No branches or pull requests

2 participants