Closed sky4git closed 3 years ago
I deleted my comment before about API.post
because I totally forgot we had an @http
directive and that's what you're using lol
Did you follow the docs here and try to do the same with Mutation? It seems Query is dedicated to handling GET requests where Mutation handles POST, PUT, and PATCH. I think the end result should be the same though.
Yes, I tried with Mutation too. It ends up with the same bug of MappingTemplate.
What is the API Gateway response supposed to look like?
Haven't been able to reproduce the issue but this setup worked for me.
Schema
type Mutation {
addPost(title: String!, description: String!, views: Int): String
@http(
method: POST
url: "https://xxxxxxxx.execute-api.us-east-2.amazonaws.com/dev/post"
)
}
API Call
const res = await API.graphql(
graphqlOperation(addPost, {
body: {
title: "Testing My API Gateway",
description: 'this should return a string of "post added"',
views: 1,
},
})
);
Lambda
exports.handler = async(event, ctx) => {
console.log("EVENT\n" + JSON.stringify(event, null, 2))
console.info("CONTEXT\n" + JSON.stringify(ctx, null, 2))
const response = "post added"
return response;
};
Response
whoops, did not mean to close. Was trying to make another comment lol
Was going to say the issue might be in the VTL file that was generated.
The path to the relevant file in your project should be similar to this 8586-graphql/amplify/backend/api/8586graphql/build/resolvers/Mutation.addPost.res.vtl
#if( $ctx.result.statusCode == 200 || $ctx.result.statusCode == 201 )
#if( $ctx.result.headers.get("Content-Type").toLowerCase().contains("xml") )
$utils.xml.toJsonString($ctx.result.body)
#else
$ctx.result.body
#end
#else
$util.qr($util.appendError($ctx.result.body, $ctx.result.statusCode))
#end
Compare the above to your file and make sure that it's returning ctx.result.body
When API Gateway resource/method has IAM authorisation on, in that case it gives "mapping template" error. Without IAM authorisation, it will work fine.
So in the case of, IAM authorisation required where should we need to pass the "Authorization" header.
@sky4git I think your return value will have to be one of your models already authorized by IAM. Otherwise, I'm not sure that it's possible because of this line in the documentation for the @http
directive.
https://docs.amplify.aws/cli/graphql-transformer/http/#generates
Note that @http transformer does not support calling other AWS services where Signature Version 4 signing process is required.
Here's some relevant documentation for the IAM authorization mode
https://aws.amazon.com/blogs/mobile/graphql-security-appsync-amplify/
Yes. It doesn't seem to work for Authorised API gateway methods.
It would be good if we can apply the IAM policy from amplify-cli when we want to invoke API gateway method.
Hi! I am having a very simmilar problem with the appsync application, I have it pointing the a REST endpoint with no auth however im using custom resolvers instead of the http directive.
This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.
Looking for a help forum? We recommend joining the Amplify Community Discord server amplify-help
forum.
Before opening, please confirm:
JavaScript Framework
Next.js
Amplify APIs
GraphQL API
Amplify Categories
api
Environment information
Describe the bug
I have defined the GraphQL query in schema.json like the following:
With the query I want to invoke/execute API Gateway endpoint with POST method and get some JSON response.
Above schema.graphql query generates a query definition in src/queries.ts like the following:
Now using this in function to run GraphQL Query. I have define it like the following:
The ERROR: I always get response as error:
Expected behavior
await API.graphql should return some response from API Gateway, instead it looks like AppSync is not understanding what data the API.graphql function is sending to it.
Reproduction steps
Check Describe the Bug section.
Code Snippet
Log output
aws-exports.js
No response
Manual configuration
No response
Additional configuration
No response
Mobile Device
No response
Mobile Operating System
No response
Mobile Browser
No response
Mobile Browser Version
No response
Additional information and screenshots
There is no documentation about GraphQL type Query with POST method usage with amplify js anywhere. If there is a working example somewhere, I am happy to look at it.