AWS AppSync - 指令 "aws_subscribe" 不能用于 FIELD_DEFINITION

AWS AppSync - Directive "aws_subscribe" may not be used on FIELD_DEFINITION

我正在尝试掌握 AWS AppSync。我对 GraphQL 很陌生。我有以下 GraphQL:

type Mutation {
    deleteParcel(geoHash: String!, type_id: String!): Parcel
    addParcel(input: ParcelInput!): Parcel
    batchAddParcels(parcels: [ParcelInput]): [Parcel]
}

type Parcel {
    geoHash: String!
    type_id: String!    
}

type ParcelConnection {
    items: [Parcel]
}

input ParcelInput {
    geoHash: String!
    type_id: String!    
}

input ParcelsInput {
    parcels: [ParcelInput]
}

type Query {
    getNearbyParcels(geoHash: String!): ParcelConnection
}

type Subscription {
    onAddParcel(geoHash: String, type_id: String): Parcel
        @aws_subscribe(mutations: ["addParcel"])
    onBatchAddParcels(geoHash: String): Parcel
        @aws_subscribe(mutations: ["batchAddParcels"])
    onDeleteParcel(geoHash: String, type_id: String): Parcel
        @aws_subscribe(mutations: ["deleteParcel"])
}

schema {
    query: Query
    mutation: Mutation
    subscription: Subscription
}

AWS 控制台上的所有设置似乎都很好。我得到 schema.json 然后 运行 命令:

aws-appsync-codegen generate AWSGraphQL.graphql --schema schema.json --output AppsyncAPI.swift 并得到响应:

../SnatchHQ/snatch_appsync/AppSync/AWSGraphQL.graphql: Directive "aws_subscribe" may not be used on FIELD_DEFINITION. .../SnatchHQ/snatch_appsync/AppSync/AWSGraphQL.graphql: Directive "aws_subscribe" may not be used on FIELD_DEFINITION. .../SnatchHQ/snatch_appsync/AppSync/AWSGraphQL.graphql: Directive "aws_subscribe" may not be used on FIELD_DEFINITION. error: Validation of GraphQL query document failed

有人能帮忙吗?

如果文件 AWSGraphQL.graphql 是您的 API GraphQL 架构,那么就可以解释问题。您需要做的是定义一个 *.graphql 文件,该文件根据您的 GraphQL API 定义您的查询、变更和订阅操作。例如,以下查询定义将匹配您的架构

mutation AddParcel($geoHash: String!, $type_id: String!) {
    addParcel(input: {
        geoHash: $geoHash
        type_id: $typeId
    }) {
        ...Parcel
    }
}

query GetNearbyParcels($geoHash: String!) {
    getNearbyParcels(
        geoHash: $geoHash
    ) {
        ...ParcelConnection
    }
}

subscription OnAddParcel {
    onAddParcel {
        ...Parcel
    }
}

fragment Parcel on Parcel {
    geoHash
    type_id
}

fragment ParcelConnection on Parcel Connection {
    items {
        ...Parcel
    }
}

假设您将其命名为 parcels.graphql,然后您可以调用以下命令生成 AddParcel 突变、GetNearbyParcels 查询和 OnAddParcel 订阅的 Swift 实现

aws-appsync-codegen generate parcels.graphql \ 
    --schema schema.json \
    --output AppSyncParcelsAPI.swift