From c6908866ddaaca096c754bb0c38fe6338d1a4532 Mon Sep 17 00:00:00 2001 From: tactonbishop Date: Sat, 19 Nov 2022 17:30:05 +0000 Subject: [PATCH] Last Sync: 2022-11-19 17:30:05 --- Databases/GraphQL/Apollo/Apollo_Server.md | 2 +- .../Using_arguments_with_Apollo_Client.md | 64 ++++++++++++++++++- .../Adding_documents_to_a_collection.md | 2 +- .../MongoDB/Creating_a_schema_and_model.md | 2 +- .../MongoDB/Validating_Mongoose_schemas.md | 2 +- Theory_of_Computation/Defining_a_computer.md | 2 +- Trash/Creating_a_GraphQL_server.md | 2 +- 7 files changed, 69 insertions(+), 7 deletions(-) diff --git a/Databases/GraphQL/Apollo/Apollo_Server.md b/Databases/GraphQL/Apollo/Apollo_Server.md index 31ceed1..4037e0e 100644 --- a/Databases/GraphQL/Apollo/Apollo_Server.md +++ b/Databases/GraphQL/Apollo/Apollo_Server.md @@ -2,7 +2,7 @@ title: Apollo Server categories: - Databases -tags: [graphql] +tags: [graphql, REST, APIs] --- # Apollo Server diff --git a/Databases/GraphQL/Apollo/Using_arguments_with_Apollo_Client.md b/Databases/GraphQL/Apollo/Using_arguments_with_Apollo_Client.md index 7396218..080b872 100644 --- a/Databases/GraphQL/Apollo/Using_arguments_with_Apollo_Client.md +++ b/Databases/GraphQL/Apollo/Using_arguments_with_Apollo_Client.md @@ -219,4 +219,66 @@ query track(id: 'xyz'){ } ``` -The ' +The `id` parameter would be used by the `modules` resolver to return the array of the `Modules` type. + +## Variables in arguments + +Instead of writing the following within our query constants on the client-side: + +```js +query GetTrack { + track(id: 'xyz'){ + title + } +} +``` + +We can make the code more reusable by using variables instead of the hardcoded `id` argument: + +```js +query GetTrack($trackId: ID!) { + track(id: $trackId){ + title + } +} +``` + +This way we do not need to write a new query constant every time we want to return a specific track. + +## Send query with arguments using Apollo `useQuery` + +We can now write a proper query using the [useQuery hook](/Databases/GraphQL/Apollo/Apollo_Client.md#usequery-hook) from Apollo Client, with variables. + +First define our query constant: + +```js +export const GET_TRACK = gql` + query GetTrack($trackId: ID!) { + track(id: $trackId) { + id + title + author { + name + } + description + modules { + id + title + length + } + } + } +`; +``` + +Then to employ in React: + +```jsx +const trackId = "xyz"; + +const { loading, error, data } = useQuery(GET_TRACK, { + variables: trackId, +}); +``` + +Note that in contrast to the [simple example](/Databases/GraphQL/Apollo/Apollo_Client.md#query-constants) diff --git a/Databases/MongoDB/Adding_documents_to_a_collection.md b/Databases/MongoDB/Adding_documents_to_a_collection.md index 469e24c..5f104b1 100644 --- a/Databases/MongoDB/Adding_documents_to_a_collection.md +++ b/Databases/MongoDB/Adding_documents_to_a_collection.md @@ -1,7 +1,7 @@ --- categories: - Databases -tags: [mongo_db, node-js, mongoose] +tags: [mongo-db, node-js, mongoose] --- # Adding documents to a collection diff --git a/Databases/MongoDB/Creating_a_schema_and_model.md b/Databases/MongoDB/Creating_a_schema_and_model.md index f753aa9..ad94983 100644 --- a/Databases/MongoDB/Creating_a_schema_and_model.md +++ b/Databases/MongoDB/Creating_a_schema_and_model.md @@ -1,7 +1,7 @@ --- categories: - Databases -tags: [mongo_db, node-js, mongoose] +tags: [mongo-db, node-js, mongoose] --- # Creating a schema and model diff --git a/Databases/MongoDB/Validating_Mongoose_schemas.md b/Databases/MongoDB/Validating_Mongoose_schemas.md index d965fa6..52ecb94 100644 --- a/Databases/MongoDB/Validating_Mongoose_schemas.md +++ b/Databases/MongoDB/Validating_Mongoose_schemas.md @@ -1,7 +1,7 @@ --- categories: - Databases -tags: [mongo_db, node-js, mongoose] +tags: [mongodb, node-js, mongoose] --- # Validating Mongoose schemas diff --git a/Theory_of_Computation/Defining_a_computer.md b/Theory_of_Computation/Defining_a_computer.md index c46f90d..19798da 100644 --- a/Theory_of_Computation/Defining_a_computer.md +++ b/Theory_of_Computation/Defining_a_computer.md @@ -2,7 +2,7 @@ categories: - Computer Architecture tags: - - theory-of-omputation + - theory-of-computation - history --- diff --git a/Trash/Creating_a_GraphQL_server.md b/Trash/Creating_a_GraphQL_server.md index a630d1a..e8b659d 100644 --- a/Trash/Creating_a_GraphQL_server.md +++ b/Trash/Creating_a_GraphQL_server.md @@ -2,7 +2,7 @@ title: Creating a GraphQL server categories: - Databases -tags: [graph-ql] +tags: [graphql] --- # Creating a GraphQL server