1

I’m currently using GraphQL transform lib to generate all my schema. I have a model defined like this:

type Feedback @model {
  id: ID!
  event: Event! @connection(name: "EventFeedbacks")
  submittedDate: AWSDateTime!
}

and the auto-generated subscription schema is like this:

type Subscription {
    onCreateFeedback: Feedback
        @aws_subscribe(mutations: ["createFeedback"])
}

I would like to have an argument for the subscription so that I can subscribe to that event only, like this:

type Subscription {
    onCreateFeedback(eventId: ID): Feedback
        @aws_subscribe(mutations: ["createFeedback"])
}

What do I need to do to get this subscription auto generated? Thanks!

odieatla
  • 1,049
  • 3
  • 15
  • 35

2 Answers2

2

Customizing the subscription fields arguments is currently not supported. The only supported customization is to create multiple subscription fields tied to a single mutation.

Example:

type Feedback @model(subscriptions: { onCreate: ["onCreateFeedback", "onCreateFeedbackById"] }) {
  id: ID!
  event: Event! @connection(name: "EventFeedbacks")
  submittedDate: AWSDateTime!
}

will generate for the subscription type:

type Subscription {
    onCreateFeedback: Feedback
        @aws_subscribe(mutations: ["createFeedback"])
    onCreateFeedbackById: Feedback
        @aws_subscribe(mutations: ["createFeedback"])
}

but then you will have to add the eventId argument manually on the onCreateFeedbackById field.

Though, I would suggest to open a feature request in https://github.com/aws-amplify/amplify-cli/issues

Tinou
  • 5,908
  • 4
  • 21
  • 24
2

As @Tinou correctly outlines, you can rename and turn off subscription fields that are generated by @model using the subscriptions arg but you also the ability to create custom subscriptions by adding a Subscription type to your schema.

type Subscription {
    customField(arg: String): String @aws_subscribe(mutations:["customPublish"])
}

With this approach, you can add any fields and arguments that you need.

mparis
  • 3,623
  • 1
  • 17
  • 16
  • Before I was having error message like 'unknown directive @aws_subscribe'. Now I can compile after I switch to multienv branch of @aws-amplify/cli. Now the subscription with argument can be generated now, but no result is received after a mutation. Do I need to write a resolver for the subscription? – odieatla Jan 22 '19 at 19:00