From 6d86539fb39e42b3f3d7c97f3df1077cd7e81f60 Mon Sep 17 00:00:00 2001 From: Alastair Porter Date: Fri, 23 Apr 2021 18:33:59 +0200 Subject: [PATCH 1/3] chore(project): add example app instructions to readme --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index f033c43..ddaa543 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,16 @@ class Example extends Component { } ``` +## Local development + +To start the example demo, run + +```bash +npm run start:example +``` + +And visit http://localhost:5050 in a browser + ## License Apache-2.0 © From 7b64b3cf67785783bdbf1ef6024bf364cba87891 Mon Sep 17 00:00:00 2001 From: Alastair Porter Date: Fri, 23 Apr 2021 18:35:01 +0200 Subject: [PATCH 2/3] feat(project): postprocess results --- example/App.js | 55 ++++++++++++++++++++++++++++++++++++++ src/search/SearchConfig.js | 18 ++++++++----- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/example/App.js b/example/App.js index d676944..9ff32e1 100644 --- a/example/App.js +++ b/example/App.js @@ -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 (
@@ -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); @@ -191,6 +242,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..4137b4e 100644 --- a/src/search/SearchConfig.js +++ b/src/search/SearchConfig.js @@ -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, }; } From 61895f066c9c364e6ef5dc7f23827af0c78b31ff Mon Sep 17 00:00:00 2001 From: Christiaan Scheermeijer Date: Fri, 30 Apr 2021 10:23:51 +0200 Subject: [PATCH 3/3] chore(project): fix processSearchResults function --- README.md | 2 +- example/App.js | 12 +++++------- src/search/SearchConfig.js | 14 ++++++++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ddaa543..5ddb45d 100644 --- a/README.md +++ b/README.md @@ -230,7 +230,7 @@ class Example extends Component { To start the example demo, run ```bash -npm run start:example +yarn start:example ``` And visit http://localhost:5050 in a browser diff --git a/example/App.js b/example/App.js index 9ff32e1..1ca3e87 100644 --- a/example/App.js +++ b/example/App.js @@ -52,7 +52,7 @@ class PostprocessCustomType { static searchAllQuery = gql` query($query: String!) { - ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") { + allResults: ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") { identifier name itemListElement(filter:{name_regexp:$query}) { @@ -65,10 +65,10 @@ class PostprocessCustomType { static searchQuery = gql` query($filter: _ThingInterfaceFilter) { - ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") { + results: ItemList(identifier:"e91489d7-a776-40dd-8abf-0c934922bd99") { identifier name - itemListElement(filter: $filter) { + itemListElement(filter: $filter, first: 50) { identifier name } @@ -78,10 +78,8 @@ class PostprocessCustomType { 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; - } + if (Array.isArray(result) && result[0]) { + return result[0].itemListElement; } return []; diff --git a/src/search/SearchConfig.js b/src/search/SearchConfig.js index 4137b4e..b0dccaa 100644 --- a/src/search/SearchConfig.js +++ b/src/search/SearchConfig.js @@ -74,9 +74,9 @@ class SearchConfig { }, }); - let processedAllResults; + let processedAllResults = allResults; - if (searchType.processSearchResult) { + if (typeof searchType.processSearchResult === 'function') { processedAllResults = searchType.processSearchResult(allResults); } @@ -87,11 +87,17 @@ class SearchConfig { }, }); + let processedResults = results; + + if (typeof searchType.processSearchResult === 'function') { + processedResults = searchType.processSearchResult(results); + } + return { typename : searchType.name, - total : searchType.processSearchResult ? searchType.processSearchResult(results).length : results.length, + total : processedResults.length, allResults: processedAllResults, - results : searchType.processSearchResult ? searchType.processSearchResult(results) : results, + results : processedResults, }; }