Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取当前用户配置文件的GraphQLAPI+;获取特定id用户配置文件_Graphql_Apollo_Apollo Server - Fatal编程技术网

获取当前用户配置文件的GraphQLAPI+;获取特定id用户配置文件

获取当前用户配置文件的GraphQLAPI+;获取特定id用户配置文件,graphql,apollo,apollo-server,Graphql,Apollo,Apollo Server,在应用程序中,您可以查看自己的个人资料,也可以查看其他人的个人资料: 是否将此设计为带有可选id arg的1查询 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,

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

是否将此设计为带有可选id arg的1查询

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
}
或者两个单独的查询,一个用于当前登录的用户,另一个用于其他用户

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

我将两者都设置为虽然它们都返回相同类型的对象,但它们回答不同的问题

GraphQL的优势在于它的表现力

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