Querying Many-To-Many Relationships in AWS Amplify

922 Views Asked by At

I have two models in my graphql schema and the one I am trying to query on, Sessions, has two @belongsTo directives (read on a forum this matters). I can successfully save these models and view them on the AWS AppSync Queries Tab where I can query getSessions successfully BUT when I try to the exact same query locally following these docs:

(https://docs.amplify.aws/lib/graphqlapi/advanced-workflows/q/platform/flutter/#combining-multiple-operations)

I get an error locally:

type "Null" is not a subtype of type 'string'

What am I doing wrong and how do I fix this so I can successfully retrieve my nested query:

Here are my models as a reference:

Sessions:

type Session
  @model
  @auth(
    rules: [
      { allow: public }
      { allow: owner }
      { allow: groups, groups: ["Admin"] }
    ]
  ) {
  id: ID!
  name: String
  numPeoplePresent: Int
  notes: String
  eIdReader: String
  weighTiming: String
  cows: [Cow] @manyToMany(relationName: "CowSession")
  proceduresID: ID
  procedures: Procedures @hasOne(fields: ["proceduresID"])
}

Cow:

type Cow
  @model
  @auth(
    rules: [
      { allow: public }
      { allow: owner }
      { allow: groups, groups: ["Admin"] }
    ]
  ) {
  id: ID!
  name: String!
  RfId: String
  weight: [Float!]!
  temperament: [Int]
  breed: String
  type: String
  dateOfBirth: AWSDate
  sessions: [Session] @manyToMany(relationName: "CowSession")
  procedures: [Procedures] @manyToMany(relationName: "CowProcedures")
}

This is the query that is causing the error:

const getSession = 'getSession';
      String graphQLDocument = '''query getSession(\$id: ID!) {
        $getSession(id: \$id) {
          numPeoplePresent
          notes
          name
          eIdReader
          id
          owner
          proceduresID
          updatedAt
          weighTiming
          cows {
            items {
              cow {
                RfId
              }
            }
          }
        }
      }''';
      final getSessionRequest = GraphQLRequest<Session>(
        document: graphQLDocument,
        modelType: Session.classType,
        variables: <String, String>{'id': sessID}, //parameter of the current session can hardcode to whatever you need here 
        decodePath: getSession,
      );
      final response =
          await Amplify.API.query(request: getSessionRequest).response;
      print('Response: ${response.data}');
1

There are 1 best solutions below

0
On BEST ANSWER

The wonderful people at amplify answered this quickly so I will relay the information here:

the problem was the intermediary ids were not included in my local query so it was unable to retrieve the nested Cows. Updated query looks like this:

getSession = 'getSession';
  String graphQLDocument = '''query getSession(\$id: ID!) {
    $getSession(id: \$id) {
      numPeoplePresent
      notes
      name
      eIdReader
      id
      owner
      proceduresID
      updatedAt
      weighTiming
      cows {
        items {
          id <-- needed this one
          cow {
            id <-- and this id too
            RfId
            breed
            dateOfBirth
            name
            type
            weight
          }
        }
      }
    }
  }''';