Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: updating build #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions data/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { find, filter } from 'lodash';
import { pubsub } from './subscriptions';
import { PubSub } from 'graphql-subscriptions';

const pubsub = new PubSub();

const authors = [
{ id: 1, firstName: 'Tom', lastName: 'Coleman' },
Expand All @@ -22,20 +24,20 @@ const resolveFunctions = {
},
},
Mutation: {
upvotePost(_, { postId }) {
async upvotePost(_, { postId }) {
const post = find(posts, { id: postId });
if (!post) {
throw new Error(`Couldn't find post with id ${postId}`);
}
post.votes += 1;
pubsub.publish('postUpvoted', post);
pubsub.publish('postUpvoted', { postUpvoted: post });
return post;
},
},
Subscription: {
postUpvoted(post) {
return post;
},
postUpvoted: {
subscribe: () => pubsub.asyncIterator(['postUpvoted'])
}
},
Author: {
posts(author) {
Expand Down
10 changes: 0 additions & 10 deletions data/subscriptions.js

This file was deleted.

42 changes: 20 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,28 @@
},
"homepage": "https://github.com/apollostack/apollo-starter-kit#readme",
"dependencies": {
"body-parser": "^1.15.2",
"cors": "^2.8.0",
"express": "4.14.0",
"graphql": "^0.8.1",
"graphql-server-express": "^0.4.2",
"graphql-subscriptions": "^0.2.0",
"graphql-tools": "^0.8.0",
"lodash": "^4.15.0",
"subscriptions-transport-ws": "^0.3.1"
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "4.17.1",
"apollo-server-express": "^2.9.4",
"graphql-tools": "^4.0.5",
"graphql-subscriptions": "^1.1.0",
"lodash": "^4.17.15"
},
"devDependencies": {
"babel-cli": "6.16.0",
"babel-core": "^6.17.0",
"babel-eslint": "^7.0.0",
"babel-plugin-inline-import": "^2.0.1",
"babel-polyfill": "6.16.0",
"babel-preset-es2015": "6.16.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "6.16.0",
"eslint": "^3.8.1",
"eslint-config-airbnb": "^12.0.0",
"eslint-plugin-import": "^2.0.1",
"eslint-plugin-react": "^6.4.1",
"nodemon": "^1.9.1"
"babel-cli": "6.26.0",
"babel-core": "^6.26.3",
"babel-eslint": "^10.0.3",
"babel-plugin-inline-import": "^3.0.0",
"babel-polyfill": "6.26.0",
"babel-preset-es2015": "6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "6.24.1",
"eslint": "^6.5.1",
"eslint-config-airbnb": "^18.0.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-react": "^7.15.1",
"nodemon": "^1.19.3"
},
"eslintConfig": {
"parser": "babel-eslint",
Expand Down
50 changes: 14 additions & 36 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,27 @@
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import bodyParser from 'body-parser';
import { ApolloServer } from 'apollo-server-express';
import cors from 'cors';
import { createServer } from 'http';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { printSchema } from 'graphql/utilities/schemaPrinter';

import { subscriptionManager } from './data/subscriptions';
import schema from './data/schema';
import { createServer } from 'http';

const GRAPHQL_PORT = 8080;
const WS_PORT = 8090;

const graphQLServer = express().use('*', cors());
const app = express();

graphQLServer.use('/graphql', bodyParser.json(), graphqlExpress({
schema,
context: {},
}));
app.use(cors());

graphQLServer.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));

graphQLServer.use('/schema', (req, res) => {
res.set('Content-Type', 'text/plain');
res.send(printSchema(schema));
const server = new ApolloServer({
schema
});

graphQLServer.listen(GRAPHQL_PORT, () => console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`
));

// WebSocket server for subscriptions
const websocketServer = createServer((request, response) => {
response.writeHead(404);
response.end();
server.applyMiddleware({
app,
path: '/graphql',
});

websocketServer.listen(WS_PORT, () => console.log( // eslint-disable-line no-console
`Websocket Server is now running on http://localhost:${WS_PORT}`
));
const httpServer = createServer(app);
server.installSubscriptionHandlers(httpServer);

// eslint-disable-next-line
new SubscriptionServer(
{ subscriptionManager },
websocketServer
);
httpServer.listen(GRAPHQL_PORT, () =>
console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`));