Skip to content

Instantly share code, notes, and snippets.

@imjacobclark
Created December 7, 2018 12:01
Show Gist options
  • Save imjacobclark/d3cee1271f1fd307db99ed25e2eb4873 to your computer and use it in GitHub Desktop.
Save imjacobclark/d3cee1271f1fd307db99ed25e2eb4873 to your computer and use it in GitHub Desktop.
API Gateway -> Lambda -> Apollo -> AppSync -> GitHub API
const query = require('./query.js');
require('es6-promise').polyfill();
exports.handler = function(event, context, callback) {
query(event["queryStringParameters"]['username']).then(function(data){
var resp = {
"isBase64Encoded": false,
"statusCode": 200,
"headers": {
'Content-Type': 'text/html'
},
"body": "<h1>" + data + "</h1>"
}
callback(null, resp);
});
}
{
"name": "HelloWorldBBC",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"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"
}
}
"use strict";
polyfillGlobalScope();
const gql = require('graphql-tag');
const API_KEY_AUTH_TYPE = require('aws-appsync/lib/link/auth-link').AUTH_TYPE.API_KEY;
const AWSAppSyncClient = require('aws-appsync').default;
const query = function (username) {
return gql(`
query{
getUser(username: "${username}"){
name
}
}
`);
};
const client = new AWSAppSyncClient({
url: 'https://YOUR_APP_SYNC_ID.appsync-api.YOUR_REGION.amazonaws.com/graphql',
region: 'YOUR_REGION',
auth: {
type: API_KEY_AUTH_TYPE,
apiKey: 'YOUR_APP_SYNC_API_KEY_HERE',
},
disableOffline: true
});
function polyfillGlobalScope(){
require('es6-promise').polyfill();
require('isomorphic-fetch');
}
module.exports = function(username) {
return client.hydrated().then(function (client) {
client.query({ query: query(username) })
return client.query({ query: query(username), fetchPolicy: 'network-only' })
.then(function logData(data) {
return data.data.getUser.name + "!";
})
.catch(console.error);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment