Created
March 1, 2018 11:46
-
-
Save satoshin2071/c7bbd67a3d6747799be783a9e697da14 to your computer and use it in GitHub Desktop.
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
"use strict"; | |
Object.defineProperty(exports, "__esModule", { value: true }); | |
var config = { | |
APIKEY: 'key', | |
REGION: 'us-west-2', | |
TYPE: 'API_KEY', | |
ENDPOINT: 'endpoint', | |
}; | |
exports.default = config; |
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
"use strict"; | |
global.WebSocket = require('ws'); | |
global.window = global.window || { | |
setTimeout: setTimeout, | |
clearTimeout: clearTimeout, | |
WebSocket: global.WebSocket, | |
ArrayBuffer: global.ArrayBuffer, | |
addEventListener: function () { }, | |
navigator: { onLine: true } | |
}; | |
global.localStorage = { | |
store: {}, | |
getItem: function (key) { | |
return this.store[key] | |
}, | |
setItem: function (key, value) { | |
this.store[key] = value | |
}, | |
removeItem: function (key) { | |
delete this.store[key] | |
} | |
}; | |
require('es6-promise').polyfill(); | |
require('isomorphic-fetch'); | |
const aws_exports = require('./aws-exports').default; | |
const AWSAppSyncClient = require('aws-appsync').default; | |
const Alexa = require('alexa-sdk'); | |
const client = new AWSAppSyncClient({ | |
url: aws_exports.ENDPOINT, | |
region: aws_exports.REGION, | |
auth: { | |
type: aws_exports.TYPE, | |
apiKey: aws_exports.APIKEY, | |
} | |
}); | |
// Import gql helper | |
const gql = require('graphql-tag'); | |
// craft a GraphQL query | |
const query = gql(` | |
query listCities { | |
listCities { | |
items { | |
id | |
name | |
country | |
} | |
} | |
}` | |
); | |
const mutation = gql(` | |
mutation createCity { | |
createCity( | |
input: { | |
id: "hdpjqwojdoqjdpoqjdq" | |
name: "Osaka" | |
country: "Japan" | |
} | |
) { | |
id | |
name | |
country | |
} | |
}` | |
); | |
// client.hydrated().then(function (client) { | |
// //Now run a query | |
// client.query({ query: query }) | |
// .then(function logData(data) { | |
// console.log('results of query: ', data); | |
// console.log('results of query: ', data['data']['listCities']['__typename']); | |
// }) | |
// .catch(console.error); | |
// //Now run a mutation | |
// // client.mutate({ mutation: mutation }) | |
// // .then(function logData(data) { | |
// // console.log('results of mutation: ', data); | |
// // }) | |
// // .catch(console.error); | |
// }); | |
exports.handler = function(event, context, callback) { | |
const alexa = Alexa.handler(event, context, callback); | |
alexa.registerHandlers(handlers); | |
alexa.execute(); | |
}; | |
const handlers = { | |
'LaunchRequest': function () { | |
client.hydrated().then(function (client) { | |
client.mutate({ mutation: mutation }) | |
.then(function logData(data) { | |
console.log('results of mutation: ', data); | |
}) | |
.catch(console.error); | |
}); | |
this.emit(':tell', 'データベースに大阪を追加しておきました'); | |
}, | |
'OrderIntent': function () { | |
client.hydrated().then(function (client) { | |
client.mutate({ mutation: mutation }) | |
.then(function logData(data) { | |
console.log('results of mutation: ', data); | |
}) | |
.catch(console.error); | |
}); | |
this.emit(':tell', 'データベースに大阪を追加しておきました'); | |
}, | |
'AMAZON.HelpIntent': function () { | |
const speechOutput = 'AWS AppSync を利用した サンプルスキルです。声をトリガーにしてリアルタイム同期を確認致します。'; | |
this.emit(':ask', speechOutput); | |
}, | |
'AMAZON.CancelIntent': function () { | |
this.emit('AMAZON.StopIntent'); | |
}, | |
'AMAZON.StopIntent': function () { | |
this.emit(':tell', 'サンプル動作を終了致します。'); | |
}, | |
'Unhandled': function () { | |
const speechOutput = 'すみません。うまく理解できませんでした。'; | |
const reprompt = 'もう一度おっしゃってください。'; | |
this.emit(':ask', speechOutput, reprompt); | |
} | |
}; |
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
{ | |
"name": "appsync_nodejs_sample", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"request": "^2.83.0", | |
"apollo-cache-inmemory": "^1.1.0", | |
"apollo-client": "^2.0.3", | |
"apollo-link": "^1.0.3", | |
"apollo-link-http": "^1.2.0", | |
"aws-sdk": "^2.141.0", | |
"aws-appsync": "^1.0.0", | |
"es6-promise": "^4.1.1", | |
"graphql": "^0.11.7", | |
"graphql-tag": "^2.5.0", | |
"isomorphic-fetch": "^2.2.1", | |
"ws": "^3.3.1", | |
"uuid": "^3.2.1", | |
"alexa-sdk": "^1.0.10" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment