GraphQL Apollo Server 4: How to set response headers and cookies

GraphQL Apollo Server 4: How to set response headers and cookies


0

I got a basic setup of Apollo Server 4 to be integrated with Azure Functions.
I followed this guide:
https://www.apollographql.com/docs/apollo-server/migration

And for the connection to Azure Functions I’m using the officially supported integration package:
https://github.com/apollo-server-integrations/apollo-server-integration-azure-functions#readme

I was able to set cookies and response headers with Apollo Server 3, but I can’t get it working with v4. Nothing is being sent.

Ideas?

index.ts

import { ApolloServer } from '@apollo/server';
import {
    AzureFunctionsContextFunctionArgument,
    startServerAndCreateHandler,
} from '@as-integrations/azure-functions';
import type {
    AzureFunction,
    Context,
} from '@as-integrations/azure-functions/node_modules/@azure/functions';
import { myGraphQLApp } from '../src/index';
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
import { GraphQLSchema } from 'graphql';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { applyMiddleware } from 'graphql-middleware';
import { enforceCorrelationId } from '../src/middleware/enforceCorrelationId';

const { typeDefs, resolvers } = myGraphQLApp;

export interface ContextExtended extends Context {
    accessToken?: string;
}

const schema: GraphQLSchema = makeExecutableSchema({ typeDefs, resolvers });
const schemaWithMiddleware: GraphQLSchema = applyMiddleware(schema, enforceCorrelationId());

const server: ApolloServer = new ApolloServer({
    schema: schemaWithMiddleware,
    csrfPrevention: true,
    introspection: true,
    cache: 'bounded',
    plugins: [ApolloServerPluginLandingPageLocalDefault({ footer: false })],
});

export default <AzureFunction>startServerAndCreateHandler(server, {
    context: async ({
        context,
        req,
    }: AzureFunctionsContextFunctionArgument): Promise<ContextExtended> => {
        return {
            accessToken: 'foo-test',
            ...context,
        };
    },
});

resolvers.ts

import 'graphql-import-node';
import typeDefs from './schema.graphql';
import { ContextExtended } from '../HttpTriggerGraphQL';

class MyGraphQLApp {
    public get typeDefs() {
        return typeDefs;
    }
    public get resolvers() {
        const resolvers = {
            Query: {
                hello: (_: unknown, args: unknown, context: ContextExtended) => {
                    const cookie =
                        'accessToken=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c; Max-Age=3600; HttpOnly=true; SameSite=None';
                    
                    // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie

                    // A similar approach worked with V3 but not V4 ..
                    context.res.headers['Set-Cookie'] = cookie; // Dud
                    context.res.headers['x-foo'] = 'bar'; // Dud
                    context.res.cookies.push(cookie); // Dud
                    context.res.set({'x-foo': 'bar'}); // Dud

                    return 'hello';
                },
            },
        };
        return resolvers;
    }
}

export const myGraphQLApp = new MyGraphQLApp();


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *