GraphQL query and check the returned data

回眸只為那壹抹淺笑 提交于 2019-12-24 07:01:51

问题


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:


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
}


来源:https://stackoverflow.com/questions/44590315/graphql-query-and-check-the-returned-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!