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

optionally post-process results before displaying them #50

Merged
merged 3 commits into from
Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ class Example extends Component {
}
```

## Local development

To start the example demo, run

```bash
npm run start:example
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
npm run start:example
yarn start:example

```

And visit http://localhost:5050 in a browser

## License

Apache-2.0 ©
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),
Copy link
Contributor

Choose a reason for hiding this comment

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

When processSearchResult is not defined, the generateFilter function won't receive allResults anymore. Doesn't this break the query without the processing functions?

},
});

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