我正在使用Koa,阿波罗和护照,我有困难访问护照用户从req.user内阿波罗Resolver。我还有一个简单的REST端点。当我从rest端点的路由调用ctx.req.user时,它会返回用户名、电子邮件等。
然而,阿波罗Resolver中相同的req.user语句作为未定义返回。如果我只是单独调用ctx.req,那么我可以将完整的请求记录到包含cookie/会话的控制台。
我怀疑(主要是因为我尝试了其他一切),这可能是因为我在应用Passport中间件之前在我的app.ts文件中创建了阿波罗服务器。然而,我不确定情况是否如此,我也不知道该做什么改变。为了改变这一点,我也很慢地把我主要工作的代码库拆开。
//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);
}//解析器.
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;发布于 2019-07-16 09:29:18
问题是,在我将Passport应用到应用程序之前,我已经启动了阿波罗服务器。我按照下面的步骤对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;https://stackoverflow.com/questions/57047250
复制相似问题