AWS AppSync - 指令" aws_subscribe"不能在FIELD_DEFINITION上使用

时间:2018-05-03 14:26:56

标签: amazon-web-services graphql aws-appsync

我试图掌握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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:4)

如果文件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变异的Swift实现,GetNearbyParcels查询和OnAddParcel预订

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