From 4a9d9c44dd7482ac9e912bb3266062939172c505 Mon Sep 17 00:00:00 2001 From: tactonbishop Date: Sat, 19 Nov 2022 15:30:04 +0000 Subject: [PATCH] Last Sync: 2022-11-19 15:30:04 --- Databases/GraphQL/Apollo/Apollo_Server.md | 4 +- .../Using_arguments_with_Apollo_Client.md | 22 +++++++++ Databases/GraphQL/Using_GraphQL_with_Node.md | 45 +++++++++++++++++++ .../Creating_a_GraphQL_server.md | 0 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 Databases/GraphQL/Using_GraphQL_with_Node.md rename {Databases/GraphQL => Trash}/Creating_a_GraphQL_server.md (100%) diff --git a/Databases/GraphQL/Apollo/Apollo_Server.md b/Databases/GraphQL/Apollo/Apollo_Server.md index c5bf8b7..6b935e4 100644 --- a/Databases/GraphQL/Apollo/Apollo_Server.md +++ b/Databases/GraphQL/Apollo/Apollo_Server.md @@ -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 = {}; diff --git a/Databases/GraphQL/Apollo/Using_arguments_with_Apollo_Client.md b/Databases/GraphQL/Apollo/Using_arguments_with_Apollo_Client.md index bde47f0..928f835 100644 --- a/Databases/GraphQL/Apollo/Using_arguments_with_Apollo_Client.md +++ b/Databases/GraphQL/Apollo/Using_arguments_with_Apollo_Client.md @@ -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 +} +``` diff --git a/Databases/GraphQL/Using_GraphQL_with_Node.md b/Databases/GraphQL/Using_GraphQL_with_Node.md new file mode 100644 index 0000000..f959463 --- /dev/null +++ b/Databases/GraphQL/Using_GraphQL_with_Node.md @@ -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. diff --git a/Databases/GraphQL/Creating_a_GraphQL_server.md b/Trash/Creating_a_GraphQL_server.md similarity index 100% rename from Databases/GraphQL/Creating_a_GraphQL_server.md rename to Trash/Creating_a_GraphQL_server.md