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

graphql-api-construct addResolver for Query failing #2346

Open jimjiminyjimjim opened 8 months ago

jimjiminyjimjim commented 8 months ago

Amplify CLI Version

12.2.5

Question

I can successfully add Appsync JS resolvers for any field in my api schema. However, if I try and add my own resolver for a Query such as getRegistryItem or listRegistryItems I receive the following error on deploy:

Resource handler returned message: "Resource already exists: arn:aws:appsync:eu-west-2:*****:apis/**/types/Query/resolvers/getRegistryItem"

Here is my resolver code:

    const registryItemTable = Table.fromTableName(
      this,
      "RegistryItemTable",
      amplifyApi.resources.cfnResources.cfnTables["RegistryItem"].ref
    );

    const registryItemDS = amplifyApi.addDynamoDbDataSource(
      "RegistryItemDataSource",
      registryItemTable
    );

    amplifyApi.addResolver("ListRegistryItems", {
      dataSource: registryItemDS,
      typeName: "Query",
      fieldName: "listRegistryItems",
      code: AppsyncCode.fromAsset(
        path.join(__dirname, "resolvers/registryItem/query", "listRegistryItems.js")
      ),
      runtime: FunctionRuntime.JS_1_0_0,
    });

Am I doing something wrong? I've tried disabling the creation of the query in the schema but then I get the following error:

"No field named listRegistryItems found on type Query"

jimjiminyjimjim commented 8 months ago

UPDATE:

After looking more at this I have worked out how to do the following:


  const amplifyApi = new AmplifyGraphqlApi(this, "AmplifyCdkGraphQlApi", {
      //...rest of graphql definition
      functionSlots: [queryFunctionSlot]
    const existingResolverResource = amplifyApi.resources.cfnResources.cfnResolvers['Query.listRegistryItems'];
    existingResolverResource.addPropertyOverride('ResponseMappingTemplate', `$util.toJson($ctx.result)`);

But I don't seem to be able to override or add an appSync resolver for the main data request or response function of a generated query by the api definition?

Using addResolver seems to work on listRegistryItems if I define a custom Query or Mutation in the schema and disable the generated one like this but ideally I'd like to do something to override the generated Query.listRegistryItems.req.vtl that I would do normally do with Amplify:


type RegistryItem
  @model #(queries: { list: null })
  ) {
  id: ID!
  name: String
  version: Int
}

type Query {
  listRegistryItems(id: ID!): String
 }

Any further information on this would be much appreciated.