GraphQL query and check the returned data

2019-08-02 21:35发布

I would like to have a query containing a check, for example when querying users, only users that have a non-null email are wanted. How could I add such check? Thanks!

users {
  id,
  username,
  email
}

1条回答
等我变得足够好
2楼-- · 2019-08-02 21:53

GraphQL fields can have arguments. So you could pass an argument to your users field, named onlyNonNullEmail: boolean:

type Query {
  users(onlyNonNullEmail: Boolean) {
    ...
  }
}

Then, inside your resolve function:

users: (_, args, context, ast) => {
  const { onlyNonNullEmail } = args;

  // If onlyNonNullEmail is true, return only users with a non-null email
  // Else proceed as usual
}
查看更多
登录 后发表回答