mickhansen / graphql-sequelize

GraphQL & Relay for MySQL & Postgres via Sequelize
MIT License
1.9k stars 172 forks source link

Nested arguments #677

Closed bssergy closed 5 years ago

bssergy commented 5 years ago

I.e. my schema looks like:

products
  user

I want to query products whose user has property active: true. But query like:

{
  products(where: { user: { active: true } }) {
    user {
      name
    }
  }
}

throws an error: "Unknown value { active: true }" Does sequelize have support for nested arguments? Are there any workarounds?

mickhansen commented 5 years ago

It's not something graphql-sequelize can handle. You can write your own logic to pass down arguments via the resolver.

bssergy commented 5 years ago

@mickhansen ok, I use defaultArgs and resolver helpers. Where clause can be really difficult with operators: where: { or: [ { user: { active: true } }, productName: "Others" ] } It's really hard to parse this query manually. Also, there should be included user assosiation. Is it possible to do this automatically?

mickhansen commented 5 years ago

I wouldn't suggest using where in the graphql schema at all, use descriptive arguments to express your logic instead.

User associations can be handled via the resolver and dataloader, no need to prefetch.

bssergy commented 5 years ago

Ok. Just to clarify. If I handle user assosiation in resolver with dataloader, then I will have query like:

{
  products: {
    user(active: true) {
      name
    }
  }
}

But this query returns me ALL products, and then attach users with property active=true. But I need query ONLY these products, whose user has property active=true. Is it possible to do with graphql-sequelize?

mickhansen commented 5 years ago

products(fromActiveUsers: true) {} , for the products resolver you add a before hook that looks for that argument and applies logic.

janmeier commented 5 years ago

Everything is possible, but you will probably have to write the logic yourself :)

The query is doing what it's supposed to. A filter on the child field (user) should never affect the parent field (products). If you want to filter products, you need to add the filters directly to product like above ^^