Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are many-to-many connections supported? #254

Closed
rodrigoelp opened this issue Dec 12, 2019 · 6 comments
Closed

Are many-to-many connections supported? #254

rodrigoelp opened this issue Dec 12, 2019 · 6 comments
Labels
closing soon This issue will be closed in 7 days unless further comments are made. datastore Issues related to the DataStore category pending-community-response Issue is pending response from the issue requestor

Comments

@rodrigoelp
Copy link

I have been unable to create a many-to-many relationship with the current api.

I started with the following model:

type Person @model {
  id: ID!
  fullName: String!
  nationality: [Nationality] @connection(keyName: "byCountry", fields: ["id"])
}

type Nationality
  @model(queries: null)
  @key(name: "byCountry", fields: ["personID", "countryID"])
  @key(name: "byCitizen", fields: ["countryID", "personID"]) {
  id: ID!
  personID: ID!
  countryID: ID!
  person: Person! @connection(fields: ["personID"])
  country: Country! @connection(fields: ["countryID"])
}

type Country @model {
  id: ID!
  name: String!
  citizens: [Nationality] @connection(keyName: "byCitizen", fields: ["id"])
}

The model generation will create an instance:

public struct Nationality: Model {
  public let id: String
  public var person: Person
  public var country: Country
  
  public init(id: String = UUID().uuidString,
      person: Person,
      country: Country) {
      self.id = id
      self.person = person
      self.country = country
  }
}

Which seems perfectly fine until you call the mutation and get the following error:

[Amplify.GraphQLError(message: "Variable \'input\' has coerced Null value for NonNull type \'CountryInput!\'", locations: Optional([Amplify.GraphQLError.Location(line: 1, column: 41)]), path: nil, extensions: nil)]

Trying out the endpoint directly in console/AppSync/Query I can see the mutation requires me to provide more than just the ids, it requires the the instances of person and country as well and it seems the generated code isn't doing it for me.

Is this a bug of the generator, a more fundamental issue or am I doing something wrong here?

@rodrigoelp
Copy link
Author

Providing a bit more information on this ticket, I checked the documentation trying to understand why this is an issue.

In this particular scenario I have modelled the many to many relationship as two one to many relationships as specified by Nationality, yet the attempt to insert data in that connection seems completely broken.

@drochetti drochetti added datastore Issues related to the DataStore category pending-community-response Issue is pending response from the issue requestor labels Jan 2, 2020
@drochetti
Copy link
Contributor

Right now the only way to achieve many-to-many is like you did, define a model so you connect two other models via one-to-many. Automatic (or implicit) many-to-many mapping is something that we intend to support in the future but no concrete plans as of now.

Regarding the error you're seeing, can you share the code you're using to execute the mutation?

@rodrigoelp
Copy link
Author

If you are talking about the swift code to execute the mutation, here it is:

let person: Person // This was fetched from the api.
let country: Country // this was also fetched from the api.

let nationality = Nationality()
nationality.person = person
nationality.country = country

Amplify.API.mutate(of: nationality, type: .create) { event in
    switch event {
        case .completed(let result):
            print(String(describing: result))
        case .failed(let error):
            print("Why it does NOT WORK?")
            print(String(describing: error))
            print(String(describing: event))
        default:
            print("Ignoring this scenario for now.")
    }
}

Both the instance of persona and country where created just before inserting the nationality, chained as part of the callback for each of its mutate methods.

As I mentioned before, this definition is using the belongsTo documentation. I explained in #277 that this functionality seems to be broken as I have not been able to use this definition at all.

@drochetti drochetti added follow up Requires follow up from maintainers and removed pending-community-response Issue is pending response from the issue requestor labels Jun 24, 2020
@palpatim
Copy link
Member

@rodrigoelp Is this still an issue with the latest released version of Amplify?

@palpatim palpatim added pending-community-response Issue is pending response from the issue requestor closing soon This issue will be closed in 7 days unless further comments are made. and removed follow up Requires follow up from maintainers labels Jun 26, 2020
@rodrigoelp
Copy link
Author

Hi @palpatim, I haven't checked to be honest. We moved away from amplify and now I don't have access to an environment I could use to test this.

@palpatim
Copy link
Member

Thanks for the update. We've successfully tested many-many connections in the GA version of Amplify and haven't seen issues like you described, so I'm closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closing soon This issue will be closed in 7 days unless further comments are made. datastore Issues related to the DataStore category pending-community-response Issue is pending response from the issue requestor
Projects
None yet
Development

No branches or pull requests

3 participants