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

Many To Many Relation not working #5802

Open
3 of 14 tasks
HusseinReda97pro opened this issue Jan 14, 2025 · 2 comments
Open
3 of 14 tasks

Many To Many Relation not working #5802

HusseinReda97pro opened this issue Jan 14, 2025 · 2 comments
Labels
bug Something is not working; the issue has reproducible steps and has been reproduced datastore Issues related to the DataStore Category GraphQL API Issues related to the API (GraphQL) Category

Comments

@HusseinReda97pro
Copy link

Description

when I declare Many To Many Relation and query models many-to-many list come with null value

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

GraphQL scheam

type Category @model @auth(rules: [{ allow: public }]) {
id: ID!
name: String!
order: Int
people: [Person] @manytomany(relationName: "PersonCategories")

}

type Taxonomy @model @auth(rules: [{ allow: public }]) {
id: ID!
name: String!
emoji: String
people: [Person] @manytomany(relationName: "PersonTaxonomies")
}

type Person @model @auth(rules: [{ allow: public }]) {
id: ID!
first_name: String!
last_name: String!
bio: String
city: String
country: String
image: String
job_title: String
organization: String
taxonomies: [Taxonomy] @manytomany(relationName: "PersonTaxonomies")
categories: [Category] @manytomany(relationName: "PersonCategories")
is_featured: Boolean
is_discoverable: Boolean
}

query person

final persons = await Amplify.DataStore.query(Person.classType);
print(persons.first.taxonomies);
taxonomies come with null value

and any many-to-many relation
also the data saved correct in data base and the GraphQL Request on API retrieve data

like this

Future customRequest2() async {
const document = '''
query GetPeople {
listPeople {
items {
id
first_name
last_name
bio
city
country
image
job_title
organization
is_featured
is_discoverable
industry {
id
name
}
taxonomies {
items {
id
taxonomy {
id
name
emoji
}
}
}
categories {
items {
id
category {
id
name
}
}
}
goals {
items {
id
goal {
id
name
}
}
}
}
}
}
''';

final request = GraphQLRequest<String>(
  document: document,
  decodePath: 'listPeople',
);

try {

  final response = await Amplify.API.query(request: request).response;

  if (response.errors.isNotEmpty) {
    print('Response Error: ${response.errors}');
  } else {
    print("data: ${response.data}");
    final List<dynamic> items = jsonDecode(response.data!)['listPeople']['items'];
    final List<Person> people = items.map((item) => Person.fromJson(item)).toList();

    print('Response data: ${people.length}');

    setState(() {
      persons = people;
    });
  }
} catch (e, sk) {
  print('An error occurred: $e');
  print(sk);
}

}

I don't know why DataStore does not retrieve taxonomies and categories with every person

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.22.3

Amplify Flutter Version

^2.5.0

Deployment Method

Amplify CLI (Gen 1)

Schema

type Category @model @auth(rules: [{ allow: public }]) {
    id: ID!
    name: String!
    order: Int
    people: [Person] @manyToMany(relationName: "PersonCategories")


}

type Taxonomy @model @auth(rules: [{ allow: public }]) {
    id: ID!
    name: String!
    emoji: String
    people: [Person] @manyToMany(relationName: "PersonTaxonomies")
}


type Person @model @auth(rules: [{ allow: public }]) {
    id: ID!
    first_name: String!
    last_name: String!
    bio: String
    city: String
    country: String
    image: String
    job_title: String
    organization: String
    taxonomies: [Taxonomy] @manyToMany(relationName: "PersonTaxonomies")
    categories: [Category] @manyToMany(relationName: "PersonCategories")
    is_featured: Boolean
    is_discoverable: Boolean
}
@github-actions github-actions bot added pending-triage This issue is in the backlog of issues to triage pending-maintainer-response Pending response from a maintainer of this repository labels Jan 14, 2025
@ekjotmultani
Copy link
Member

Hi @HusseinReda97pro, I'm sorry you are facing this issue. I will attempt to reproduce and get back to you

@github-actions github-actions bot removed the pending-maintainer-response Pending response from a maintainer of this repository label Jan 14, 2025
@ekjotmultani ekjotmultani added datastore Issues related to the DataStore Category to-be-reproduced Issues that have not been reproduced yet, but have reproduction steps provided labels Jan 14, 2025
@tyllark tyllark added bug Something is not working; the issue has reproducible steps and has been reproduced GraphQL API Issues related to the API (GraphQL) Category and removed to-be-reproduced Issues that have not been reproduced yet, but have reproduction steps provided pending-triage This issue is in the backlog of issues to triage labels Jan 22, 2025
@tyllark
Copy link
Member

tyllark commented Jan 22, 2025

Hello @HusseinReda97pro, we were able to reproduce the issue you described and marked it as a bug. We will update here once we have a fix.

As a workaround we recommend creating your own join table to manage this many to many relationship. Gen 2 does not support the manyToMany directive, but it has a guide for how to represent a many to many relationship with the hasMany and belongsTo directives.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working; the issue has reproducible steps and has been reproduced datastore Issues related to the DataStore Category GraphQL API Issues related to the API (GraphQL) Category
Projects
None yet
Development

No branches or pull requests

3 participants