Skip to content

Instantly share code, notes, and snippets.

@r3-yamauchi
Last active December 16, 2018 23:12
Show Gist options
  • Save r3-yamauchi/16d991327dfcb0ffaa4c54f55fc597ff to your computer and use it in GitHub Desktop.
Save r3-yamauchi/16d991327dfcb0ffaa4c54f55fc597ff 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 listMessages = gql(`
query getConversationMessages($conversationId: ID!, $after: String, $first: Int) {
allMessageConnection(conversationId: $conversationId, after: $after, first: $first) {
__typename
nextToken,
messages {
__typename
id
conversationId
content
createdAt
sender
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 param = {
conversationId: "hello",
after: null,
first: 20
};
try {
const result = await client.query({
query: listMessages,
variables: param,
fetchPolicy: 'network-only'
});
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