获取当前用户配置文件的GraphQLAPI+;获取特定id用户配置文件
在应用程序中,您可以查看自己的个人资料,也可以查看其他人的个人资料: 是否将此设计为带有可选id arg的1查询获取当前用户配置文件的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,
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的优势在于它的表现力 当然,在幕后,您可以重用解析器逻辑