【问题标题】:GraphQL API to get current user profile + get specific id user profileGraphQL API 获取当前用户配置文件 + 获取特定 id 用户配置文件
【发布时间】: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

【问题讨论】:

    标签: graphql apollo apollo-server


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 2011-08-18
      相关资源
      最近更新 更多