首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拥有护照、graphql瑜伽和棱镜的GoogleOAuth

拥有护照、graphql瑜伽和棱镜的GoogleOAuth
EN

Stack Overflow用户
提问于 2018-10-09 16:44:25
回答 1查看 560关注 0票数 7

我们可以在一个项目中同时使用Restful API和Graphql API吗?除了谷歌认证,我们需要一些途径来处理它。对于CRUD操作,我们使用Graphql。

如下所示:

代码语言:javascript
复制
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的回调函数中,如何调用变异并将谷歌的所有配置文件信息保存到数据库中?

代码语言:javascript
复制
(accessToken, refreshToken, profile, done) => {
    console.log(profile);

    // After receiving profile info from google, call mutation and save
    // profile into the database.
  }

以及在deserializeUserserializeUser函数中。以前,当我使用Nodejs和MongoDB时,我做过这样的事情:

代码语言:javascript
复制
passport.serializeUser((user, done) => {
   done(null, user.id);
});

passport.deserializeUser((id, done) => {

  // mongoose.
  User.findById(id).then(user => {
    done(null, user);
  });

});

使用Prisma和Graphql,如何解决突变问题?

EN

回答 1

Stack Overflow用户

发布于 2018-12-10 18:19:32

在服务器中,创建prisma-binding实例,然后将其传递给graphql-yoga的上下文。这允许您在解析器中进行prisma操作,如下所示:

代码语言:javascript
复制
context.db.query.user({where: {id: 'ABCD'}})

但这并不意味着您不能在其他地方使用prisma-binding实例!

在你的passport回调中,你可以访问你的prisma-binding实例,还可以调用查询和突变:

代码语言:javascript
复制
db.mutation.createUser({data: {name: 'John Doe'}})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52716826

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档