aws-amplify / amplify-category-api

The AWS Amplify CLI is a toolchain for simplifying serverless web and mobile development. This plugin provides functionality for the API category, allowing for the creation and management of GraphQL and REST based backends for your amplify project.
https://docs.amplify.aws/
Apache License 2.0
89 stars 77 forks source link

Mock API result is different than result on AppSync console #808

Open tb102122 opened 2 years ago

tb102122 commented 2 years ago

Before opening, please confirm:

How did you install the Amplify CLI?

Npm

If applicable, what version of Node.js are you using?

No response

Amplify CLI Version

10.0.0

What operating system are you using?

Windows

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No manual changes made

Amplify Categories

api

Amplify Commands

Not applicable

Describe the bug

I am creating a custom resolver to extend my automatic generated mutationCreate resolvers. The custom extension queries a dynamodb table and evaluates the result based on the result the update is executed or a error is raised. The logic works correctly while running some tests via the AWS console but when running amplify mock api I get wrong results. Based on my analysis so far the result of the following line of code is different via the AppSync and mock.

$ctx.result.items.size() == 0

AppSync returns [] for $ctx.result.items While mock returns null for $ctx.result.items

Expected behavior

Results are consistent between mock and AppSync to allow local testing

Reproduction steps

  1. Run test via AppSync console
  2. Run test via mock api

GraphQL schema(s)

No response

Project Identifier

No response

Log output

No response

Additional information

No response

josefaidt commented 2 years ago

Hey @tb102122 :wave: thanks for raising this! To better understand the context of your issue would you mind sending the project identifier output from amplify diagnose --send-report?

tb102122 commented 2 years ago

@josefaidt Sure no problem here you go Project Identifier: 51a20327a332cd6417119f4d26ea6cdd

tb102122 commented 2 years ago

@josefaidt @ykethan do you have any further questions? I also have noticed a strange behavior in the update mutation resolver which I dont understand. What way to you suggest to share the details to clarify it.

josefaidt commented 2 years ago

Hey @tb102122 apologies for the delay here. Would you mind providing an example of an app where you're experiencing this issue? I'm seeing $ctx.result.items return [] in an error message, using the following resolver to test (Query.listTodos.res.vtl):

## [Start] ResponseTemplate. **
#if( $ctx.error )
  $util.error($ctx.error.message, $ctx.error.type)
#else
  $util.error($ctx.result.items)
#end
## [End] ResponseTemplate. **
image
tb102122 commented 2 years ago

Hey @josefaidt these are the following steps to reproduce.

  1. create schema as below
  2. update resolves based on definition attached backupResolvers.zip
  3. run amplify mock
  4. run below query and mutation
type Class @model {
  id: ID!
  Name: String! @index(name: "byClassName", queryField: "classByName", sortKeyFields: ["id"])
  Note: String
 }

query MyQuery {
  listClasses {
    items {
      Name
      id
    }
  }
}

mutation MyMutation {
  createClass(input: {Name: "Heron"}) {
    Name
    id
  }
}

Expected result first mutation createClass runs successful the 2nd mutation createClass with the same name should raise an error. When I run the setup via the APPSync console it works but in the mock API it does not work. For the update process nothing works and there is no update done of the record.

josefaidt commented 2 years ago

Hey @tb102122 I was able to reproduce the issue using the provided steps and sample files, schema. Thank you for clarifying! Additionally, I am only able to receive the error after restarting the mock server, otherwise I am allowed to create as many records as I want.

"Name already existing please pick a different name."

After pushing to AWS and navigating to the AppSync console I am not able to create a record, receiving the following:

{
  "data": {
    "createClass": null
  },
  "errors": [
    {
      "path": [
        "createClass"
      ],
      "data": null,
      "errorType": "MappingTemplate",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Unsupported element '$[operation]'."
    }
  ]
}

I also noticed a separate issue when reproducing this, which I've documented here https://github.com/aws-amplify/amplify-category-api/issues/849

tb102122 commented 2 years ago

@josefaidt thanks for the update and the other finding did you also try the update why it is not working? Not sure from where you are getting the error "message": "Unsupported element '$[operation]'." from I am not using the name in any of the resolvers. Would it help to jump on a short call to check the details within my setup?

I also realized that the naming's in AppSync are not consistent:

AWS AppSync > XXX > Schema > Mutation > CreateClass > PipelineResolver [MutationcreateStartSequenceinit0Function] [MutationcreateClassauth0Function] [QuerygetStartSequencepostAuth0Function] [MutationCreateClassDataResolverFn]

The StartSequence is another table in my schema linked to the table Class.

josefaidt commented 2 years ago

Hey @tb102122 apologies for the delay, but yes I'm open to hop on a quick call! Are you on our community Discord server? My username there is josef

josefaidt commented 2 years ago

Hey @tb102122 :wave: after discussing with the team, the inconsistent naming you are seeing for the pipeline resolver is coming from an effort to re-use pipeline resolvers and reduce the number of resources we need to push. I'm still investigating the $[operation] issue, though

tb102122 commented 2 years ago

thanks for the updated

tb102122 commented 1 year ago

Is there any update on the investigation? @josefaidt

tb102122 commented 1 year ago

Do you have any update on the progress?

tb102122 commented 1 year ago

@josefaidt do you have any further update on this issue?