Skip to content

Commit

Permalink
Task_6_Start
Browse files Browse the repository at this point in the history
  • Loading branch information
peteyycz committed Sep 23, 2019
1 parent 7336c75 commit 3e3dc4c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
15 changes: 15 additions & 0 deletions src/gateway/index.js
Original file line number Diff line number Diff line change
@@ -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);
35 changes: 25 additions & 10 deletions src/server/gqlSchema.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]
Expand All @@ -46,10 +53,12 @@ const schema = buildSchema(`
author
timestamp
}
input PostFieldOrder {
field: PostArgField
direction: OrderDirection
}
type Post {
id: ID
title: String
Expand All @@ -58,9 +67,11 @@ const schema = buildSchema(`
author: String
timestamp: String
}
type PostEdge {
node: Post
}
type PostConnection {
pageInfo: PageInfo
edges: [PostEdge]
Expand All @@ -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,
};
14 changes: 8 additions & 6 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions src/weatherServer/gqlSchema.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit 3e3dc4c

Please sign in to comment.