Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
feat(project): postprocess results
Browse files Browse the repository at this point in the history
  • Loading branch information
alastair committed Apr 23, 2021
1 parent 6d86539 commit 7b64b3c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
55 changes: 55 additions & 0 deletions example/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,53 @@ class CustomType {
`;
}

class PostprocessCustomType {
static name = 'MusicComposition';

static filters = [];

static preprocessQuery = query => {
return `(?i).*${query}.*`;
}

static searchAllQuery = gql`
query($query: String!) {
ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") {
identifier
name
itemListElement(filter:{name_regexp:$query}) {
identifier
name
}
}
}
`;

static searchQuery = gql`
query($filter: _ThingInterfaceFilter) {
ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") {
identifier
name
itemListElement(filter: $filter) {
identifier
name
}
}
}
`;

static processSearchResult = result => {
// Find the itemListElements of this ItemList, instead of a list of ItemLists
if (result) {
if (result.data.ItemList) {
return result.data.ItemList[0].itemListElement;
}
}

return [];
}
}

const BlockQuote = ({ children }) => {
return (
<div style={{ backgroundColor: '#fff', borderLeft: `6px solid rgb(63 81 181)`, padding: 16, marginBottom: 8 }}>
Expand Down Expand Up @@ -126,6 +173,10 @@ const ex6Config = new SearchConfig({
searchTypes: [CustomType, searchTypes.MusicComposition],
});

const ex7Config = new SearchConfig({
searchTypes: [PostprocessCustomType],
});

const App = () => {
const [production, setProduction] = useState(false);

Expand Down Expand Up @@ -191,6 +242,10 @@ const App = () => {
return <div onClick={() => onClick(item)}>MusicComposition: {item.title}</div>;
}}
/>
<BlockQuote>
As a developer I want to be able to post-process search results.
</BlockQuote>
<MultiModalComponentSelect config={ex7Config} production={production} />
</Paper>
<Paper style={{ padding: 16, backgroundColor: '#f1f1f1', marginBottom: 64 }} color="red" variant="outlined">
<Typography variant="h6" gutterBottom>Use cases:</Typography>
Expand Down
18 changes: 12 additions & 6 deletions src/search/SearchConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,28 @@ class SearchConfig {
const { data: { allResults } } = await client.query({
query : searchType.searchAllQuery,
variables: {
query,
query: searchType.preprocessQuery ? searchType.preprocessQuery(query) : query,
},
});

let processedAllResults;

if (searchType.processSearchResult) {
processedAllResults = searchType.processSearchResult(allResults);
}

const { data: { results } } = await client.query({
query : searchType.searchQuery,
variables: {
filter: generateFilter(query, allResults, filtersState, this.filter),
filter: generateFilter(query, processedAllResults, filtersState, this.filter),
},
});

return {
typename: searchType.name,
total : results.length,
allResults,
results,
typename : searchType.name,
total : searchType.processSearchResult ? searchType.processSearchResult(results).length : results.length,
allResults: processedAllResults,
results : searchType.processSearchResult ? searchType.processSearchResult(results) : results,
};
}

Expand Down

0 comments on commit 7b64b3c

Please sign in to comment.