Last Sync: 2022-11-19 17:30:05
This commit is contained in:
parent
af7caca54b
commit
c6908866dd
7 changed files with 69 additions and 7 deletions
|
@ -2,7 +2,7 @@
|
||||||
title: Apollo Server
|
title: Apollo Server
|
||||||
categories:
|
categories:
|
||||||
- Databases
|
- Databases
|
||||||
tags: [graphql]
|
tags: [graphql, REST, APIs]
|
||||||
---
|
---
|
||||||
|
|
||||||
# Apollo Server
|
# Apollo Server
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
categories:
|
categories:
|
||||||
- Databases
|
- Databases
|
||||||
tags: [mongo_db, node-js, mongoose]
|
tags: [mongo-db, node-js, mongoose]
|
||||||
---
|
---
|
||||||
|
|
||||||
# Adding documents to a collection
|
# Adding documents to a collection
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
categories:
|
categories:
|
||||||
- Databases
|
- Databases
|
||||||
tags: [mongo_db, node-js, mongoose]
|
tags: [mongo-db, node-js, mongoose]
|
||||||
---
|
---
|
||||||
|
|
||||||
# Creating a schema and model
|
# Creating a schema and model
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
categories:
|
categories:
|
||||||
- Databases
|
- Databases
|
||||||
tags: [mongo_db, node-js, mongoose]
|
tags: [mongodb, node-js, mongoose]
|
||||||
---
|
---
|
||||||
|
|
||||||
# Validating Mongoose schemas
|
# Validating Mongoose schemas
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
categories:
|
categories:
|
||||||
- Computer Architecture
|
- Computer Architecture
|
||||||
tags:
|
tags:
|
||||||
- theory-of-omputation
|
- theory-of-computation
|
||||||
- history
|
- history
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
title: Creating a GraphQL server
|
title: Creating a GraphQL server
|
||||||
categories:
|
categories:
|
||||||
- Databases
|
- Databases
|
||||||
tags: [graph-ql]
|
tags: [graphql]
|
||||||
---
|
---
|
||||||
|
|
||||||
# Creating a GraphQL server
|
# Creating a GraphQL server
|
||||||
|
|
Loading…
Add table
Reference in a new issue