Say I have this simple model
type Customer @model {
id: ID!
name: String!
companyId: String!
}
Once i run amplify api push or gql-compile amplify will generate a graphql.schema in the 'amplify/backend/.../build' folder containing queries, mutations and subscriptions to get, create and update these models and to subscribe to changes and creations of them.
But, how do I modify or override what's in the built graphql.schema to do things like:
For a multi-tenant app something like companyId is the discriminator to ensure a user only sees and can work with data belonging to their company. Obviously I could write UI code to achieve this but I'd rather have it within my backend API.
Add companyId as a filter to subscriptions?
You can do this yourself by providing your own subscription type in your schema.graphql. E.G.
type Subscription {
onCreateCompany(companyId: String!): Company @aws_subscribe(mutations: ["createCompany"])
}
You can then turn off the generated subscriptions with @model(subscriptions: null)
.
Make companyId a required filter input when getting lists of customers?
You cannot override the structure that is output from the compilation but you will be able to write custom queries and mutations in the future that will support this use case (see #74).
Closing this issue. Please, feel free to re-open this issue if the problem still persists.
Support for custom queries and mutations is covered as a part of #74. Closing it as a duplicate
Hi @mikeparisstuff, I am trying your way and somehow it is not working. In the amplify auto-generated schema.graphql file, mutation uses its own input
type. Namely:
type Mutation {
createMessage(input: CreateMessageInput!): Message
}
Do we need to adjust Subscription's argument accordingly? Something like this?
type Subscription {
onCreateMessage(input: CreateMessageInput): Message @aws_subscribe(mutations: ["createMessage"])
}
In order to add arguments in Subscriptions, I also tried to use the method described here - https://docs.aws.amazon.com/appsync/latest/devguide/real-time-data.html. I tried to manually create mutations whose input are arguments instead of an input
type, and try to subscribe to the arguments. However, it doesn't work for me either.
Any help would be highly appreciated.
--------Update--------
It turns out that the subscription is using the _output_ (not the _input_) of the corresponding mutation, to trigger the subscription. In other words, the param used as the subscription argument MUST appear in the mutation result.
````
subscription subscribeToMessages {
onCreateMessage(authorId: "28244caf-5357-44f6-b42e-bb68455284c7"){ # <---- filtering authorId
id
content
}
}
mutation sendMessage {
createMessage(
content: "test"
authorId: "28244caf-5357-44f6-b42e-bb68455284c7"
) {
id
content
authorId # <------- this has to be here to make the above subscription work
}
}
````