graphback icon indicating copy to clipboard operation
graphback copied to clipboard

Extend `@oneToOne` to allow querying from both sides

Open craicoverflow opened this issue 4 years ago • 1 comments

Problem

This creates a user.details_id column in the database, and enables relation data fetching when querying the User model.

@model
type User {
  id: ID!
  """
  @oneToOne key: 'details_id'
  """
  details: UserDetails
}

Currently it is not possible to fetch the User relation when querying UserDetails. This is because the oneToOne annotation is only applied to one side. It should be possible to query from both sides making it a bidirectional relationship (at the GraphQL level).

Proposal

Have a @oneToOne field annotation which maps between the two types. This field should be optional.

@model
type User {
  id: ID!
  """
  @oneToOne key: 'details_id', field: 'user'
  """
  details: UserDetails
}

The oneToOne annotation in the opposite type should NOT have a key attribute as this associates the field with a column in the table for this type.

@model
type UserDetails {
  id: ID!
  """
  @oneToOne field: 'details'
  """
  user: User
}

Challenges

  • Validation might be tricky
  • Following our existing patterns, fields should be generated.

craicoverflow avatar Mar 18 '20 09:03 craicoverflow

I think the approach taken in https://github.com/aerogear/graphback/pull/964 will actually work. The main issue with it was specifying a virtual: true annotation on the non-owning side of the 1:1 is a confusing concept not really seen as a common pattern anywhere else.

It would make more sense to put an owner: true annotation on the owner side of the relationship instead.

On Wed, 18 Mar 2020 at 09:16, Enda [email protected] wrote:

Problem

This creates a user.details_id column in the database, and enables relation data fetching when querying the User model.

@modeltype User { id: ID! """ @oneToOne key: 'details_id' """ details: UserDetails }

Currently it is not possible to fetch the User relation when querying UserDetails. This is because the oneToOne annotation is only applied to one side. It should be possible to query from both sides making it a bidirectional relationship (at the GraphQL level). Proposal

Have a @oneToOne field annotation which maps between the two types. This field should be optional.

@modeltype User { id: ID! """ @oneToOne key: 'details_id', field: 'user' """ details: UserDetails }

The oneToOne annotation in the opposite type should NOT have a key attribute as this associates the field with a column in the table for this type.

@modeltype UserDetails { id: ID! """ @oneToOne field: 'details' """ user: User }

Challenges

  • Validation might be tricky
  • Following our existing patterns, fields should be generated.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/aerogear/graphback/issues/930, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACZTDZNON3NF4MDXNZ6O2YTRICGNHANCNFSM4LOMHA5Q .

--

Enda Phelan

Software Engineer

Red Hat https://www.redhat.com

Communications House

Cork Road, Waterford, Ireland, X91 NY33

[email protected] https://www.redhat.com

craicoverflow avatar Jun 29 '20 11:06 craicoverflow