How can I get my request object passed into my context? I am trying to verify the user is authenticated.
I am using the hapi pkg yar to manage the session.
For example in my root path, when I do the following
app.route({
method: 'GET',
path: '/',
options: {
auth: 'simple'
},
handler(request) {
return 'Logged in' + JSON.stringify(request.yar.get('auth'))
}
})
I see Logged in{"username":"admin","isValid":true,"id":"1","name":"John"}
But the following returns undefined:
const server = new ApolloServer({
schema: query,
tracing: true,
context: async ({ request }) => {
return {
request
}
}
})
const resolvers = {
Query: {
rules: async (_, args, context) => {
console.log(context.request.yar.get('auth'))
return mock.rules
}
}
}
Here's a Github gist of my whole index.ts file https://gist.github.com/apollomusa/1afff80b29600c863705dc2cc174e86a
Anyone experiencing? For some reason the request object that Apollo Server receives does not hold the same values the request object Hapi receives
I had a similar issue with express
have u solved ur problem?
@apollomusa I set user to req.session in express, but couldn't get user in apolloServer context definition.
const server = new ApolloServer({
schema,
context: ({req}) => {
const { user } = req.session;
return { user };
},
});
@ILovePing similar issue with me, the request object in apolloServer context differs so for example
request.auth.isAuthenticated will be true for all other paths I setup in Hapi. But false when viewed within the apolloServer context:
const server = new ApolloServer({
schema: makeExecutableSchema(schema),
playground: config.playground,
context: async ({ request }) => {
return {
isAuthenticated: request.auth.isAuthenticated
}
}
})
Still trying to find a solution
@apollomusa Currently,I just use a variable to store user for the first /graphql Get request
let user = null;
//...some other code here
public configureGraphQL(app: any, httpServer: any) {
const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
const schema = this.graphQLFactory.createSchema({ typeDefs });
app.use('/graphql', (req, res, next) => {
if (req.session.user){
user = req.session.user;
}
next();
});
const server = new ApolloServer({
schema,
context: () => {
return { user };
},
});
server.applyMiddleware({ app,
cors: true,
bodyParserConfig: true,
});
server.installSubscriptionHandlers(httpServer);
}
This may has some other problems but helps now.
And I still couldn't figure out why req.session is different in the second /graphql Post request
You can specify route options when you call await apolloServer.applyMiddleware(), for example:
await apolloServer.applyMiddleware({
app: server, // a hapi server instance
path: '/my/path/to/graphql',
route: {
auth: 'simple',
},
});
@apollomusa - Hope that helps a bit.
@maxnachlinger Your commend it's not clear. What does route does? I tried finding that option in the API reference documentation but there's no mention of applyMiddleware accepting a route option anywhere.
@obedparla route provides options for the graphql route in Hapi, have a look here - https://github.com/apollographql/apollo-server/blob/ad4240295469821c24aaf2f1032b12882dd3b6f9/packages/apollo-server-hapi/src/ApolloServer.ts#L59 - route is if type hapi.RouteOptions.
@musab if you found an answer would you mind closing this issue?
Most helpful comment
You can specify route options when you call
await apolloServer.applyMiddleware(), for example:@apollomusa - Hope that helps a bit.