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

fix(types): union SearchForFacetValuesResponse type for multipleQueries #1460

Merged
merged 3 commits into from Jun 19, 2023
Merged

fix(types): union SearchForFacetValuesResponse type for multipleQueries #1460

merged 3 commits into from Jun 19, 2023

Conversation

omikader
Copy link
Contributor

Summary

This PR unions SearchForFacetValuesResponse with SearchResponse<TObject> as possible return types for results in the multipleQueries API.

Context

The MultipleQueriesQuery interface states that you can supply either a default or a facet query. This is also confirmed by the Algolia documentation.

However, the MultipleQueriesResponse definition does not include SearchForFacetValuesResponse as a possible return type. This is confusing because the response data from Algolia does in fact include facet responses when requested (see attached image).

image

@codesandbox-ci
Copy link

codesandbox-ci bot commented May 16, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 70eaed3:

Sandbox Source
javascript-client-app Configuration

@shortcuts
Copy link
Member

@Haroenv do you know why we did not made the union in the first place?

@Haroenv
Copy link
Contributor

Haroenv commented Jun 6, 2023

I don't know the history of this, but I believe initially type didn't exist. We'd need to check what the impact on the types of eg InstantSearch for example is though, as I believe almost everything assumes the type is. default

Copy link
Contributor

@Haroenv Haroenv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I feared this was a breaking change, as only MultipleQueriesResponse is changed, this actually does not impact any code used by customers today (only the helper's internals, but that isn't typescript). I'm thus happy with the change after all. Sorry for the delay!

@shortcuts
Copy link
Member

While I feared this was a breaking change, as only MultipleQueriesResponse is changed, this actually does not impact any code used by customers today (only the helper's internals, but that isn't typescript). I'm thus happy with the change after all. Sorry for the delay!

good to know! thanks for double checking

@shortcuts shortcuts enabled auto-merge (squash) June 19, 2023 09:39
@shortcuts shortcuts merged commit 6a786f1 into algolia:master Jun 19, 2023
8 checks passed
@chrisneven
Copy link

@Haroenv fyi, it did impact us in a way:

image

We depend on this typing a lot throughout our codebase

@anjakunkel
Copy link

Indeed, the type change has breaking impact, also on us.

But it can be fixed easily by explicitly typing your searchResult to be either SearchForFacetValuesResponse with SearchResponse<TObject>, depending on your input.

@VictorGosse
Copy link

Hello,

We are indeed facing the same typing issue after the upgrade to 4.18.0. As suggested above we can explicitly type the searchResult (I guess via a as ?).

Couldn't it be possible to improve the typing here by allowing the type to be sent as a generic so that way it could be defined directly when calling multipleQueries and would be type safe.
eg: client.multipleQueries<SearchResponse<TObject>>(...) would force the return type to be SearchResponse<TObject>.
As there is already a generic on multipleQueries maybe it could be enough to use it: if TObject is defined then it'll return SearchResponse<TObject> otherwise it could be both.

Or even better maybe the return type could be inferred based on the data sent in multipleQueries 🤷‍♂️

Thanks :)

@ranman
Copy link

ranman commented Jul 6, 2023

This also broke our search.

How can we import the relevant types from the algoliasearch namespace or do we need to add the @algolia/* packages into the imports?

export const Algolia = {
  async search<T = Record<string, string>[]>(
    query: string,
    params: Record<string, string | number> = {}
  ): Promise<{ hits: T; totalPages: number }> {
    try {
      const queries = BUILDER_MODELS.map((model) => {
        return {
          indexName: model,
          query,
          params: {
            ...params,
            hitsPerPage: HITS_PER_PAGE,
          },
        }
      })

      const res = await client.multipleQueries(queries)

      // Retrieve hits out of results and return
      // @ts-expect-error
      const hits = res?.results?.flatMap((result) => result.hits)
      // @ts-expect-error
      const pages = Math.max(...res.results.map((result) => result.nbPages))
      return { hits: hits as T, totalPages: pages - 1 }
    } catch (e) {
      throw new Error(`Failed to load search data`)
    }
  },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants