Skip to content

Commit

Permalink
[v1.0.0] adding localStorage utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanith committed Dec 4, 2018
1 parent 824f2f7 commit 0932dfe
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 13 deletions.
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nutshelllab/aws-apollo-tools",
"version": "0.0.6",
"version": "1.0.0",
"description": "Aws apollo",
"main": "lib/aws-apollo-tools.js",
"scripts": {
Expand Down Expand Up @@ -29,24 +29,27 @@
},
"homepage": "https://github.com/nutshell-lab/aws-apollo-tools#readme",
"dependencies": {
"@babel/register": "^7.0.0",
"apollo-cache-inmemory": "^1.3.11",
"apollo-client": "^2.4.7",
"apollo-link": "^1.2.4",
"apollo-link-context": "^1.0.10",
"apollo-link-http": "^1.5.7",
"apollo-server-lambda": "^2.2.6",
"aws-sdk": "^2.363.0",
"aws4": "^1.8.0",
"babel-plugin-module-resolver": "^3.1.1",
"graphql": "^14.0.2",
"node-fetch": "^2.3.0",
"node-localstorage": "^1.3.1",
"url": "^0.11.0"
},
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/preset-env": "^7.1.6",
"@babel/register": "^7.0.0",
"ava": "1.0.0-beta.4",
"babel-loader": "^8.0.4",
"babel-plugin-module-resolver": "^3.1.1",
"eslint": "^5.9.0",
"eslint-config-prettier": "^2.9.0",
"eslint-config-prettier-standard": "^1.0.1",
Expand Down
12 changes: 12 additions & 0 deletions src/apollo-server-lambda.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ApolloServer as ApolloServerClass } from 'apollo-server-lambda'
import { LocalStorage } from 'node-localstorage'

const store = new LocalStorage('/tmp/store')

export class ApolloServer extends ApolloServerClass {
constructor(args) {
super(args)
const { context } = args
store.setItem('context', JSON.stringify(context))
}
}
10 changes: 9 additions & 1 deletion src/lambda-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { ApolloLink, Observable } from 'apollo-link'
import { print } from 'graphql/language/printer'
import AWS from 'aws-sdk'

export default ({ lambdaName, region, ...options }) => {
const makeFullHeaders = (headers, { headers: contextHeaders }) => {
return {
...headers,
...contextHeaders
}
}

export default ({ lambdaName, region, headers, ...options }) => {
return new ApolloLink(operation => {
const lambda = new AWS.Lambda({ region })
const { variables } = operation
Expand All @@ -12,6 +19,7 @@ export default ({ lambdaName, region, ...options }) => {
Payload: JSON.stringify({
httpMethod: 'POST',
body: JSON.stringify({ query, variables }),
headers: makeFullHeaders(headers, operation.getContext()),
...options
})
}
Expand Down
13 changes: 8 additions & 5 deletions src/lambda-request.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { graphqlRequest } from './core/graphql-request'
import createLambdaLink from './lambda-link'
import withHeaders from './link-with-headers'

export default ({ lambdaName, region, request, variables, headers = {} }) => {
const link = createLambdaLink({
lambdaName,
region,
headers
})
const link = withHeaders(
createLambdaLink({
lambdaName,
region
})
)(headers)

return graphqlRequest({ link, request, variables })
}
19 changes: 19 additions & 0 deletions src/link-with-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { setContext } from 'apollo-link-context'
import { LocalStorage } from 'node-localstorage'

const store = new LocalStorage('/tmp/store')

export default link => (headers = {}) =>
setContext((_request, _context) => {
const context = store.getItem('context')
if (!context) return { headers }
const {
event: { headers: contextHeaders = {} }
} = JSON.parse(context)
return {
headers: {
...headers,
...contextHeaders
}
}
}).concat(link)
Loading

0 comments on commit 0932dfe

Please sign in to comment.