目前,我正在通过 Google 构建登录功能,但我遇到了一些让我有点困惑的问题。
我们可以在一个项目中同时使用 Restful API 和 Graphql API 吗?除了谷歌身份验证,我们需要一些路由来处理它。对于 CRUD 操作,我们使用 Graphql。
像这样的东西:
const { GraphQLServer } = require('graphql-yoga');
const { Prisma } = require('prisma-binding');
const resolvers = require('./resolvers');
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20').Strategy;
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
// mongoose.
User.findById(id).then(user => {
done(null, user);
});
});
passport.use(new GoogleStrategy(
{
clientID: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
callbackURL: '/auth/google/callback',
},
(accessToken, refreshToken, profile, done) => {
console.log(profile);
// After receive profile info from google, call mutation and save
// profile into database.
}
));
const db = new Prisma({
typeDefs: 'src/generated/prisma.graphql',
endpoint: process.env.PRISMA_ENDPOINT,
debug: true,
secret: process.env.PRISMA_SECRET,
});
const server = new GraphQLServer({
typeDefs: './src/schema.graphql',
resolvers,
resolverValidationOptions: {
requireResolversForResolveType: false
},
context: req => ({ ...req, db })
});
server.express.get('/auth/connect', passport.authenticate('google', {
scope: ['profile', 'email']
}));
server.express.get('/auth/google/callback', passport.authenticate('google'));
server.start(() => console.log(`Server is running on ${process.env.PRISMA_ENDPOINT}`));
在回调函数中GoogleStrategy
,如何调用 Mutation 并将所有配置文件信息从 Google 保存到数据库?
(accessToken, refreshToken, profile, done) => {
console.log(profile);
// After receiving profile info from google, call mutation and save
// profile into the database.
}
并在deserializeUser
和serializeUser
功能。之前,当我使用 Nodejs 和 MongoDB 时,我已经这样做了:
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
// mongoose.
User.findById(id).then(user => {
done(null, user);
});
});
使用 Prisma 和 Graphql,如何用变异解决这个问题?