0
0
GraphQLquery~10 mins

Bidirectional relationships in GraphQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a bidirectional relationship field in GraphQL schema.

GraphQL
type Author {
  id: ID!
  name: String!
  books: [Book][1]
}

type Book {
  id: ID!
  title: String!
  author: Author
}
Drag options to blanks, or click blank then click option'
A@relation(name: "BooksByAuthor")
B@relation(name: "AuthorBooks")
C@relation(name: "AuthorToBooks")
D@relation(name: "BooksAuthor")
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the @relation directive causes the relationship to be unidirectional.
Using inconsistent relation names on both sides.
2fill in blank
medium

Complete the code to define the inverse side of the bidirectional relationship.

GraphQL
type Book {
  id: ID!
  title: String!
  author: Author[1]
}
Drag options to blanks, or click blank then click option'
A@relation(name: "AuthorBooks")
B@relation(name: "BooksAuthor")
C@relation(name: "AuthorToBooks")
D@relation(name: "BooksByAuthor")
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different relation name breaks the bidirectional link.
Forgetting to add the @relation directive on one side.
3fill in blank
hard

Fix the error in the bidirectional relationship by completing the missing directive.

GraphQL
type Author {
  id: ID!
  name: String!
  books: [Book][1]
}

type Book {
  id: ID!
  title: String!
  author: Author
}
Drag options to blanks, or click blank then click option'
A@relation(name: "BooksByAuthor")
B@relation(name: "AuthorToBooks")
C@relation(name: "BooksAuthor")
D@relation(name: "AuthorBooks")
Attempts:
3 left
💡 Hint
Common Mistakes
Using different relation names on each side.
Missing the @relation directive on one side.
4fill in blank
hard

Fill both blanks to define a bidirectional relationship with explicit foreign key field.

GraphQL
type Author {
  id: ID!
  name: String!
  books: [Book][1]
}

type Book {
  id: ID!
  title: String!
  authorId: ID!
  author: Author[2]
}
Drag options to blanks, or click blank then click option'
A@relation(name: "BooksByAuthor")
B@relation(name: "AuthorBooks")
C@relation(name: "BooksByAuthor", fields: [authorId], references: [id])
D@relation(name: "AuthorBooks", fields: [authorId], references: [id])
Attempts:
3 left
💡 Hint
Common Mistakes
Using different relation names.
Omitting fields and references on the foreign key side.
5fill in blank
hard

Fill all three blanks to define a bidirectional relationship with custom relation name and explicit foreign key.

GraphQL
type User {
  id: ID!
  username: String!
  posts: [Post][1]
}

type Post {
  id: ID!
  content: String!
  userId: ID!
  user: User[2][3]
}
Drag options to blanks, or click blank then click option'
A@relation(name: "UserPosts")
B@relation(name: "UserPosts", fields: [userId]
C, references: [id])
D@relation(name: "PostsByUser")
Attempts:
3 left
💡 Hint
Common Mistakes
Using different relation names on each side.
Forgetting to add references on the foreign key side.