Skip to content

Commit

Permalink
Merge pull request #21 from oslabs-beta/pre-gql
Browse files Browse the repository at this point in the history
Update GraphQL Server to using apollo server
  • Loading branch information
nattiechan authored Apr 10, 2023
2 parents 5b58f00 + a9d79d5 commit 1419d4d
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 146 deletions.
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"report": "istanbul report --dir ./test/coverage/total-coverage html",
"test": "webpack --mode=production --config ./webpack.production.js && cross-env process.env.NODE_ENV=test process.env.TRAVIS_LOCAL_API=true nyc --report-dir ./test/coverage/mocha-coverage --reporter json --reporter text --reporter html mocha --exit",
"server-sse": "node ./test/fakeSSEServer.js",
"server-gql": "node ./test/graphqlServer.js",
"server-gql": "node ./test/graphqlServer.mjs",
"server-grpc": "node ./test/grpcServer.js",
"server-http": "node ./test/httpServer.js",
"server-http2": "node ./test/HTTP2_server.js",
Expand Down Expand Up @@ -107,6 +107,7 @@
},
"dependencies": {
"@apollo/client": "^3.5.0",
"@apollo/server": "^4.6.0",
"@graphql-tools/schema": "^8.3.10",
"@grpc/grpc-js": "^1.6.7",
"@grpc/proto-loader": "^0.6.9",
Expand All @@ -117,17 +118,13 @@
"@uiw/codemirror-extensions-langs": "^4.19.9",
"@uiw/codemirror-theme-vscode": "^4.19.9",
"@uiw/react-codemirror": "^4.19.9",
"apollo-server-express": "^3.6.7",
"axios": "^0.27.1",
"body-parser": "^1.20.0",
"bulma": "^0.9.3",
"bulma-checkradio": "^2.1.3",
"bulma-switch": "^2.0.4",
"chai": "^4.3.6",
"chai-http": "^4.3.0",
"chart.js": "^3.7.1",
"classnames": "^2.3.1",
"concurrently": "^7.1.0",
"cookie-parser": "^1.4.6",
"cross-fetch": "^3.1.5",
"date-fns": "^2.28.0",
Expand All @@ -143,7 +140,6 @@
"fs": "0.0.1-security",
"fs-extra": "^10.1.0",
"graphql": "^16.4.0",
"graphql-subscriptions": "^2.0.0",
"graphql-tag": "^2.12.6",
"graphql-ws": "^5.8.1",
"highland": "^2.13.5",
Expand All @@ -170,9 +166,7 @@
"socket.io": "^4.5.0",
"socket.io-client": "^4.5.0",
"uuid": "^8.3.2",
"vm2": "^3.9.9",
"websocket": "^1.0.34",
"ws": "^8.5.0",
"yamljs": "^0.3.0"
},
"devDependencies": {
Expand Down Expand Up @@ -205,6 +199,9 @@
"@typescript-eslint/eslint-plugin": "^5.21.0",
"@typescript-eslint/parser": "^5.21.0",
"babel-loader": "^8.2.5",
"chai": "^4.3.7",
"chai-http": "^4.3.0",
"concurrently": "^8.0.1",
"cross-env": "^7.0.3",
"csp-html-webpack-plugin": "^5.1.0",
"css-loader": "^6.7.1",
Expand All @@ -215,6 +212,7 @@
"eslint-config-airbnb": "^19.0.4",
"eslint-plugin-react-hooks": "^4.5.0",
"fake-indexeddb": "^3.1.7",
"graphql-subscriptions": "^2.0.0",
"html-webpack-plugin": "^5.5.0",
"istanbul": "^0.4.5",
"jest": "26.0.1",
Expand All @@ -236,12 +234,14 @@
"ts-migrate": "^0.1.28",
"typescript": "^4.6.3",
"url-loader": "^4.1.1",
"vm2": "^3.9.14",
"webpack": "^5.72.0",
"webpack-bundle-analyzer": "^4.5.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.8.1",
"webpack-merge": "^5.8.0",
"webpack-node-externals": "^3.0.0"
"webpack-node-externals": "^3.0.0",
"ws": "^8.13.0"
},
"author": {
"name": "Swell",
Expand Down
134 changes: 0 additions & 134 deletions test/graphqlServer.js

This file was deleted.

144 changes: 144 additions & 0 deletions test/graphqlServer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/**
* @file This is mock server for GraphQL
* tested query, mutation, and subcription and confrimed its working fine
*
* NOTE:
* Apollo server 4 requires top-level async/await for the server to start properly,
* which can only be done in a module. Since most backend logic in Swell uses
* CommonJS, we need to change this file to `.mjs` to make things work.
* The downside to making this as a `.mjs` file is that it would not be automatically
* compatible with the rest of the backend code that uses CommonJS.
* Since this is only for testing and as a standalone server, we should not need
* to configure the bundler to accept both. If you do, you may need to research
* on how to do so in webpack.
*
* Apollo server reference: https://www.apollographql.com/docs/apollo-server/data/subscriptions/
*/
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import { createServer } from 'http';
import express from 'express';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import bodyParser from 'body-parser';
import cors from 'cors';
import { PubSub } from 'graphql-subscriptions';

const PORT = 4000;
const pubsub = new PubSub();

const typeDefs = `
type Query {
feed: [Link!]!
link(id: ID!): Link
}
type Mutation {
post(url: String!, description: String!): Link!
}
type Subscription {
newLink: Link
}
type Link {
id: ID!
description: String!
url: String!
}
`;

//first time, sending a query request should get response of link data below ('www.getswell.io')
const links = [
{
id: 'link-0',
url: 'www.getswell.io',
description: 'One-stop-shop for testing API endpoints',
},
];

let idCount = links.length;

const resolvers = {
// similar to GET request -> display
Query: {
feed: () => links,
},
// with Mutation, send post request -> adding new data
Mutation: {
post: (parent, args) => {
const link = {
id: `link-${idCount++}`,
description: args.description,
url: args.url,
};
console.log(link);
links.push(link);
pubsub.publish('NEW_LINK', { newLink: link });
return link;
},
},
// subscription will listen to the server and everytime there's new update, server will send a new link to client
// if subscription worked, should console log 'subscribed' in terminal
Subscription: {
newLink: {
// create a subscription resolver function
subscribe: () => {
console.log('subscribed');
return pubsub.asyncIterator('NEW_LINK');
}, // subscribe to changes in a topic
},
},
Link: {
id: (parent) => parent.id,
description: (parent) => parent.description,
url: (parent) => parent.url,
},
};

// Create the schema, which will be used separately by ApolloServer and
// the WebSocket server.
const schema = makeExecutableSchema({ typeDefs, resolvers });

// Create an Express app and HTTP server; we will attach both the WebSocket
// server and the ApolloServer to this HTTP server.
const app = express();
const httpServer = createServer(app);

// Create our WebSocket server using the HTTP server we just set up.
const wsServer = new WebSocketServer({
server: httpServer,
path: '/graphql',
});
// Save the returned server's info so we can shutdown this server later
const serverCleanup = useServer({ schema }, wsServer);

// Set up ApolloServer.
const server = new ApolloServer({
schema,
plugins: [
// Proper shutdown for the HTTP server.
ApolloServerPluginDrainHttpServer({ httpServer }),

// Proper shutdown for the WebSocket server.
{
async serverWillStart() {
return {
async drainServer() {
await serverCleanup.dispose();
},
};
},
},
],
});

await server.start();
app.use('/graphql', cors(), bodyParser.json(), expressMiddleware(server));

// Now that our HTTP server is fully set up, we can listen to it.
httpServer.listen(PORT, () => {
console.log(`Server is now running on http://localhost:${PORT}/graphql`);
});
1 change: 0 additions & 1 deletion test/subSuites/grpcTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

const grpcServer = require('../grpcServer.js');
const graphqlServer = require('../graphqlServer');
const { _electron: electron } = require('playwright');
const chai = require('chai');
const expect = chai.expect;
Expand Down
4 changes: 2 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
module: {
rules: [
{
test: /\.(ts|js)x?$/,
test: /\.(ts|js|mjs)x?$/,
include: [path.resolve(__dirname, 'src')],
use: {
loader: 'babel-loader',
Expand All @@ -50,7 +50,7 @@ module.exports = {
},
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'],
extensions: ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.json'],
},
},
{
Expand Down

0 comments on commit 1419d4d

Please sign in to comment.