Skip to content

Allow field-level resolvers to be added to the model #3306

@fossamagna

Description

@fossamagna

Is your feature request related to a problem? Please describe.

I have added a resolver to a specific field in the model.

For example, suppose I have a text stored in S3 that is too large to register in a DynamoDB field, and in the Amplify Gen2 model I want to add a field for the S3 key.
I want to additionally add a field to the model that has the contents of the object of that S3 key.

In Gen1, I could add a custom Lambda resolver to the additional field by adding the @function directive to the field. Within that Lambda, I was able to retrieve the object from S3 and return the value of the additional field.

I have not found a way to accomplish the same thing with schema-building with amplify-data in Gen2.

Solution in Gen1:

type Asset @model {
  #
  # S3 Key of this asset
  #
  name: String!
  #
  # Type of this asset
  #
  type: String!
  #
  # Content of this asset
  #
  content: String! @function("assetContent")
}

Describe the solution you'd like

I would like to be able to add a handler to the field as follows

import { a, defineData, defineFunction } from "@aws-amplify/backend";

const assetContentHandler = defineFunction({
  entry: "./assetContent-handler/handler.ts",
});

const schema = a.schema({
  Asset: a
    .model({
      name: a.string(),
      type: a.string(),
      // In fact, a.url().handler(...) cannot be written this way because there is no API like
      content: a.string().handler(a.handler.function(assetContentHandler)),
    })
    .authorization((allow) => [allow.publicApiKey()]),
});

Describe alternatives you've considered

Add a resolver using CDK API.

Example: https://github.com/fossamagna/amplify-gen2-resolvers-sample/blob/40a937fcbd0e3732aa946dc01c3ee7b96d415dc5/amplify/add_field_resolver.ts

Additional context

n/a

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions