I'm trying to update a GraphQL subscription when a DynamoDb table receives a new row. I got the following code working with only the RekognitionId, but I'm not trying to send the entire NewImage object, and I cannot make it work. I get all sorts of type problems, but with no real information to solve it with. The most telling was:
"Error: GraphQL error: Variable 'input' has an invalid value. Expected type 'Map' but was 'String'. Variables for input objects must be an instance of type 'Map'."
Unfortunately, I can't find a single reference to a GraphQL type called "map", so it's probably scrambled.
Does anyone have any experience of this? This is my Lambda function, like I said it worked with only RekognitionId formatted as a dynamoDb semi-json-string {"S": "id"}
global.WebSocket = require('ws');
require('es6-promise').polyfill();
require('isomorphic-fetch');
const AWS = require('aws-sdk');
const aws_exports = require('./aws-exports').default;
const AWSAppSyncClient = require('aws-appsync').default;
exports.handler = async (event, context, callback) => {
if(!event.Records.length){
console.log('no records');
return false;
}
const AUTH_TYPE = require('aws-appsync/lib/link/auth-link').AUTH_TYPE;
const url = aws_exports.ENDPOINT;
const region = aws_exports.REGION;
const type = AUTH_TYPE.AMAZON_COGNITO_USER_POOLS;
AWS.config.update({
region: aws_exports.REGION,
credentials: new AWS.Credentials({
accessKeyId: aws_exports.AWS_ACCESS_KEY_ID,
secretAccessKey: aws_exports.AWS_SECRET_ACCESS_KEY
})
});
const cognitoIdentityServiceProvider = new AWS.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' });
const initiateAuth = async ({ clientId, username, password }) => cognitoIdentityServiceProvider.initiateAuth({
AuthFlow: 'USER_PASSWORD_AUTH',
ClientId: clientId,
AuthParameters: {
USERNAME: username,
PASSWORD: password,
},
}).promise();
const { AuthenticationResult } = await initiateAuth({
clientId: '*******',
username: '*******',
password: '*******',
});
const accessToken = AuthenticationResult && AuthenticationResult.AccessToken;
// Import gql helper and craft a GraphQL query
const gql = require('graphql-tag');
const query = gql(`
mutation faceAdded($input: UpdateFaceInput!) {
updateFace(input: $input) {
RekognitionId
Emotions {
Confidence
Type
}
AgeRange
Beard
Mustache
Gender
Eyeglasses
}
}`);
// Set up Apollo client
const client = new AWSAppSyncClient({
url: url,
region: region,
auth: {
type: type,
jwtToken: accessToken,
},
disableOffline: true //Uncomment for AWS Lambda
});
return runMutationInClient(client, query, event.Records[0], (res) => {
return res;
});
}
const runMutationInClient = async (client, query, RekognitionObj, callback) => {
await client.hydrated().then( async (client) => {
console.log('Running mutation in Lambda: ', query, 'RekognitionId: ', RekognitionObj);
var inputObj = AWS.DynamoDB.Converter.unmarshall(RekognitionObj.dynamodb.NewImage)
await client.mutate({
mutation: query,
variables: {
input: {
RekognitionId: {"S": inputObj.RekognitionId},
Emotion: {"L": []}
}
},
fetchPolicy: 'no-cache'
}).then(function logData(data) {
console.log('LAMBDA results of query: ', data);
callback(data);
})
.catch((error)=>{
console.log('LAMBDA ERROR:', error);
callback(error);
});
});
};