AWS AppSync - Директива "aws_subscribe" не может использоваться в FIELD_DEFINITION - PullRequest
0 голосов
/ 03 мая 2018

Я пытаюсь справиться с 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 голосов
/ 09 мая 2018

Если файл AWSGraphQL.graphql является вашей схемой API GraphQL, то это объясняет проблему. Вам нужно определить файл *.graphql, который определяет ваши запросы, мутации и операции подписки на основе вашего API GraphQL. Например, следующие определения запроса будут соответствовать вашей схеме

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, вы можете вызвать следующее для генерации реализации Swift мутации AddParcel, запроса GetNearbyParcels и подписки OnAddParcel

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