Skip to content
This repository has been archived by the owner on Jun 22, 2019. It is now read-only.

Migrate to graphql-yoga #4

Merged
merged 2 commits into from
Jan 19, 2018
Merged
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
83 changes: 23 additions & 60 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,44 @@
const express = require('express');
const { createServer } = require('http');
const bodyParser = require('body-parser');
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');
const { GraphQLServer, PubSub } = require('graphql-yoga');
const { formatError } = require('apollo-errors');
const cors = require('cors');
const jwt = require('express-jwt');
const faker = require('faker/locale/en');
const compression = require('compression');
const { execute, subscribe } = require('graphql');
const { SubscriptionServer } = require('subscriptions-transport-ws');

const typeDefs = require('./server/typeDefs');
const resolvers = require('./server/resolvers');
const initQuery = require('./server/initQuery');
const { sslRedirect } = require('./server/utils');
const rootValue = require('./server/rootValue');

const {
PORT = 5000,
JWT_SECRET = 'bufb73f3f084f3487f7803fn30f34bf0n3fb3f83'
} = process.env;
const { JWT_SECRET = 'bufb73f3f084f3487f7803fn30f34bf0n3fb3f83' } = process.env;

const schema = makeExecutableSchema({
const pubsub = new PubSub();
const server = new GraphQLServer({
typeDefs,
resolvers
resolvers,
context: ({ request }) => ({
jwtSecret: JWT_SECRET,
faker,
pubsub,
user: request.user
})
});

const app = express();
const server = createServer(app);
const isDeveloping = app.get('env') === 'development';

if (!isDeveloping) {
app.disable('x-powered-by');
app.use(compression());
}
server.express.disable('x-powered-by');

app.use(cors());

app.use(
server.express.use(
'/graphql',
jwt({
secret: JWT_SECRET,
credentialsRequired: false
}),
bodyParser.json(),
graphqlExpress(req => ({
formatError,
schema,
context: {
jwtSecret: JWT_SECRET,
faker,
user: req.user
}
}))
);

app.get(
'/',
graphiqlExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: `ws://localhost:${PORT}/subscriptions`,
query: initQuery
})
);

server.listen(PORT, () => {
console.log(`Listening on PORT ${PORT}`);
const options = {
formatError,
endpoint: '/graphql',
subscriptions: '/subscriptions',
playground: '/'
};

new SubscriptionServer(
{
execute,
subscribe,
schema
},
{
server,
path: '/subscriptions'
}
);
});
server.start(options, ({ port }) =>
console.log(`Server is running on PORT: ${port}`)
);
13 changes: 2 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,11 @@
},
"dependencies": {
"apollo-errors": "^1.5.1",
"body-parser": "^1.18.2",
"compression": "^1.7.1",
"cors": "^2.8.4",
"cuid": "^2.0.2",
"express": "^4.16.2",
"express-jwt": "^5.3.0",
"faker": "^4.1.0",
"graphql": "^0.12.3",
"graphql-redis-subscriptions": "^1.4.0",
"graphql-server-express": "^1.3.2",
"graphql-subscriptions": "^0.5.6",
"graphql-tools": "^2.17.0",
"ioredis": "^3.2.2",
"jsonwebtoken": "^8.1.0",
"subscriptions-transport-ws": "^0.9.5"
"graphql-yoga": "^1.1.5",
"jsonwebtoken": "^8.1.0"
}
}
56 changes: 31 additions & 25 deletions server/resolvers.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const { RedisPubSub } = require('graphql-redis-subscriptions');
const Redis = require('ioredis');
// const { RedisPubSub } = require('graphql-redis-subscriptions');
// const Redis = require('ioredis');

const cuid = require('cuid');
const { generateAuthToken } = require('./utils');

const { REDIS_URL = 'http://localhost:6379/' } = process.env;
// const { REDIS_URL } = process.env;

const pubsub = new RedisPubSub({
publisher: new Redis(REDIS_URL),
subscriber: new Redis(REDIS_URL)
});
// const pubsub = new RedisPubSub({
// publisher: new Redis(REDIS_URL),
// subscriber: new Redis(REDIS_URL)
// });

const DEFAULT_COUNT = 25;

Expand Down Expand Up @@ -137,30 +137,36 @@ module.exports = {
createTodo: (parent, { title, completed }, { faker }) => {
const id = cuid();

pubsub.publish('todoAdded', {
todoAdded: {
id,
title,
completed
}
});
// pubsub.publish('todoAdded', {
// todoAdded: {
// id,
// title,
// completed
// }
// });

return {
id,
title,
completed: completed === undefined ? faker.random.boolean() : completed
};
}
},

Subscription: {
todoAdded: {
resolve: payload => ({
id: 'abc',
title: 'Hello',
completed: true
}),
subscribe: () => pubsub.asyncIterator('todoAdded')
}
}

// Subscription: {
// todoAdded: {
// subscribe: (parent, args, { pubsub }) => {
// // setInterval(
// // () => pubsub.publish(channel, { counter: { count: count++ } }),
// // 2000
// // );
// //
// // return pubsub.asyncIterator({
// // id: 'abc',
// // title: 'Hello',
// // completed: true
// // });
// }
// }
// }
};
File renamed without changes.
Loading