Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve auth header for playground #49

Merged
merged 7 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
"dependencies": {
"@apollo/server": "^4.5.0",
"@prisma/client": "^4.11.0",
"@types/cookie-parser": "^1.4.3",
"bcrypt": "^5.1.0",
"body-parser": "^1.20.2",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
Expand Down
1 change: 1 addition & 0 deletions api/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const createApp = (): Express => {
endpoint: '/api/graphql',
settings: {
'editor.theme': 'light',
'request.credentials': 'include',
},
}),
);
Expand Down
3 changes: 2 additions & 1 deletion api/src/graphql/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ export function createContext(params: CreateContextParams): Context {
logger.trace('Creating context with params: %o', params);
const { req } = params;
const authorizationHeader = req.get('Authorization');
const cookieAuthHeader = req.cookies['gql:default'];
const token = authorizationHeader?.replace('Bearer ', '');

return {
request: params,
prisma,
apolloLogger,
getSession: async () => prisma.session.getSessionByToken(token),
getSession: async () => prisma.session.getSessionByToken(token || cookieAuthHeader),
};
}
13 changes: 10 additions & 3 deletions api/src/graphql/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ApolloServerPlugin, ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
import bodyParser from 'body-parser';
import cookierParser from 'cookie-parser';
import cors from 'cors';
import { PORT } from '../env';
import { schemaWithMiddleware } from './schema';
Expand Down Expand Up @@ -41,9 +42,15 @@ export const startServer = async (
const apollo = createApolloServer();

await apollo.start();
app.use('/graphql', cors<cors.CorsRequest>(), bodyParser.json(), expressMiddleware(apollo, {
context: async (params) => (createContext(params)),
}));
app.use(
'/graphql',
cors<cors.CorsRequest>(),
cookierParser(undefined, { decode: (value: string) => value }),
bodyParser.json(),
expressMiddleware(apollo, {
context: async (params) => (createContext(params)),
}),
);

return httpServer.listen({ port: PORT }, () => {
logger.info(`Running on ${PORT}`);
Expand Down