* Which Category is your question related to? *
graphql-transformer
* What AWS Services are you utilizing? *
AppSync / Dynamo
* Provide additional details e.g. code snippets *
I'm currently running into a situation where initially I had a model like follows:
type Post @model {
id: ID!
title: String!
}
type Response @model {
id: ID!
content: String!
post: Post @connection
}
This correctly stores a 'responsePostId' for each row in the Response table.
And then I decided I wanted access to 'response' from the Post model so I modified the schema to the following:
type Post @model {
id: ID!
title: String!
response: Response @connection(name: "PostResponse")
}
type Response @model {
id: ID!
content: String!
post: Post @connection(name: "PostResponse")
}
What this does is create a "postResponseId" attribute for every new model in the Posts table and uses $context.source.postResponseId to get Post.response. However I don't need that index, because to get the response for a post (through the Post.response resolver) I should just use $context.source.id and as the value for "responsePostId' in the Response data source.
Note if I change the Post schema to a one to many relationship:
type Post @model {
id: ID!
title: String!
responses: [Response] @connection(name: "PostResponse")
}
The Post.responses resolver correctly queries using $context.source.id rather than a $context.source.postResponseId.
I read https://aws-amplify.github.io/docs/cli/graphql?sdk=js and it talks about how to do bidirectional one-to-many relationship and a simple one-to-one relationship but there is no example in there for a bidirectional one-to-one. Am I missing something here. Maybe one-to-one bidirectional relationships are not supported because there is no way to enforce the strict 1:1ness in the database?
I'm looking at the code here: https://github.com/aws-amplify/amplify-cli/blob/master/packages/graphql-connection-transformer/src/ModelConnectionTransformer.ts#L132
And it looks like the case of {} to {} is not mentioned or handled. Why?
I'm new to AWS and I'm testing AppSync... I ended up with this same question.
Any answers?
This can be implemented via a BatchWrite operation or via an AppSync pipeline resolver but is not yet implemented out of the box via the GraphQL transform. To build this yourself, you may use the @connection directive to configure the has one relationship on both @model types and then implement your own updatePost & updateResponse resolvers such that they use a pipeline resolver or BatchWrite operation to write the id of the other object into both records in both tables.
@mikeparisstuff Do you have to do the same for create? Because I also tried this like this:
type Coordinates
@model @auth(rules: [{ allow: owner }]) {
id: ID!
latitude: Float!
longitude: Float!
address: Address! @connection(name: "AddressCoordinates")
}
type Address @model(queries: { get: "getAddress", list: "listAddresses" }) {
id: ID!
country: String!
city: String!
zipCode: String
streetName: String!
streetNumber: String
# notice the optional parameter below, because addresses exist first
coordinates: Coordinates @connection(name: "AddressCoordinates")
}
But when I first create an Address entity and then create Coordinates using to the Address's ID, it correctly points to the Addreess from Coordinates but not the other way around.
Would I also have to write a custom resolver for createCoordinates using batchWrite to write both the new Coordinates and populate the corresponding field for the coordinates id on the address' field?
@mikeparisstuff
Any plan on this will be available out of the box?
Ping. These are great questions. I am new to Amplify and ran into the same issue trying to model a one-to-one through the connection.
This video may help you : https://www.youtube.com/watch?v=eUQvsuO6EnU
This video may help you : https://www.youtube.com/watch?v=eUQvsuO6EnU
The video is useful to have a very clear overview about relationships implementation, but it doesn't help with this specific topic
I am also facing the same issue where i am not able to do one to one bidirectional relationship.
In my case i am trying to achieve the following, A User can only make one Post at a time and a Post can only have one User as its owner. Any suggestion on how to solve this @mikeparisstuff
I am on the same topic too! any solutions to that? Unless we implement ourselves using GraphQL lambda resolvers?
There should be an easier way to achieve these functionlaities without writing custom resolvers.
Any ideas? 馃槄 or Do we wait for amplify maintainers??
@gautamkshitij i guess we have to wait, but for now i switch over to aurora serverless, it as serverless relational database which makes things more closer to our traditional databases.
bump!
I have also run into this issue, would love some guidance on the best way to handle this.