Sergiy
Sergiy

Reputation: 651

Fetching data by GraphQL

I'm using GraphQL with graphql-sequelize and mysql db. I need to fetch all products, whose users have status = true. As I understand, this query is wrong:

{
  products {
    name
    users(where: { status: true }) {
      id
    }
  }
}

And this query is correct:

{
  products(where: { users: { status: true } }) {
    name
    users {
      id
    }
  }
}

Is it true? But graphql-sequelize does not understand nested object agrument:

where: { users: { status: true } }

Is there any workaround? How should I use sequelize here in correct way?

Upvotes: 1

Views: 137

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84687

In order to limit the query to products where the users meet a certain condition, we need an inner join (i.e. an include with require set to true). graphql-sequelize doesn't utilize include at all under the hood, so you won't get this functionality out of the box. However, you should be able to implement it yourself. Something like:

resolver(ProductModel, {
  // other options
  before: (findOptions, args, context) => {
    if (args.users) {
      findOptions.include = [{
        model: UserModel,
        required: true,
        where: args.users,
      }]
    }
    return findOptions;
  },
}

Upvotes: 2

Related Questions