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

Posted

技术标签:

【中文标题】GraphQL API 获取当前用户配置文件 + 获取特定 id 用户配置文件【英文标题】:GraphQL API to get current user profile + get specific id user profile 【发布时间】:2019-04-07 13:35:41 【问题描述】:

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

您会将其设计为 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】:

我会让它们都返回相同类型的对象,但它们回答不同的问题。

GraphQL 的优势在于它的表现力。

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

【讨论】:

以上是关于GraphQL API 获取当前用户配置文件 + 获取特定 id 用户配置文件的主要内容,如果未能解决你的问题,请参考以下文章