pass httponly cookies through apollo graphlql server

Viewed 80

GOAL: have my api gateway get the httponly cookies being returned from my rest endpoints and pass it along to frontend, also the front end should be able to pass the cookies through.

httpO=httponly

   SPA(react)              apiGateway(apolloQL)            restEndpoint
httpO-cookies---->     <-----(httpO)cookies----->      <-----(httpO)cookies

current the resolvers I have are able to see the "set-cookies" in the response from the endpoints but throughout the response lifecycle the header's are lost.

const apolloServer: ApolloServer = new ApolloServer({
    context: ({ res }) => {
        
        // console.log(res,"res");
        return ({
            res
        });

    },
    formatError,
    resolvers,
    typeDefs,
    formatResponse: (response: GraphQLResponse) => {
        console.log(response.http?.headers, "header?");
        return {
            headers: {
                'Access-Control-Allow-Headers': 'Content-Type',
                'Access-Control-Allow-Credentials': 'true',
            },
            data: response.data,
            errors: response.errors,
        };
    }
});

ex. of resolver:

const signupUser = async (parent: any, args: any, context: any, info: any) => {
    try {
        const response = await UserService.createUser(args);
        console.log(response , "response");
        return response;
    } catch (error) {
        console.log(error
    }
};

in this example lets assume the UserService.createUser return the entire response Object, not response.data.

1 Answers
const signupUser = async (parent: any, args: any, context: any, info: any) => {
    try {
        //call get the Express response from the context
        const contextResponse = context.res
        //or 
        const {res} = context

        //then set the cookies to the response
        const response = await UserService.createUser(args);
        contextResponse.header('set-cookie', response?.headers['set-cookie']);

        //return the original data
        return response?.data;
    } catch (error) {
        console.log(error
    }
};
Related