aws-amplify - 无权访问 IFreightDriver 类型上的 id

标签 aws-amplify aws-appsync aws-amplify-cli

我定义了一个自定义查询及其自定义响应,如下所示:

input ILocations {
  id: String
  address: String
  latitude: String
  longitude: String
  city: String
  state: String
}

type IBox {
  id: ID
  type: String
  depth: Float
  height: Float
  width: Float
}

type ITruck {
  id: ID
  basePrice: Float
  brand: String
  model: String
  yearModel: Int
  axes: Int
  hasGPS: Boolean
  tons: Float
  utilityPercentage: Float
  fuelEfficiency: Float
  fuelType: FuelType
  frontPhoto: String
  sidePhoto: String
  backPhoto: String
  measuresPhoto: String
  truckBoxId: ID
  truckFreightDriverId: ID
  box: IBox
}

type IFreightDriver {
  id: ID
  state: String
  city: String
  truck: ITruck
}

type Query {
  quotations(
    origin: ILocations
    destination: ILocations
    boxType: String
    boxSize: Float
    truckTons: Float
  ): [IFreightDriver]
    @function(name: "quotations-${env}")
    @auth(rules: [{ allow: public, provider: iam }])
}

如果我在 AppSync 仪表板上执行此查询:

query {
  quotations (
    origin: {
        address: "sdas"
        latitude: "sdas"
        longitude: "dsa"
        city: "Guadalajara",
        state: "Jalisco"
    },
    destination: {
      address: "sdas"
      latitude: "sdas"
      longitude: "dsa"
      city: "Zapopan",
      state: "Jalisco"
    },
    boxType: "Seca (cerrada)",
    boxSize: 64,
    truckTons: 12
  ) {
    id
  }
}

我收到有关权限的错误:

{
  "data": {
    "quotations": [
      {
        "id": null
      }
    ]
  },
  "errors": [
    {
      "path": [
        "quotations",
        0,
        "id"
      ],
      "data": null,
      "errorType": "Unauthorized",
      "errorInfo": null,
      "locations": [
        {
          "line": 20,
          "column": 5,
          "sourceName": null
        }
      ],
      "message": "Not Authorized to access id on type IFreightDriver"
    }
  ]
}

但是,如果我对 lambda 的响应进行字符串化,并更改查询以接收字符串,如下所示:

  quotations(
    origin: ILocations
    destination: ILocations
    boxType: String
    boxSize: Float
    truckTons: Float
  ): String
    @function(name: "quotations-${env}")
    @auth(rules: [{ allow: public, provider: iam }])

我正确收到响应...

{
  "data": {
    "quotations": "[{\"id\":\"19847d0c-185c-48f6-9e5c-435e3907133a\",\"state\":\"Jalisco\",\"city\":\"Zapopan\",\"truck\":{\"__typename\":\"Truck\",\"model\":\"1234\",\"tons\":12,\"yearModel\":2020,\"measuresPhoto\":\"freight-driver/19847d0c-185c-48f6-9e5c-435e3907133a/trucks/qzo1x9g7kvb51.jpg\",\"truckFreightDriverId\":\"19847d0c-185c-48f6-9e5c-435e3907133a\",\"createdAt\":\"2020-08-13T21:02:36.305Z\",\"brand\":\"chevrolet\",\"fuelEfficiency\":12,\"truckBoxId\":\"b3d9000f-0464-4c3d-b436-f380b19d6fd5\",\"backPhoto\":\"freight-driver/19847d0c-185c-48f6-9e5c-435e3907133a/trucks/QCzn1_mWetzZjAFavJYSaug4F2IMAeOAzJa7T6rhC5Q.jpg\",\"frontPhoto\":\"freight-driver/19847d0c-185c-48f6-9e5c-435e3907133a/trucks/dlb0275xqna51.png\",\"utilityPercentage\":12,\"updatedAt\":\"2020-08-13T21:02:36.305Z\",\"sidePhoto\":\"freight-driver/19847d0c-185c-48f6-9e5c-435e3907133a/trucks/ebcpyk19csa51.jpg\",\"fuelType\":\"premium\",\"hasGPS\":true,\"id\":\"b7ec3b05-78c6-4ed9-823d-172a1e9991e5\",\"axes\":12,\"box\":{\"__typename\":\"Box\",\"width\":4,\"height\":4,\"updatedAt\":\"2020-08-13T21:02:35.840Z\",\"depth\":4,\"createdAt\":\"2020-08-13T21:02:35.840Z\",\"id\":\"b3d9000f-0464-4c3d-b436-f380b19d6fd5\",\"type\":\"Seca (cerrada)\"}},\"basePrice\":null}]"
  }
}

为什么要求这些权限?如果有人需要我收到的回复:

[
    {
        "id": "19847d0c-185c-48f6-9e5c-435e3907133a",
        "state": "Jalisco",
        "city": "Zapopan",
        "truck": {
            "__typename": "Truck",
            "model": "1234",
            "tons": 12,
            "yearModel": 2020,
            "measuresPhoto": "freight-driver/19847d0c-185c-48f6-9e5c-435e3907133a/trucks/qzo1x9g7kvb51.jpg",
            "truckFreightDriverId": "19847d0c-185c-48f6-9e5c-435e3907133a",
            "createdAt": "2020-08-13T21:02:36.305Z",
            "brand": "chevrolet",
            "fuelEfficiency": 12,
            "truckBoxId": "b3d9000f-0464-4c3d-b436-f380b19d6fd5",
            "backPhoto": "freight-driver/19847d0c-185c-48f6-9e5c-435e3907133a/trucks/QCzn1_mWetzZjAFavJYSaug4F2IMAeOAzJa7T6rhC5Q.jpg",
            "frontPhoto": "freight-driver/19847d0c-185c-48f6-9e5c-435e3907133a/trucks/dlb0275xqna51.png",
            "utilityPercentage": 12,
            "updatedAt": "2020-08-13T21:02:36.305Z",
            "sidePhoto": "freight-driver/19847d0c-185c-48f6-9e5c-435e3907133a/trucks/ebcpyk19csa51.jpg",
            "fuelType": "premium",
            "hasGPS": true,
            "id": "b7ec3b05-78c6-4ed9-823d-172a1e9991e5",
            "axes": 12,
            "box": {
                "__typename": "Box",
                "width": 4,
                "height": 4,
                "updatedAt": "2020-08-13T21:02:35.840Z",
                "depth": 4,
                "createdAt": "2020-08-13T21:02:35.840Z",
                "id": "b3d9000f-0464-4c3d-b436-f380b19d6fd5",
                "type": "Seca (cerrada)"
            }
        },
        "basePrice": null
    }
]

更新:我还尝试更改 lambda 响应以仅返回 ids 数组,如下所示:

来自 lambda 的响应: [ { id: '19847d0c-185c-48f6-9e5c-435e3907133a' }]

来自 AppSync 的错误:

{
  "data": {
    "quotations": [
      {
        "id": null
      }
    ]
  },
  "errors": [
    {
      "path": [
        "quotations",
        0,
        "id"
      ],
      "data": null,
      "errorType": "Unauthorized",
      "errorInfo": null,
      "locations": [
        {
          "line": 20,
          "column": 5,
          "sourceName": null
        }
      ],
      "message": "Not Authorized to access id on type IFreightDriver"
    }
  ]
}

schema.graphql:

type IFreightDriver {
  id: ID
}

type Query {
  quotations(
    origin: ILocations
    destination: ILocations
    boxType: String
    boxSize: Float
    truckTons: Float
  ): [IFreightDriver]
    @function(name: "quotations-${env}")
    @auth(rules: [{ allow: public, provider: iam }])
}

最佳答案

您必须提供访问 IFreightDriver 的权限。由于您正在为查询设置 auth,而不是在它正在访问/返回的对象上设置(在您的情况下为 IFreightDriver),因此它会引发权限错误。但是,当您将查询切换为仅返回字符串时,您不需要为此设置任何权限,这就是您看不到任何错误的原因。当您仅返回 IFreightDriverid 时,情况也是如此。除非您在 IFreightDriver 上设置权限,否则它不会允许您访问 ID。

关于aws-amplify - 无权访问 IFreightDriver 类型上的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63442276/

相关文章:

ios - AWS 放大添加身份验证 : how to add a redirect signin URI after executing the amplify cli

python - 如何在 Amplify Python Lambda 函数上本地更新依赖项?

amazon-web-services - AWS AppSync RDS : $util. rds.toJSONObject() 嵌套对象

amazon-web-services - 如何操作 AWS AppSync 和 GraphQL 以符合 DynamoDB 最佳实践?

android - 放大授权: Unable to add User to the Pool

amazon-cognito - 是否可以在不使用 Amazon Cognito 的情况下使用 AWS Amplify?

amazon-web-services - 基于位置的AWS AppSync事件订阅

amazon-web-services - 使用 ElasticSearch 的 AWS DynamoDB 地理空间查询

android - 在此文件夹中未检测到 Amplify 后端项目文件

javascript - Electron Amplify AppSync "Realtime disabled when in a server-side environment"