From 824773602c83105192710b28e85d269b279e3113 Mon Sep 17 00:00:00 2001 From: Amber Date: Thu, 22 Oct 2020 17:39:24 +0100 Subject: [PATCH 1/3] Write functionning query with argument in utils file Co-authored-by: Glenroy-Terence (Terry) --- src/App.js | 28 +++---------------------- src/utils/SearchQueries.js | 42 ++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 47 deletions(-) diff --git a/src/App.js b/src/App.js index 5008eea..033d637 100644 --- a/src/App.js +++ b/src/App.js @@ -18,33 +18,11 @@ import Dashboard from "./components/Dashboard/Dashboard"; // import Signup from "./components/SignupLogin/Signup"; // import Login from "./components/SignupLogin/Login"; import query from "./utils/SearchQueries"; + export default function App() { React.useEffect(() => { - const user_input = "Jane"; - const userSearchQuery = ` - { - users(where: {full_name: {_ilike: "%e%"}}) { - bio - email - } - }`; - const url = "https://tfb-mlink.herokuapp.com/v1/graphql"; - const options = { - method: "POST", - headers: { - "Content-type": "application/json", - "x-hasura-admin-secret": "secretKey" - }, - body: JSON.stringify({ userSearchQuery }) - }; - - fetch(url, options) - .then((response) => { - if (!response.ok) throw new Error("Request failed"); - return response.json(); - }) - .then((res) => console.log(res)) - .catch(console.error); + const input = "%Doe%"; + query(input); }, []); return ( diff --git a/src/utils/SearchQueries.js b/src/utils/SearchQueries.js index f995bb6..9bfbb16 100644 --- a/src/utils/SearchQueries.js +++ b/src/utils/SearchQueries.js @@ -3,34 +3,32 @@ // return all profiles which contain the inputted string in their name // specialty // medical school +export default (input) => { + const variables = { input: input }; + const query = ` + query SearchByName($input: String!) { + users(where: {full_name: {_ilike: $input}}) { + bio + email + } + }`; -const userSearchQuery = ` -query SearchByName($user_input: String!){ - { - users { - full_name( - where: { full_name: {_ilike: "%$user_input%"}}) - } - } -}`; - -export default (user_input) => { - const url = "https://tfb-mlink.herokuapp.com/v1/graphql"; - const options = { + fetch("https://tfb-mlink.herokuapp.com/v1/graphql", { method: "POST", headers: { - "Content-type": "application/json", - "x-hasura-admin-secret": "secretKey", - variables: { user_input } + "content-type": "application/json", + "x-hasura-admin-secret": "secretKey" }, - body: JSON.stringify({ userSearchQuery, user_input }) - }; - - fetch(url, options) + body: JSON.stringify({ query, variables }) + }) .then((response) => { if (!response.ok) throw new Error("Request failed"); return response.json(); }) - .then(console.log) - .catch(console.error); + .then((json) => { + console.log(json); + }) + .catch((error) => { + console.error(error); + }); }; From e15d9c7e266ef0ed7ee63ae5ad1308a4f987f936 Mon Sep 17 00:00:00 2001 From: Amber Date: Thu, 22 Oct 2020 17:45:18 +0100 Subject: [PATCH 2/3] Add commrchQueries.js o-authored-by: Glenroy-Terence (Terry) --- src/utils/SearchQueries.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utils/SearchQueries.js b/src/utils/SearchQueries.js index 9bfbb16..06dc55d 100644 --- a/src/utils/SearchQueries.js +++ b/src/utils/SearchQueries.js @@ -3,6 +3,8 @@ // return all profiles which contain the inputted string in their name // specialty // medical school + +// Imported as query to App.js export default (input) => { const variables = { input: input }; const query = ` From 1cc3eebac8df52a1aa0690088dddcb81825f2caf Mon Sep 17 00:00:00 2001 From: Amber Date: Thu, 22 Oct 2020 17:48:04 +0100 Subject: [PATCH 3/3] change input variable to work with _ilike query by wrapping in % Co-authored-by: Glenroy-Terence (Terry) --- src/utils/SearchQueries.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/SearchQueries.js b/src/utils/SearchQueries.js index 06dc55d..78f61f8 100644 --- a/src/utils/SearchQueries.js +++ b/src/utils/SearchQueries.js @@ -6,6 +6,7 @@ // Imported as query to App.js export default (input) => { + input = `%${input}%`; const variables = { input: input }; const query = ` query SearchByName($input: String!) {