Update the Amplify function grant access to the GraphQL API
? Select which capability you want to update: Lambda function (serverless function) ? Select the Lambda function you want to update: MyFunction General information | Name: MyFunction | Runtime: nodejs ...
? Which setting do you want to update
? Resource access permissions
? Select the categories you want this function to have access to. (Select using <space>) > api
? Select the operations you want to permit on <YOUR_API_NAME> (Select using <space>) > Query, Mutation, Subscription You can access the following resource attributes as environment variables from your Lambda function API_<YOUR_API_NAME>_GRAPHQLAPIENDPOINTOUTPUT API_<YOUR_API_NAME>_GRAPHQLAPIIDOUTPUT
API_<YOUR_API_NAME>_GRAPHQLAPIKEYOUTPUT
? Do you want to edit the local lambda function now? No
Update the schema.graphql
to include the following directives on the tables
@aws_cognito_user_pools
@aws_iam
Update the function code to make the GraphQL call from the lambda function:
const appsyncUrl = process.env.API_<YOUR_API_NAME>_GRAPHQLAPIENDPOINTOUTPUT;
module.exports = function (req, res) {
const getEmployeeById = {
query: print(getEmployeByIdGQL),
variables: {
employeeId: req.body.employeeId
}
};
const data = JSON.stringify(req.body);
const uri = URL.parse(appsyncUrl);
const httpRequest = new AWS.HttpRequest(uri.href, env.REGION);
httpRequest.headers.host = uri.host;
httpRequest.headers['Content-Type'] = 'application/json';
httpRequest.method = 'POST';
httpRequest.body = JSON.stringify(getEmployeeById);
AWS.config.credentials.get(err => {
const signer = new AWS.Signers.V4(httpRequest, "appsync", true);
signer.addAuthorization(AWS.config.credentials, AWS.util.date.getDate());
const options = {
method: httpRequest.method,
body: httpRequest.body,
headers: httpRequest.headers
};
fetch(uri.href, options)
.then( response => {
response.json().then(resEmployee => {
res.json(resEmployee);
})
});
});