我当时正在考虑编写一个执行以下操作的API:注册和登录用户,为用户提供身份验证令牌创建地图(数据示例:{ name: “Quotes”, attributes: [“quote”, “author"] })创建地图项(数据示例:{ quote: "...", author: "..." })我将建立这样的查询:// return the name and id of all the user's mapsmaps(authToken="…") {  name,  id}// return all the items of a single mapmaps(authToken="…") {  map(name=“Quotes") {    items  }}// OR by using the map_idmaps(authToken="…") {  map(id=“…") {    items  }}所以,我的问题是,这是正确的还是我需要以不同的方式构造它? 最佳答案 我建议在GraphQL本身之外构造身份验证,并让您的架构逻辑处理授权。例如,如果使用的是express-graphql NPM模块,则可以检查cookie或HTTP Basic Auth或要用于获取身份验证令牌的任何机制,然后通过,在查询解析期间的每个级别均可使用:app.use('/graphql', (request, response, next) => { const viewer = getViewerFromRequest(); // You provide this. const options = { rootValue: { viewer, }, schema, }; return graphqlHTTP(request => options)(request, response, next);});然后,在模式内部,您可以访问rootValue并将其用于访问控制和授权:resolve: (parent, args, {rootValue}) => { const viewer = {rootValue}; // Code that uses viewer here...}请注意,从graphql v0.5.0开始,在参数列表中的位置3处插入了the rootValue signature has changed和第三个“ context”参数。此参数适用于传递身份验证令牌或类似内容:resolve: (parent, args, authToken, {rootValue}) => { // Code that uses the auth token here...}
08-25 05:05