forked from nikhilaravi/serverless-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7077359
commit 34ba9b1
Showing
12 changed files
with
339 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
bucket="serverless-test-database" | ||
bucket="serverless-database" | ||
region="eu-west-1" | ||
|
||
aws s3api create-bucket \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const invokeQuery = require('./schema/fixtures').suggestionsQuery; | ||
|
||
var assert = require('assert'); | ||
var index = require('../index.js'); | ||
describe('Invoke Test', () => { | ||
it('invokes the lambda with a suggestions query', (done) => { | ||
const event = { | ||
query: invokeQuery, | ||
variables: { | ||
query: 'Hello' | ||
} | ||
}; | ||
index.handler(event, {}, (err, res) => { | ||
assert.equal(err, null); | ||
assert.equal(res.data.suggestions.length > 0, true); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
var graphql = require('graphql').graphql; | ||
var assert = require('assert'); | ||
const simple = require('simple-mock'); | ||
|
||
var addTrack = require('../../lib/schema/mutation/addTrackMutation.js'); | ||
var root = require('../../lib/schema').root; | ||
var playlistService = require('../../lib/services/playlistService.js'); | ||
|
||
var introspect = require('../utils/introspectGraphQL'); | ||
var schemaHelper = require('../utils/schemaHelper'); | ||
|
||
var addTrackMutation = require('./fixtures').addTrackMutation; | ||
|
||
describe('Suggestions schema', function () { | ||
it('should be possible to introspect the playlistQuery schema', function (done) { | ||
var schema = schemaHelper.createQuerySchema(addTrack.addTrackMutation); | ||
introspect.introspectGraphQL(schema, done); | ||
}); | ||
|
||
it('should be able to execute the suggestionsQuery', function (done) { | ||
var id = { | ||
id: '123456' | ||
}; | ||
var stub = simple.mock(playlistService, 'addTrack').resolvesWith(id); | ||
var expectedResult = { | ||
'data': { | ||
'addTrack': id | ||
} | ||
}; | ||
graphql(root, addTrackMutation, null, {}).then(function (result) { | ||
assert.deepEqual(result, expectedResult); | ||
stub.restore(); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export const playlistQuery = ` | ||
query { | ||
playlist { | ||
name, | ||
artist, | ||
url, | ||
imageUrl | ||
} | ||
} | ||
`; | ||
|
||
export const suggestionsQuery = ` | ||
query($query: String) { | ||
suggestions { | ||
name, | ||
artist, | ||
url, | ||
imageUrl | ||
} | ||
} | ||
`; | ||
|
||
export const addTrackMutation = ` | ||
mutation addTrackMutation($name: String, $artist: String, $url: String, $imageUrl: String) { | ||
addTrack(name: $name, artist: $artist, url: $url, imageUrl: $imageUrl) { | ||
id | ||
} | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
var graphql = require('graphql').graphql; | ||
var assert = require('assert'); | ||
const simple = require('simple-mock'); | ||
|
||
var playlist = require('../../lib/schema/query/playlistQuery.js'); | ||
var root = require('../../lib/schema').root; | ||
var playlistService = require('../../lib/services/playlistService.js'); | ||
|
||
var introspect = require('../utils/introspectGraphQL'); | ||
var schemaHelper = require('../utils/schemaHelper'); | ||
|
||
var playlistQuery = require('./fixtures').playlistQuery; | ||
|
||
describe('Playlist schema', function () { | ||
it('should be possible to introspect the playlistQuery schema', function (done) { | ||
var schema = schemaHelper.createQuerySchema(playlist.playlistQuery); | ||
introspect.introspectGraphQL(schema, done); | ||
}); | ||
|
||
it('should be able to execute the playlistQuery', function (done) { | ||
var playlist = [{ | ||
name: 'Hello', | ||
artist: 'Adele', | ||
url: 'url', | ||
imageUrl: 'imageUrl' | ||
}]; | ||
var stub = simple.mock(playlistService, 'retrievePlaylist').resolvesWith(playlist); | ||
var expectedResult = { | ||
'data': { | ||
'playlist': playlist | ||
} | ||
}; | ||
graphql(root, playlistQuery, null, {}).then(function (result) { | ||
assert.deepEqual(result, expectedResult); | ||
stub.restore(); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
var graphql = require('graphql').graphql; | ||
var assert = require('assert'); | ||
const simple = require('simple-mock'); | ||
|
||
var suggestions = require('../../lib/schema/query/suggestionsQuery.js'); | ||
var root = require('../../lib/schema').root; | ||
var suggestionsService = require('../../lib/services/suggestionsService.js'); | ||
|
||
var introspect = require('../utils/introspectGraphQL'); | ||
var schemaHelper = require('../utils/schemaHelper'); | ||
|
||
var suggestionsQuery = require('./fixtures').suggestionsQuery; | ||
|
||
describe('Suggestions schema', function () { | ||
it('should be possible to introspect the playlistQuery schema', function (done) { | ||
var schema = schemaHelper.createQuerySchema(suggestions.suggestionsQuery); | ||
introspect.introspectGraphQL(schema, done); | ||
}); | ||
|
||
it('should be able to execute the suggestionsQuery', function (done) { | ||
var songSuggestions = [{ | ||
name: 'Hello', | ||
artist: 'Adele', | ||
url: 'url', | ||
imageUrl: 'imageUrl' | ||
}]; | ||
var stub = simple.mock(suggestionsService, 'retrieveSongSuggestions').resolvesWith(songSuggestions); | ||
var expectedResult = { | ||
'data': { | ||
'suggestions': songSuggestions | ||
} | ||
}; | ||
graphql(root, suggestionsQuery, null, {}).then(function (result) { | ||
assert.deepEqual(result, expectedResult); | ||
stub.restore(); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict'; | ||
|
||
const simple = require('simple-mock'); | ||
const assert = require('assert'); | ||
const lambdaInvoke = require('../../lib/utils/lambda-invoke-promise'); | ||
const playlistService = require('../../lib/services/playlistService'); | ||
|
||
const track = { | ||
name: 'Hello', | ||
artist: 'Adele', | ||
url: 'url', | ||
imageUrl: 'url' | ||
}; | ||
|
||
const suggestions = [{ | ||
name: 'Hello', | ||
artist: 'Adele', | ||
url: 'url', | ||
imageUrl: 'url' | ||
}]; | ||
|
||
describe('Playlist service', () => { | ||
afterEach(function (done) { | ||
simple.restore(); | ||
done(); | ||
}); | ||
|
||
it('retrieveSuggestions: returns an array of song suggestions', done => { | ||
const result = { | ||
data: { | ||
suggestions: result | ||
} | ||
}; | ||
simple.mock(lambdaInvoke, 'invoke').resolveWith(suggestions); | ||
|
||
playlistService.retrieveSuggestions('Fire', 10).then(data => { | ||
assert.deepEqual(data, result); | ||
done(); | ||
}).catch(done); | ||
}); | ||
|
||
it('retrieveSuggestions: will return an error due to request error', done => { | ||
const error = 'Big bad error'; | ||
simple.mock(lambdaInvoke, 'invoke').rejectWith(error); | ||
|
||
playlistService.retrieveSuggestions('some song', 10).then(data => { | ||
assert.deepEqual(data, error); | ||
done(); | ||
}).catch(done); | ||
}); | ||
|
||
it('addTrack: calls the s3 save micro service and returns a track id', done => { | ||
const result = { | ||
data: { | ||
addTrack: { | ||
id: '1234456' | ||
} | ||
} | ||
}; | ||
|
||
simple.mock(lambdaInvoke, 'invoke').resolveWith({id: '1234456'}); | ||
|
||
playlistService.addTrack(track).then(data => { | ||
assert.deepEqual(data, result); | ||
done(); | ||
}).catch(done); | ||
}); | ||
it('addTrack: will return an error due to request error', done => { | ||
const error = 'Big bad error'; | ||
simple.mock(lambdaInvoke, 'invoke').rejectWith(error); | ||
|
||
playlistService.addTrack(track).then(data => { | ||
assert.deepEqual(data, error); | ||
done(); | ||
}).catch(done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
var graphql = require('graphql'); | ||
var introspectionQuery = require('graphql/utilities').introspectionQuery; | ||
|
||
var q = exports; | ||
|
||
q.introspectGraphQL = function (schema, done) { | ||
graphql.graphql(schema, introspectionQuery).then(function (result) { | ||
if (result.errors && result.errors.length) { | ||
return done(new Error(result.errors[0].message)); | ||
} | ||
done(); | ||
}).catch(function (err) { | ||
console.log('ERR', err); | ||
done(err); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var graphql = require('graphql'); | ||
|
||
var createQuerySchema = function (query, field) { | ||
return new graphql.GraphQLSchema({ | ||
query: new graphql.GraphQLObjectType({ | ||
name: 'RootQuery', | ||
fields: function () { | ||
var x = {}; | ||
x[field || 'type'] = query; | ||
return x; | ||
} | ||
}) | ||
}); | ||
}; | ||
|
||
var createMutationSchema = function (mutation, field) { | ||
return new graphql.GraphQLSchema({ | ||
query: new graphql.GraphQLObjectType({ | ||
name: 'RootQuery', | ||
fields: { | ||
helperField: { type: graphql.GraphQLString } | ||
} | ||
}), | ||
mutation: new graphql.GraphQLObjectType({ | ||
name: 'RootMutation', | ||
fields: function () { | ||
var x = {}; | ||
x[field || 'type'] = mutation; | ||
return x; | ||
} | ||
}) | ||
}); | ||
}; | ||
|
||
exports.createQuerySchema = createQuerySchema; | ||
exports.createMutationSchema = createMutationSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
var simple = require('simple-mock'); | ||
var AwsHelper = require('aws-lambda-helper'); | ||
var Code = require('code'); | ||
var expect = Code.expect; | ||
var lambdaInvoke = require('../../lib/utils/lambda-invoke-promise'); | ||
|
||
describe('lambda invoke promise util', function () { | ||
afterEach(function (done) { | ||
simple.restore(); | ||
done(); | ||
}); | ||
|
||
it('will invoke a lambda and return a promise', function (done) { | ||
simple.mock(AwsHelper.Lambda, 'invoke').callbackWith(null, 'some data'); | ||
|
||
lambdaInvoke.invoke({some: 'params'}).then(function (data) { | ||
expect(data).equals('some data'); | ||
done(); | ||
}).catch(done); | ||
}); | ||
|
||
it('will throw an error when the lambda.invoke failed', function (done) { | ||
simple.mock(AwsHelper.Lambda, 'invoke').callbackWith('Big error'); | ||
|
||
lambdaInvoke.invoke({some: 'params'}).catch(function (error) { | ||
expect(error).equals('Big error'); | ||
done(); | ||
}); | ||
}); | ||
}); |