diff --git a/README.md b/README.md index f033c43..5ddb45d 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,16 @@ class Example extends Component { } ``` +## Local development + +To start the example demo, run + +```bash +yarn start:example +``` + +And visit http://localhost:5050 in a browser + ## License Apache-2.0 © diff --git a/example/App.js b/example/App.js index d676944..1ca3e87 100644 --- a/example/App.js +++ b/example/App.js @@ -41,6 +41,51 @@ class CustomType { `; } +class PostprocessCustomType { + static name = 'MusicComposition'; + + static filters = []; + + static preprocessQuery = query => { + return `(?i).*${query}.*`; + } + + static searchAllQuery = gql` + query($query: String!) { + allResults: ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") { + identifier + name + itemListElement(filter:{name_regexp:$query}) { + identifier + name + } + } + } + `; + + static searchQuery = gql` + query($filter: _ThingInterfaceFilter) { + results: ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") { + identifier + name + itemListElement(filter: $filter, first: 50) { + identifier + name + } + } + } + `; + + static processSearchResult = result => { + // Find the itemListElements of this ItemList, instead of a list of ItemLists + if (Array.isArray(result) && result[0]) { + return result[0].itemListElement; + } + + return []; + } +} + const BlockQuote = ({ children }) => { return (
@@ -126,6 +171,10 @@ const ex6Config = new SearchConfig({ searchTypes: [CustomType, searchTypes.MusicComposition], }); +const ex7Config = new SearchConfig({ + searchTypes: [PostprocessCustomType], +}); + const App = () => { const [production, setProduction] = useState(false); @@ -191,6 +240,10 @@ const App = () => { return
onClick(item)}>MusicComposition: {item.title}
; }} /> +
+ As a developer I want to be able to post-process search results. +
+ Use cases: diff --git a/src/search/SearchConfig.js b/src/search/SearchConfig.js index 1303be7..b0dccaa 100644 --- a/src/search/SearchConfig.js +++ b/src/search/SearchConfig.js @@ -70,22 +70,34 @@ class SearchConfig { const { data: { allResults } } = await client.query({ query : searchType.searchAllQuery, variables: { - query, + query: searchType.preprocessQuery ? searchType.preprocessQuery(query) : query, }, }); + let processedAllResults = allResults; + + if (typeof searchType.processSearchResult === 'function') { + 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), }, }); + let processedResults = results; + + if (typeof searchType.processSearchResult === 'function') { + processedResults = searchType.processSearchResult(results); + } + return { - typename: searchType.name, - total : results.length, - allResults, - results, + typename : searchType.name, + total : processedResults.length, + allResults: processedAllResults, + results : processedResults, }; }