From cfbbcf828ae62d96875e33c226ccc49de3d7b8c9 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Tue, 28 Jul 2020 12:05:13 +0200 Subject: [PATCH] Add server error display --- src/components/App.tsx | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 260c7a1..72512d4 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -25,7 +25,10 @@ const QUERY_ARTISTS = gql` export default function App() { const [inputValue, setInputValue] = useState(''); - const [getArtists, { data }] = useLazyQuery(QUERY_ARTISTS); + const [ + getArtists, + { data, error, loading, variables }, + ] = useLazyQuery(QUERY_ARTISTS); const [artists, setArtists] = useState([]); const [suggestions, setSuggestions] = useState([]); const [selected, setSelected] = useState(false); @@ -86,7 +89,7 @@ export default function App() { }; useEffect(() => { - if (data && data.queryArtists !== []) { + if (data && data.queryArtists && data.queryArtists !== []) { // Limit artists to 5 const updatedArtists = data.queryArtists.slice(0, 5); const updatedSuggestions: string[] = updatedArtists.map( @@ -98,6 +101,18 @@ export default function App() { } }, [data]); + // Create flag to filter "No search query" errors + let errorNoSearchQuery = false; + if (error && error.graphQLErrors) { + error.graphQLErrors.forEach((error) => { + if (error.message === 'Error: 400: No search query') { + errorNoSearchQuery = true; + } else { + errorNoSearchQuery = false; + } + }); + } + return (
@@ -110,6 +125,15 @@ export default function App() { {selected && ( )} + {error && !errorNoSearchQuery && ( +
+
+						{error.graphQLErrors.map(({ message }, i) => (
+							{message}
+						))}
+					
+
+ )} ); }