Search code examples
amazon-dynamodbgraphqlaws-amplifyaws-appsync

AppSync @connection not making two-way connection


I'm using graphql through AWS AppSync. Given my models below I would expect when I successfully createClassroom with createClassroomInput that the teacherClassrooms would have a new Classroom associated to it and that the newly created Classroom would have a teacher associated to it.

The outcome, however, is that Classroom is created and the User is correctly associated with the new Classroom but the Classroom is not associated to the existing User.

type User @model {
  id: ID!
  userType: String!
  teacherClassrooms: [Classroom] @connection(name: "TeacherClassrooms")
}

type Classroom @model {
  id: ID!
  teacher: User @connection(name: "TeacherClassrooms")
  linkCode: String!
  name: String!
}

export type CreateClassroomInput = {
  id?: string | null,
  linkCode: string,
  name: string,
  classroomTeacherId?: string | null,
};

So, if I query for listClassrooms, each classroom comes back with its associated User. But if I query for a User they do not have any classrooms in their teacherClassrooms array.

Do I need to updateUser when I create a new Classroom? My intuition, and my understanding of the docs, lead me to believe that AppSync would handle both updates when @connection is specified on a model property.

Or is this just a way of indicating to the backend that "for each Id in this property array assume it's of type X and when queried go fetch by Id against the corresponding table"?


Solution

  • Check your list query. I had the same issue and then realised that generated list query was missing relation attributes after I updated schema with name connection. In your case something like this

    listUsers { 
      items { 
        id 
        teacherClassrooms { 
          items { 
            linkCode 
            id name 
          } 
        } 
      } 
    }
    

    inside your listUsers query