GraphQL API获取当前用户配置文件+获取特定ID用户配置文件

时间:2018-11-03 16:27:50

标签: graphql apollo apollo-server

在应用中,您可以查看自己的个人资料并查看其他人的个人资料:

您是否将此设计为1个带有可选ID arg的查询?

profile: async (root, { id }, context) => {
  if (!context.user) throw unauthorized error

  if (id)
    profile = get profile for the passed in id
    return profile

  // if no id passed, get the current user instead
  profile = get profile for context.user.id
  return profile
}

或2个单独的查询,1个用于当前登录的用户,1个用于其他用户?

ownProfile: async (root, {}, context) => { get from context.user.id }
profile: async (root, { id }, context) => { get from id arg }  // otherProfile

1 个答案:

答案 0 :(得分:1)

我会把两者都当作返回相同类型的对象,而是回答不同的问题。

GraphQL的优势在于表现力。

当然,您可以在后台使用解析器逻辑。

相关问题