Skip to content

Instantly share code, notes, and snippets.

@r3-yamauchi
Last active December 16, 2018 23:12
Show Gist options
  • Save r3-yamauchi/f255c25847cc32103570799d54aace34 to your computer and use it in GitHub Desktop.
Save r3-yamauchi/f255c25847cc32103570799d54aace34 to your computer and use it in GitHub Desktop.
AWS Lambda から AppSync の API を ぶん殴る https://blog.r3it.com/aws-lambda-to-appsync-1aa0c2f1da04
require('isomorphic-fetch');
const AUTH_TYPE = require('aws-appsync/lib/link/auth-link').AUTH_TYPE;
const AWSAppSyncClient = require('aws-appsync').default;
const gql = require('graphql-tag');
const uuid = require('uuid/v4');
const newMessage = gql(`
mutation createMessage($id: ID!, $content: String, $conversationId: ID!, $createdAt: String!) {
createMessage(id: $id, content: $content, conversationId: $conversationId, createdAt: $createdAt){
__typename
conversationId
createdAt
id
sender
content
isSent
}
}`);
exports.handler = async (event) => {
const client = new AWSAppSyncClient({
url: process.env['URL'],
region: process.env['REGION'],
auth: {
type: AUTH_TYPE.API_KEY,
apiKey: process.env['APIKEY']
},
disableOffline: true
});
const now = `${new Date().toISOString()}`;
const id = `${now}_${uuid()}`;
try {
const result = await client.mutate({
variables: {
conversationId: "hello",
content: 'こんにちは!!',
createdAt: now,
sender: 'AWS Lambda',
isSent: false,
id: id
},
mutation: newMessage
});
console.log(JSON.stringify(result));
return result;
} catch (err) {
console.log(JSON.stringify(err));
return err;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment