Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many to Many relation through joint table returns null value #1569

Closed
vOtto opened this issue Aug 24, 2018 · 6 comments · May be fixed by k4ny/apollo-server#3 or penx/apollo-server#6
Closed

Many to Many relation through joint table returns null value #1569

vOtto opened this issue Aug 24, 2018 · 6 comments · May be fixed by k4ny/apollo-server#3 or penx/apollo-server#6

Comments

@vOtto
Copy link

vOtto commented Aug 24, 2018

I have my model many to many relation in my model:

Posts have many categories and categories might have many posts type Post

{
  id: Int,
  userId: Int,
  title: String,
  category: [Category]
}

and

type Category {
  id: Int,
  postId: Int,
  name: String
  description: String,
  upTick: Int,
}

through a PostCategories table.

create mutation works just fine.

 categoryId | postId 
---------------------------
 1                  | 2

but when I try to Query categories on posts im getting null as result, query:

{
post(id:2){
  category {
    name
  }
}
}

and result:

{
  "data": {
    "post": {
      "category": null
    }
  }
} 

updated:
resolver for category
Query: { category(root, id) { return db.category.findOne({ id }).then(collection => collection.dataValues); }, },
and for post
Query: { post(root, { id }) { return db.post.findOne({ id }).then(collection => collection.dataValues); },
any ideas?

@JacobT14
Copy link

Can you post your resolver code? Just for future reference, this is a question better suited for Stack Overflow!

@vOtto
Copy link
Author

vOtto commented Aug 24, 2018

Updated.

@JacobT14
Copy link

On your post type, what does the resolver look like for the category field?

@vOtto
Copy link
Author

vOtto commented Aug 24, 2018

I think thats the issue, cause i have none.

@vOtto vOtto closed this as completed Aug 24, 2018
@vOtto
Copy link
Author

vOtto commented Aug 24, 2018

thanks alot

@JacobT14
Copy link

No problem! You should be able to just return root.categories, whatever the db field is called

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants