Last Sync: 2022-11-19 15:30:04
This commit is contained in:
parent
94795c4e77
commit
4a9d9c44dd
4 changed files with 69 additions and 2 deletions
|
@ -112,11 +112,11 @@ We can now [run queries](/Databases/GraphQL/Apollo/Apollo_Client.md#running-a-qu
|
|||
|
||||
## Implementing resolvers
|
||||
|
||||
A resolver is a [function](/Databases/GraphQL/Creating_a_GraphQL_server.md#resolvers) that populates data for a given query. It should have **the same name as the field for the query**. So far we have one query in our schema: `tracksForHome` which returns the tracks to be listed on the home page. We must therefore we must also name our resolver for this query `tracksForHome`.
|
||||
A resolver is a [function](/Trash/Creating_a_GraphQL_server.md#resolvers) that populates data for a given query. It should have **the same name as the field for the query**. So far we have one query in our schema: `tracksForHome` which returns the tracks to be listed on the home page. We must therefore we must also name our resolver for this query `tracksForHome`.
|
||||
|
||||
It can fetch data from any data source or multiple data sources (other servers, databases, REST APIs) and then presents this as a single integrated resouce to the client, matching the shape requested.
|
||||
|
||||
As per the [generic example](/Databases/GraphQL/Creating_a_GraphQL_server.md#resolvers), you write write your resolvers as keys on a `resolvers` object, e.g:
|
||||
As per the [generic example](/Trash/Creating_a_GraphQL_server.md#resolvers), you write write your resolvers as keys on a `resolvers` object, e.g:
|
||||
|
||||
```js
|
||||
const resolvers = {};
|
||||
|
|
|
@ -134,3 +134,25 @@ query track(id: 'xyz'){
|
|||
}
|
||||
}
|
||||
```
|
||||
|
||||
It's not obvious how the resolver should respond to the nested query here since author name is not a field on `Track`, it is a field on `Author`:
|
||||
|
||||
```gql
|
||||
type Track {
|
||||
id: ID!
|
||||
title: String!
|
||||
author: Author!
|
||||
thumbnail: String
|
||||
length: Int
|
||||
modulesCount: Int
|
||||
description: String
|
||||
numberOfViews: Int
|
||||
modules: [Module!]!
|
||||
}
|
||||
|
||||
type Author {
|
||||
id: ID!
|
||||
name: String!
|
||||
photo: String
|
||||
}
|
||||
```
|
||||
|
|
45
Databases/GraphQL/Using_GraphQL_with_Node.md
Normal file
45
Databases/GraphQL/Using_GraphQL_with_Node.md
Normal file
|
@ -0,0 +1,45 @@
|
|||
---
|
||||
title: Using GraphQL with Node.js
|
||||
categories:
|
||||
- Databases
|
||||
tags: [graph-ql, node-js]
|
||||
---
|
||||
|
||||
# Using GraphQL with Node.js
|
||||
|
||||
## Create a basic Express server
|
||||
|
||||
First we create a basic server in Node using Express:
|
||||
|
||||
```js
|
||||
import express from "express";
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
res.send("Graph QL test project");
|
||||
});
|
||||
|
||||
app.listen(8080, () =>
|
||||
console.log("Running server on port localhost:8080/graphql")
|
||||
);
|
||||
```
|
||||
|
||||
## Add GraphQL as middlewear
|
||||
|
||||
Next we introduce GraphQL as a piece of Node.js [middlewear](/Programming_Languages/Node/Architecture/Middleware.md), with the `app.use()` method.
|
||||
|
||||
```js
|
||||
import { graphqlHTTP } from "express-graphql";
|
||||
|
||||
app.use(
|
||||
"/graphql",
|
||||
graphqlHTTP({
|
||||
schema: schema,
|
||||
rootValue: resolvers,
|
||||
graphiql: true,
|
||||
})
|
||||
);
|
||||
```
|
||||
|
||||
- `schema` is a reference to our GraphQL schema - the structure of the fields that define our server.
|
||||
- `rootValue` is a reference to our resolvers.
|
||||
- `graphiql` is the GUI tool that will be served from the GraphQL endpoint at `localhost:8080/graphql`. This tool enables us to interrogate our data using the defined schema and see what data we would get back from frontend queries.
|
Loading…
Add table
Reference in a new issue