Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 8ec8d3a

Browse files
authored
Merge pull request #365 from amerani/migrate/apollo-server
Migrate app-backend/rest-api to Apollo Server 2.0
2 parents a578c03 + 5e7cd86 commit 8ec8d3a

File tree

10 files changed

+3286
-120
lines changed

10 files changed

+3286
-120
lines changed

app-backend/dynamodb/yarn.lock

Lines changed: 704 additions & 0 deletions
Large diffs are not rendered by default.

app-backend/rest-api/handler.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,34 @@
1-
import 'babel-polyfill';
2-
import { graphqlLambda, graphiqlLambda } from 'apollo-server-lambda';
3-
import lambdaPlayground from 'graphql-playground-middleware-lambda';
4-
import { makeExecutableSchema } from 'graphql-tools';
1+
import { ApolloServer } from 'apollo-server-lambda';
52
import { schema } from './schema';
63
import { resolvers } from './resolvers';
74

8-
const myGraphQLSchema = makeExecutableSchema({
5+
const server = new ApolloServer({
96
typeDefs: schema,
107
resolvers,
11-
logger: console,
8+
formatError: error => {
9+
console.log(error);
10+
return error;
11+
},
12+
formatResponse: response => {
13+
console.log(response);
14+
return response;
15+
},
16+
context: ({ event, context }) => ({
17+
headers: event.headers,
18+
functionName: context.functionName,
19+
event,
20+
context,
21+
}),
22+
playground: {
23+
endpoint: process.env.REACT_APP_GRAPHQL_ENDPOINT
24+
? process.env.REACT_APP_GRAPHQL_ENDPOINT
25+
: '/production/graphql',
26+
},
27+
tracing: true,
1228
});
1329

14-
exports.graphqlHandler = function graphqlHandler(event, context, callback) {
15-
function callbackFilter(error, output) {
16-
// eslint-disable-next-line no-param-reassign
17-
output.headers['Access-Control-Allow-Origin'] = '*';
18-
callback(error, output);
19-
}
20-
21-
const handler = graphqlLambda({ schema: myGraphQLSchema, tracing: true });
22-
return handler(event, context, callbackFilter);
23-
};
24-
25-
// for local endpointURL is /graphql and for prod it is /stage/graphql
26-
exports.playgroundHandler = lambdaPlayground({
27-
endpoint: process.env.REACT_APP_GRAPHQL_ENDPOINT
28-
? process.env.REACT_APP_GRAPHQL_ENDPOINT
29-
: '/production/graphql',
30-
});
31-
32-
exports.graphiqlHandler = graphiqlLambda({
33-
endpointURL: process.env.REACT_APP_GRAPHQL_ENDPOINT
34-
? process.env.REACT_APP_GRAPHQL_ENDPOINT
35-
: '/production/graphql',
30+
exports.graphqlHandler = server.createHandler({
31+
cors: {
32+
origin: '*',
33+
},
3634
});

app-backend/rest-api/handler.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import { graphqlHandler, playgroundHandler } from './handler';
1+
import { graphqlHandler } from './handler';
22

33
it('graphqlHandler should be a function', () => {
44
expect(typeof graphqlHandler).toBe('function');
55
});
6-
7-
it('playgroundHandler should be a function', () => {
8-
expect(typeof playgroundHandler).toBe('function');
9-
});

app-backend/rest-api/package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@
1212
"deploy-prod": "serverless --stage=production deploy"
1313
},
1414
"dependencies": {
15-
"apollo-server-lambda": "1.3.2",
16-
"apollo-tracing": "^0.0.9",
15+
"apollo-server-lambda": "^2.2.1",
1716
"babel-polyfill": "^6.26.0",
18-
"graphql": "^0.10.5",
19-
"graphql-playground-middleware": "^1.1.0",
20-
"graphql-playground-middleware-lambda": "^1.2.0",
21-
"graphql-tools": "2.7.2",
17+
"graphql": "^14.0.2",
2218
"node-fetch": "^1.7.3",
2319
"oauth": "^0.9.15",
2420
"twitter": "^1.7.1"

app-backend/rest-api/resolvers.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,18 @@ async function getUserTweets(args) {
9292
.then(tweets => {
9393
const tweetArray = [];
9494
let listOfTweets;
95-
95+
if (tweets.length <= 0) {
96+
listOfTweets = {
97+
name: '',
98+
handle: args.handle,
99+
location: '',
100+
description: '',
101+
followers_count: 0,
102+
friends_count: 0,
103+
favourites_count: 0,
104+
following: [],
105+
};
106+
}
96107
return getFollowing(
97108
args.handle,
98109
args.consumer_key,

app-backend/rest-api/schema.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type Query {
5353
searchAllTweetsByKeyword(keyword: String!): TweetConnection
5454
}
5555
56+
directive @aws_subscribe(mutations: [String]) on FIELD_DEFINITION
57+
5658
type Subscription {
5759
addTweet: Tweet
5860
@aws_subscribe(mutations: ["createTweet"])

app-backend/rest-api/serverless.yml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,6 @@ functions:
2525
path: graphql
2626
method: post
2727
cors: true
28-
playground:
29-
handler: handler.playgroundHandler
30-
events:
3128
- http:
32-
path: playground
33-
method: get
34-
cors: true
35-
graphiql:
36-
handler: handler.graphiqlHandler
37-
events:
38-
- http:
39-
path: graphiql
40-
method: get
41-
cors: true
29+
path: graphql
30+
method: get

0 commit comments

Comments
 (0)