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 79 forks source link

Mock API: Resolver for custom mutation not executing when using mock api #864

Open gxxcastillo opened 2 years ago

gxxcastillo 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?

v16.17.1

Amplify CLI Version

10.0.0

What operating system are you using?

M1 Mac w/ rosetta terminal

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

Amplify Categories

custom, api

Amplify Commands

add

Describe the bug

I've created a resolver for a custom query but it does not execute when using the mock api server. After doing a amplify push I've confirmed that the resolver does execute when I run my custom query on the AWS AppSync admin page.

I am creating a custom resolver using amplify add custom as described in the amplify cli docs. I am using vtl files as apposed to inlining the resolvers and have confirmed that the contents of the vtl file hydrated into the cdk-stack.js file when I run amplify build.

This other issue seems related: https://github.com/aws-amplify/amplify-category-api/issues/113

Expected behavior

I would expect the resolver to execute when I run my custom query against the mock api server.

Reproduction steps

  1. amplify int -> amplify add api
  2. generate default "single object with fields" api
  3. amplify add custom to create custom resolver
  4. update cdk-stack.ts to set api name and table name
  5. add custom mutation to schema file
  6. create request resolver with this as the content:$util.error("It was called!!")
  7. amplify mock api
  8. execute your custom mutation
  9. notice that the resolver was not called. Assuming your mutation were called "doStuff" the response is simply:
{
  "data": {
    "doStuff": null
  }
}

If you do amplify push and test on the AppSync admin page it should work.

GraphQL schema(s)

```graphql # Put schemas below this line input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY! type Todo @model { id: ID! name: String! description: String } type Mutation { doStuff(id: ID!): Todo } ```

Log output

``` # Put your logs below this line ```

Additional information

No response

josefaidt commented 1 year ago

Hey @gxxcastillo :wave: thanks for raising this! I'm able to reproduce this by adding a custom resolver and query to my schema and attempt to call it during mock

type Query {
  listSpecialTodos: [Todo]
}
import * as cdk from 'aws-cdk-lib'
import * as AmplifyHelpers from '@aws-amplify/cli-extensibility-helper'
import { AmplifyDependentResourcesAttributes } from '../../types/amplify-dependent-resources-ref'
import { Construct } from 'constructs'
import * as appsync from 'aws-cdk-lib/aws-appsync'

const queryListSpecialTodosReqVtl = `$util.error("It was called!!")`

export class cdkStack extends cdk.Stack {
  constructor(
    scope: Construct,
    id: string,
    props?: cdk.StackProps,
    amplifyResourceProps?: AmplifyHelpers.AmplifyResourceProps
  ) {
    super(scope, id, props)
    /* Do not remove - Amplify CLI automatically injects the current deployment environment in this input parameter */
    new cdk.CfnParameter(this, 'env', {
      type: 'String',
      description: 'Current Amplify CLI env name',
    })
    // Access other Amplify Resources
    const retVal: AmplifyDependentResourcesAttributes =
      AmplifyHelpers.addResourceDependency(
        this,
        amplifyResourceProps.category,
        amplifyResourceProps.resourceName,
        [
          {
            category: 'api',
            resourceName: 'a12032',
          },
        ]
      )

    const requestFunction = new appsync.CfnFunctionConfiguration(
      this,
      'function1',
      {
        apiId: cdk.Fn.ref(retVal.api.a12032.GraphQLAPIIdOutput),
        dataSourceName: 'NONE_DS', // DataSource name
        functionVersion: '2018-05-29',
        name: 'function1',
        requestMappingTemplate: queryListSpecialTodosReqVtl,
        responseMappingTemplate: queryListSpecialTodosReqVtl,
      }
    )

    const resolver = new appsync.CfnResolver(this, 'custom-resolver', {
      // apiId: retVal.api.new.GraphQLAPIIdOutput,
      // https://github.com/aws-amplify/amplify-cli/issues/9391#event-5843293887
      // If you use Amplify you can access the parameter via Ref since it's a CDK parameter passed from the root stack.
      // Previously the ApiId is the variable Name which is wrong , it should be variable value as below
      apiId: cdk.Fn.ref(retVal.api.a12032.GraphQLAPIIdOutput),
      fieldName: 'listSpecialTodos',
      typeName: 'Query',
      kind: 'PIPELINE',
      pipelineConfig: {
        functions: [requestFunction.attrFunctionId],
      },
      requestMappingTemplate: queryListSpecialTodosReqVtl,
      responseMappingTemplate: queryListSpecialTodosReqVtl,
      // requestMappingTemplate: requestVTL,
      // responseMappingTemplate: responseVTL,
      dataSourceName: 'TodoTable', // DataSource name
    })
  }
}

image

Marking as a bug