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, }; }