gpt4 book ai didi

javascript - Graphql shield 返回 Not Authorized for allowed mutation

转载 作者:行者123 更新时间:2023-12-05 05:56:49 24 4
gpt4 key购买 nike

我正在尝试使用 apollo-server-express 实现 GraphQL API。我想通过 graphql-shield 中间件管理权限,但我在允许执行突变方面遇到了问题。我的目标是拥有基于 JWT 的身份验证,但允许对注册/登录突变所需的未经身份验证的用户执行一些查询/突变。我正在使用默认的 allow 规则。但是,当我尝试运行登录变更时,我收到了未授权! 错误。我不知道为什么会这样。该规则适用于查询。

谢谢你的回答。

服务器

import express from "express";
import cors from "cors";
import { ApolloServer, makeExecutableSchema } from "apollo-server-express";
import config from "./config";
import mockResolver from "./resolvers/mockResolver";
import typeDefs from "./graphql/typeDefs";
import { applyMiddleware } from "graphql-middleware";
import permissions from "./graphql/permissions";

const app = express();
app.use(cors());

const server = new ApolloServer({
schema: applyMiddleware(
makeExecutableSchema({ typeDefs, resolvers: mockResolver }),
permissions
),
playground: true,
introspection: true,
});

server.applyMiddleware({ app, path: "/graphql" });
app.listen(config.PORT, () =>
console.log("Server listening at http://localhost:%s", config.PORT)
);

类型定义

import { gql } from "apollo-server";

const typeDefs = gql`
type User {
id: Int!
email: String!
password: String!
}

type LoginResponse {
id: String
email: String
token: String
}

type Query {
user(id: Int!): User
users: [User]
}

type Mutation {
login(email: String!, password: String!): LoginResponse
}
`;

权限

import { shield, allow } from "graphql-shield";

const permissions = shield({
Query: {
users: allow,
},
Mutation: {
login: allow,
},
});

export default permissions;

最佳答案

const permissions = shield({
Query: {
users: allow,
},
Mutation: {
login: allow,
},
});

const permissions = shield({
Query: {
users: allow,
},
Mutation: {
login: allow,
},
},
{
debug: true
});

并跟踪到错误消息。

关于javascript - Graphql shield 返回 Not Authorized for allowed mutation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69004669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com