From 55c6d737c7df5ec75be0aafa805743855c6d376a Mon Sep 17 00:00:00 2001
From: Peter Czibik
Date: Fri, 20 Sep 2019 17:58:50 +0200
Subject: [PATCH] Task_6_Start
---
src/gateway/index.js | 15 +++++++++++++++
src/server/gqlSchema.js | 35 ++++++++++++++++++++++++----------
src/server/index.js | 14 ++++++++------
src/weatherServer/gqlSchema.js | 24 +++++++++++++++++++++++
4 files changed, 72 insertions(+), 16 deletions(-)
create mode 100644 src/gateway/index.js
create mode 100644 src/weatherServer/gqlSchema.js
diff --git a/src/gateway/index.js b/src/gateway/index.js
new file mode 100644
index 0000000..5d3a7a5
--- /dev/null
+++ b/src/gateway/index.js
@@ -0,0 +1,15 @@
+const { ApolloGateway } = require('@apollo/gateway');
+const { ApolloServer } = require('apollo-server');
+
+const gateway = new ApolloGateway({
+ serviceList: [
+ // TODO (5) list locally running services
+ ],
+});
+
+const server = new ApolloServer({
+ gateway,
+ subscriptions: false,
+});
+
+server.listen(3000);
diff --git a/src/server/gqlSchema.js b/src/server/gqlSchema.js
index 8ed7899..7b1e8c4 100644
--- a/src/server/gqlSchema.js
+++ b/src/server/gqlSchema.js
@@ -1,13 +1,15 @@
-
-const { buildSchema } = require('graphql');
+const { gql } = require('apollo-server');
const { getHello, resolveQuery } = require('./fetcher');
-const schema = buildSchema(`
+// TODO (3) update the user entity with the weather field and add an external reference
+// to the Weather entity
+const typeDefs = gql`
enum OrderDirection {
asc
desc
}
+
type PageInfo {
hasNextPage: Boolean
hasPreviousPage: Boolean
@@ -20,19 +22,24 @@ const schema = buildSchema(`
username
email
}
+
input UserFieldOrder {
field: UserArgField
direction: OrderDirection
}
+
type User {
id: ID
name: String
username: String
email: String
+ location: String
}
+
type UserEdge {
node: User
}
+
type UserConnection {
pageInfo: PageInfo
edges: [UserEdge]
@@ -46,10 +53,12 @@ const schema = buildSchema(`
author
timestamp
}
+
input PostFieldOrder {
field: PostArgField
direction: OrderDirection
}
+
type Post {
id: ID
title: String
@@ -58,9 +67,11 @@ const schema = buildSchema(`
author: String
timestamp: String
}
+
type PostEdge {
node: Post
}
+
type PostConnection {
pageInfo: PageInfo
edges: [PostEdge]
@@ -71,15 +82,19 @@ const schema = buildSchema(`
users(limit: Int, offset: Int, order: UserFieldOrder): UserConnection
posts(limit: Int, offset: Int, order: PostFieldOrder): PostConnection
}
-`);
+`;
-const rootValue = {
- hello: () => getHello(),
- users: (args) => resolveQuery({ table: 'users', args }),
- posts: (args) => resolveQuery({ table: 'posts', args }),
+// TODO (4) define the new root level entry for the User entity and resolve weather there with the
+// parameters from the weather schema
+const resolvers = {
+ Query: {
+ hello: () => getHello(),
+ users: (args) => resolveQuery({ table: 'users', args: args || {} }),
+ posts: (args) => resolveQuery({ table: 'posts', args: args || {} }),
+ },
};
module.exports = {
- schema,
- rootValue,
+ typeDefs,
+ resolvers,
};
diff --git a/src/server/index.js b/src/server/index.js
index 6f3586f..bd01e20 100644
--- a/src/server/index.js
+++ b/src/server/index.js
@@ -2,17 +2,18 @@ const express = require('express');
const graphqlHTTP = require('express-graphql');
const helmet = require('helmet');
const cors = require('cors');
-const { mergeSchemas } = require('graphql-tools');
-const pokemonSchema = require('./pokemonSchema');
+// const { mergeSchemas } = require('graphql-tools');
+// const pokemonSchema = require('./pokemonSchema');
+const { makeExecutableSchema } = require('graphql-tools');
const db = require('./db');
// Graphql Types - cut rootValue from graphqlHTTP when used
-const schema = require('./schema');
+// const schema = require('./schema');
const config = require('./config');
// Raw Graphql schema language - add rootValue from graphqlHTTP when used
-// const { schema, rootValue } = require('./gqlSchema');
+const { typeDefs, resolvers } = require('./gqlSchema');
const app = express();
@@ -27,8 +28,9 @@ app.use(cors({
}));
app.use('/graphql', graphqlHTTP(async () => ({
- schema: mergeSchemas({
- schemas: [schema, await pokemonSchema()],
+ schema: makeExecutableSchema({
+ typeDefs,
+ resolvers,
}),
// rootValue,
graphiql: true,
diff --git a/src/weatherServer/gqlSchema.js b/src/weatherServer/gqlSchema.js
new file mode 100644
index 0000000..9b21972
--- /dev/null
+++ b/src/weatherServer/gqlSchema.js
@@ -0,0 +1,24 @@
+const { gql } = require('apollo-server');
+
+const { getWeather } = require('./fetcher');
+
+// TODO (1) write the new weather schema
+const typeDefs = gql`
+ extend type Query {
+ weather(location: String!): Weather
+ }
+`;
+
+const resolvers = {
+ Query: {
+ weather(_, args) {
+ return getWeather(args);
+ },
+ },
+ // TODO (2) resolve reference on the root level
+};
+
+module.exports = {
+ typeDefs,
+ resolvers,
+};