-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathfetch-search-data.ts
More file actions
42 lines (38 loc) · 1.04 KB
/
fetch-search-data.ts
File metadata and controls
42 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { addonFragment, recipeFragment, validateResponse } from '@repo/utils';
import type { Addon, Recipe } from '../types';
import { fetchAddonsQuery, gql } from './fetch-addons-query';
interface SearchData {
partialSearchIntegrations: {
addons: Addon[];
recipes: Recipe[];
};
}
export async function fetchSearchData(query: string) {
try {
const data = await fetchAddonsQuery<SearchData, { query: string }>(
gql`
query Search($query: string!) {
partialSearchIntegrations(query: $query) {
addons {
${addonFragment}
}
recipes {
${recipeFragment}
}
}
}
`,
{
variables: { query },
},
);
validateResponse(
() =>
data.partialSearchIntegrations.addons &&
data.partialSearchIntegrations.recipes,
);
return data.partialSearchIntegrations;
} catch (error) {
throw new Error(`Failed to fetch search data: ${(error as Error).message}`);
}
}