admin管理员组

文章数量:1122923

I'm using AWS Amplify with the CLI (version 12.13.1) and the Amplify backend library to generate code for my project. Everything was working well until I added a new Owner: User field in the Event model. After this change, when I try to fetch invites with the listInviteByUserId query, I get the following error:

"graphQLErrors": [
    {
        "path": [
            "listInviteByUserId",
            "items",
            7,
            "event",
            "Owner"
        ],
        "data": null,
        "errorType": "MappingTemplate",
        "errorInfo": null,
        "locations": [
            {
                "line": 9,
                "column": 9,
                "sourceName": null
            }
        ],
        "message": "Value for field '$[operation]' not found."
    }
]

Here is the relevant part of my GraphQL schema:

type User
  @model
  @auth(
    rules: [
      { allow: private, provider: iam }
      { allow: owner }
    ]
  ) {
  id: ID
  owner: ID
  username: String
  name: String
}

type Event
  @model
  @auth(
    rules: [
      { allow: private, provider: iam }
      { allow: owner }
    ]
  ) {
  id: ID
  owner: ID
  Owner: User @hasOne(fields: ["owner"])
  userId: ID
    @index(
      name: "listEventByUserId"
      queryField: "listEventByUserId"
      sortKeyFields: ["createdAt"]
    )
  name: String
  description: String
  partners: [Event] @hasMany(fields: ["partnerIds"])
  partnerIds: [ID]
}

type Invite
    @model
    @auth(
        rules: [
            { allow: private, provider: iam }
            { allow: owner, operations: [read] }
        ]
    ) {
    id: ID
    owner: ID
    event: Event @hasOne(fields: ["eventId"])
    user: User @hasOne(fields: ["userId"])
    eventId: ID @index(name: "listInviteByEventId", queryField: "listInviteByEventId", sortKeyFields: ["createdAt"])
    userId: ID @index(name: "listInviteByUserId", queryField: "listInviteByUserId", sortKeyFields: ["createdAt"])
    isAccepted: Boolean
}

What I’ve tried:

Custom Resolver for Owner Field:

  • I created a custom resolver (amplify/backend/api/GraphApi/resolvers/Event.Owner.req.vtl) to resolve the Owner field. Here's the code I added:
## $util.toJson({"version":"2018-05-29","payload":{}})

#if( $ctx.stash.deniedField )
  #return($util.toJson(null))
#end
#set( $partitionKeyValue = $ctx.source.owner ) 
#if( $util.isNull($partitionKeyValue) )
  #return
#else
  #set( $idValue = $util.defaultIfNullOrBlank($partitionKeyValue, "___xamznone____") )
  
  #set( $GetRequest = {
  "version": "2018-05-29",
  "operation": "GetItem"
} )
  $util.qr($GetRequest.put("key", {
  "id": {
      "S": "$idValue"
  }
}))
  #if( !$util.isNullOrEmpty($ctx.stash.authFilter) )
    $util.qr($GetRequest.put("filter", $util.parseJson($util.transform.toDynamoDBFilterExpression($ctx.stash.authFilter))))
  #end
  $util.toJson($GetRequest)
#end
  • Modified Auth Rules: I also tried modifying the auth rules in the User model, but this didn’t resolve the issue.

However, when I query for partners in the Event model (which also uses a @hasMany relationship), it works correctly. The issue seems to be isolated to the Owner field in the Event model.

What am I missing? Why does fetching the Owner field from the Event model cause this error, while fetching related fields like partners works fine? Any insights on how to fix the issue with the custom resolver or GraphQL setup would be greatly appreciated!

Edit

I added a new model and associated the User model on field owner and it worked successfully. But when it generated gql for Event model it's not working as expected.

本文标签: aws appsyncAWS Amplify CLIGenerating wrong resolversStack Overflow