cult-of-coders / grapher

Grapher: Meteor Collection Joins + Reactive GraphQL like queries
https://atmospherejs.com/cultofcoders/grapher
MIT License
275 stars 53 forks source link

Can't have a one-to-one and one-to-many to the same type #436

Closed smeijer closed 4 years ago

smeijer commented 4 years ago

I'm trying to get a 1:1 and a 1:∞ relation to a single type.

Let's say I have a Blog, and a single Blog, has many Comments. Blog has 2 resolvers.

I understand that to get the 1:1 working, I need to add the unique on Comment.blog. The issue is, that this affects both the Blog.comment as well as Blog.comments resolvers.

Thereby, I think that unique is currently being defined on the "wrong side" of the relationship, and we should be defining a type: 'one' on the Blog.comment definition, instead of on Comment.blog.

type Blog @mongo(name: "blogs") {
  # should return object
  comment(commentId: String!): Comment @link(to: "blog")
  # should return array
  comments: [Comment] @link(to: "blog")
}

type Comment @mongo(name: "comments") {
  # unique, makes both comment queries return objects
  blog: Blog @link(field: "blogId", unique: true)
}
theodorDiaconu commented 4 years ago

Unique is only used for 1:1. Why would you add blog() unique ? If you don't specify many: true blog will return a single element. Unique != Single.

smeijer commented 4 years ago

Why would you add blog() unique?

Because a comment only has a single blog that it belongs to? The link directive doesn't seem to have the many argument that you're referring to. What am I missing?

https://github.com/cult-of-coders/grapher-schema-directives/blob/4136a7302b9d18548bd63feed550d990343fb906/directiveDefinitions.js#L6-L12

unique makes the result flip between many and one. (array / object). I'm still unable to get both the Blog.comments as well as the Blog.comment resolvers to work. I need comments to return all the comments (array) and comment to return a single object.

How should I approach that?

smeijer commented 3 years ago

@theodorDiaconu , I'm still not able to get this working. Is it because I'm using the directives?