amazon-web-services - AWS AppSync - 指令“aws_subscribe”不能用于 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
推荐阅读
- xsl-fo - 调整块容器内块的垂直位置
- c# - Nuget 包动态 ConnectionString
- html - index.html 尝试从根 URI 解析 css 文件
- regular-language - 是 L = {ww^Ru | w, u ∈ {0,1}+} 正则语言?
- typescript - 声明一个类型,允许其他类型的所有级别的所有部分
- python - django 返回“AnonymousUser”对象没有属性“_meta”错误
- c++ - 长字符串类型未正确保存
- prisma - 如何自定义 prisma graphQL 模式以不自动生成突变?
- android - 在 React Native 中实现通话录音
- javascript - 如何在浏览器上使用 JavaScript 或其他框架显示一个人在室内行走(仅二维平面图上的一些点)?