GraphQL Explorer 无法与 Express Session 一起使用
GraphQL Explorer not working with Express Sessions
我正在尝试使用 GraphQL 和 Express 创建登录系统,但未保存会话。每当我登录时,req.session.userId
保持未定义状态。
我的代码:
(async () => {
await connect(process.env.MONGO_URI!, { dbName: "example" });
const app = express();
app.use(
cors({
origin: [__clientUrl__, "https://studio.apollographql.com"],
credentials: true
})
);
app.use(
session({
name: "qid",
secret: process.env.SESSION_SECRET!,
store: MongoStore.create({
mongoUrl: process.env.MONGO_URI,
dbName: "example"
}),
saveUninitialized: false,
resave: false,
cookie: {
maxAge: 6.048e8,
httpOnly: __prod__,
sameSite: "lax",
secure: __prod__
}
})
);
const server = new ApolloServer({
schema: await buildSchema({
resolvers: [HelloResolver, UserResolver],
validate: false
}),
context: ({ req, res }) => ({ req, res })
});
await server.start();
server.applyMiddleware({
app,
cors: {
origin: [__clientUrl__, "https://studio.apollographql.com"],
credentials: true
}
});
app.listen(__port__, () =>
console.log(
` Server started at http://localhost:${__port__}${server.graphqlPath}`
)
);
})();
TypeGraphQL 登录突变:
@Mutation(() => User, { nullable: true })
public async login(
@Arg("username") username: string,
@Arg("password") password: string,
@Ctx() { req }: Context
) {
const user = await UserModel.findOne(
username.includes("@") ? { email: username } : { username }
);
if (!user) return;
if (!(await verify(user.password, password))) return;
req.session.userId = user._id;
return user;
}
我还在 GraphQL Explorer 中启用了 cookie 并设置了这些标题:
你找到解决办法了吗?
您可以在创建服务器时通过添加插件 ApolloServerPluginLandingPageGraphQLPlayground 来尝试使用“旧”游戏机
plugins: [ ApolloServerPluginLandingPageGraphQLPlayground(), ],
如果这适用于“旧”游乐场,那么问题出在 graphqlstudio 中。
AFAIC,无论我传递给服务器的 cors 值是什么,我都面临着 graphql studio 的 cors 问题。所以我放弃了graphql studio,回到了playground
我做了一些研究,意识到我需要在创建 ApolloServer 实例时写这个:
const server = new ApolloServer({
schema: await buildSchema({
resolvers: [HelloResolver, UserResolver],
validate: false
}),
context: ({ req, res }) => {
> res.header(
> "Access-Control-Allow-Origin",
> "https://studio.apollographql.com"
> );
> res.header("Access-Control-Allow-Credentials", "true");
return { req, res };
}
});
这允许在 GQL Explorer 中设置 cookie。让我知道这是否适合您!
我正在尝试使用 GraphQL 和 Express 创建登录系统,但未保存会话。每当我登录时,req.session.userId
保持未定义状态。
我的代码:
(async () => {
await connect(process.env.MONGO_URI!, { dbName: "example" });
const app = express();
app.use(
cors({
origin: [__clientUrl__, "https://studio.apollographql.com"],
credentials: true
})
);
app.use(
session({
name: "qid",
secret: process.env.SESSION_SECRET!,
store: MongoStore.create({
mongoUrl: process.env.MONGO_URI,
dbName: "example"
}),
saveUninitialized: false,
resave: false,
cookie: {
maxAge: 6.048e8,
httpOnly: __prod__,
sameSite: "lax",
secure: __prod__
}
})
);
const server = new ApolloServer({
schema: await buildSchema({
resolvers: [HelloResolver, UserResolver],
validate: false
}),
context: ({ req, res }) => ({ req, res })
});
await server.start();
server.applyMiddleware({
app,
cors: {
origin: [__clientUrl__, "https://studio.apollographql.com"],
credentials: true
}
});
app.listen(__port__, () =>
console.log(
` Server started at http://localhost:${__port__}${server.graphqlPath}`
)
);
})();
TypeGraphQL 登录突变:
@Mutation(() => User, { nullable: true })
public async login(
@Arg("username") username: string,
@Arg("password") password: string,
@Ctx() { req }: Context
) {
const user = await UserModel.findOne(
username.includes("@") ? { email: username } : { username }
);
if (!user) return;
if (!(await verify(user.password, password))) return;
req.session.userId = user._id;
return user;
}
我还在 GraphQL Explorer 中启用了 cookie 并设置了这些标题:
你找到解决办法了吗? 您可以在创建服务器时通过添加插件 ApolloServerPluginLandingPageGraphQLPlayground 来尝试使用“旧”游戏机
plugins: [ ApolloServerPluginLandingPageGraphQLPlayground(), ],
如果这适用于“旧”游乐场,那么问题出在 graphqlstudio 中。
AFAIC,无论我传递给服务器的 cors 值是什么,我都面临着 graphql studio 的 cors 问题。所以我放弃了graphql studio,回到了playground
我做了一些研究,意识到我需要在创建 ApolloServer 实例时写这个:
const server = new ApolloServer({
schema: await buildSchema({
resolvers: [HelloResolver, UserResolver],
validate: false
}),
context: ({ req, res }) => {
> res.header(
> "Access-Control-Allow-Origin",
> "https://studio.apollographql.com"
> );
> res.header("Access-Control-Allow-Credentials", "true");
return { req, res };
}
});
这允许在 GQL Explorer 中设置 cookie。让我知道这是否适合您!