无法从Apollo Resolver内的Req获取护照用户

时间:2019-07-15 21:11:18

标签: graphql passport.js apollo koa

我正在使用Koa,Apollo和Passport,但是在Apollo Resolver中无法通过req.user访问Passport用户。我也有一个简单的REST端点。当我从沿着剩余端点的溃逃中呼叫ctx.req.user时,它会给我返回用户名,电子邮件等。

但是,Apollo Resolver中相同的req.user语句以未定义形式返回。如果我自己单独调用ctx.req,则可以将完整的请求记录到包含cookie /会话的控制台。

我怀疑(主要是因为我已经尝试了其他所有方法)可能会发生这种情况,因为我在应用Passport中间件之前在app.ts文件中创建了Apollo服务器。但是,我不确定是否是这种情况,而且我也不知道该做些什么更改。我也很难将我大部分工作的代码库拆开来更改它。

// app.ts

import  Koa = require('koa');
import { getGraphqlApp} from './graphql/get-apollo-server'
import { config } from './config/config';
import { BaseContext } from 'koa';
import * as passport from 'koa-passport';

//Create the Apollo Server and the Koa App
const server = getGraphqlApp();
const app = new Koa();

console.log()
//Apply the App onto the Apollo Server
server.applyMiddleware({ app, path: config.graphqlUri });

//Export the App so that we can import it in server.ts
module.exports = app;

// get-apollo-server.ts

import { makeAugmentedSchema } from 'neo4j-graphql-js';
import { ApolloServer } from 'apollo-server-koa';
import {typeDefs} from './get-graphql-schema'//../typeDefs';
import { getNeo4jDriver} from '../database/connection-neo4j'
import resolvers from './resolvers'

const driver = getNeo4jDriver();

export function getGraphqlApp(): ApolloServer {
  const schema = makeAugmentedSchema({
    typeDefs,
    resolvers,
    config: {
      query: false,
      mutation: false
    }
    //resolverValidationOptions: { requireResolversForResolveType: false }
  });

  const graphqlOptions = {
    schema,
    context: (
      { ctx }
    ) => {
      return {
        driver,
        ctx
      };
    },
    playground: true,
    formatError: error => {
      return error
    },
    introspection: true
  };
  return new ApolloServer(graphqlOptions);
}

// resolver.ts

import { neo4jgraphql } from "neo4j-graphql-js";

const resolvers = {
  Query: {
    Dataset(object, params, ctx, resolveInfo) {
      return neo4jgraphql(object, params, ctx, resolveInfo);
    },
    DatasetAttributes(object, params, ctx, resolveInfo) {
      return neo4jgraphql(object, params, ctx, resolveInfo);
    },


    Idiom(object, params, ctx, resolveInfo) {

      console.log(ctx.ctx.req.user)

      if (!1) {
        throw new Error("request not authenticated");
      } else {
        return neo4jgraphql(object, params, ctx, resolveInfo);
      }
    },
   }
};

export default resolvers;

1 个答案:

答案 0 :(得分:0)

问题是我在将Passport应用于应用程序之前启动了Apollo服务器。我按如下方式对app.ts文件进行了重新整理,并从server.ts中删除了护照部分,以使其正常工作:

import  Koa = require('koa');
import { getGraphqlApp} from './graphql/get-apollo-server'
import { config } from './config/config';
import { BaseContext } from 'koa';
import * as session from 'koa-session';
import * as passport from 'koa-passport';

//Create the Apollo Server and the Koa App
const server = getGraphqlApp();
const app = new Koa();

//Setup the session
app.keys = ['infornite-secret-key'];
app.use(session(app));

//Setup Authentication
require('./config/auth');
app.use(passport.initialize());
app.use(passport.session());

//Apply the App onto the Apollo Server
server.applyMiddleware({ app, path: config.graphqlUri });

//Export the App so that we can import it in server.ts
module.exports = app;