Skip to content

Commit 28688a5

Browse files
committed
Task_6_Start
1 parent 51b048e commit 28688a5

File tree

4 files changed

+72
-16
lines changed

4 files changed

+72
-16
lines changed

src/gateway/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { ApolloGateway } = require('@apollo/gateway');
2+
const { ApolloServer } = require('apollo-server');
3+
4+
const gateway = new ApolloGateway({
5+
serviceList: [
6+
// TODO (5) list locally running services
7+
],
8+
});
9+
10+
const server = new ApolloServer({
11+
gateway,
12+
subscriptions: false,
13+
});
14+
15+
server.listen(3000);

src/server/gqlSchema.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
2-
const { buildSchema } = require('graphql');
1+
const { gql } = require('apollo-server');
32

43
const { getHello, resolveQuery } = require('./fetcher');
54

6-
const schema = buildSchema(`
5+
// TODO (3) update the user entity with the weather field and add an external reference
6+
// to the Weather entity
7+
const typeDefs = gql`
78
enum OrderDirection {
89
asc
910
desc
1011
}
12+
1113
type PageInfo {
1214
hasNextPage: Boolean
1315
hasPreviousPage: Boolean
@@ -20,19 +22,24 @@ const schema = buildSchema(`
2022
username
2123
email
2224
}
25+
2326
input UserFieldOrder {
2427
field: UserArgField
2528
direction: OrderDirection
2629
}
30+
2731
type User {
2832
id: ID
2933
name: String
3034
username: String
3135
email: String
36+
location: String
3237
}
38+
3339
type UserEdge {
3440
node: User
3541
}
42+
3643
type UserConnection {
3744
pageInfo: PageInfo
3845
edges: [UserEdge]
@@ -46,10 +53,12 @@ const schema = buildSchema(`
4653
author
4754
timestamp
4855
}
56+
4957
input PostFieldOrder {
5058
field: PostArgField
5159
direction: OrderDirection
5260
}
61+
5362
type Post {
5463
id: ID
5564
title: String
@@ -58,9 +67,11 @@ const schema = buildSchema(`
5867
author: String
5968
timestamp: String
6069
}
70+
6171
type PostEdge {
6272
node: Post
6373
}
74+
6475
type PostConnection {
6576
pageInfo: PageInfo
6677
edges: [PostEdge]
@@ -71,15 +82,19 @@ const schema = buildSchema(`
7182
users(limit: Int, offset: Int, order: UserFieldOrder): UserConnection
7283
posts(limit: Int, offset: Int, order: PostFieldOrder): PostConnection
7384
}
74-
`);
85+
`;
7586

76-
const rootValue = {
77-
hello: () => getHello(),
78-
users: (args) => resolveQuery({ table: 'users', args }),
79-
posts: (args) => resolveQuery({ table: 'posts', args }),
87+
// TODO (4) define the new root level entry for the User entity and resolve weather there with the
88+
// parameters from the weather schema
89+
const resolvers = {
90+
Query: {
91+
hello: () => getHello(),
92+
users: (args) => resolveQuery({ table: 'users', args: args || {} }),
93+
posts: (args) => resolveQuery({ table: 'posts', args: args || {} }),
94+
},
8095
};
8196

8297
module.exports = {
83-
schema,
84-
rootValue,
98+
typeDefs,
99+
resolvers,
85100
};

src/server/index.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ const express = require('express');
22
const graphqlHTTP = require('express-graphql');
33
const helmet = require('helmet');
44
const cors = require('cors');
5-
const { mergeSchemas } = require('graphql-tools');
6-
const pokemonSchema = require('./pokemonSchema');
5+
// const { mergeSchemas } = require('graphql-tools');
6+
// const pokemonSchema = require('./pokemonSchema');
7+
const { makeExecutableSchema } = require('graphql-tools');
78

89
const db = require('./db');
910

1011
// Graphql Types - cut rootValue from graphqlHTTP when used
11-
const schema = require('./schema');
12+
// const schema = require('./schema');
1213
const config = require('./config');
1314

1415
// Raw Graphql schema language - add rootValue from graphqlHTTP when used
15-
// const { schema, rootValue } = require('./gqlSchema');
16+
const { typeDefs, resolvers } = require('./gqlSchema');
1617

1718
const app = express();
1819

@@ -27,8 +28,9 @@ app.use(cors({
2728
}));
2829

2930
app.use('/graphql', graphqlHTTP(async () => ({
30-
schema: mergeSchemas({
31-
schemas: [schema, await pokemonSchema()],
31+
schema: makeExecutableSchema({
32+
typeDefs,
33+
resolvers,
3234
}),
3335
// rootValue,
3436
graphiql: true,

src/weatherServer/gqlSchema.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { gql } = require('apollo-server');
2+
3+
const { getWeather } = require('./fetcher');
4+
5+
// TODO (1) write the new weather schema
6+
const typeDefs = gql`
7+
extend type Query {
8+
weather(location: String!): Weather
9+
}
10+
`;
11+
12+
const resolvers = {
13+
Query: {
14+
weather(_, args) {
15+
return getWeather(args);
16+
},
17+
},
18+
// TODO (2) resolve reference on the root level
19+
};
20+
21+
module.exports = {
22+
typeDefs,
23+
resolvers,
24+
};

0 commit comments

Comments
 (0)