Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
Apollo Server executeOperation with authorization headers

How do we pass headers to Apollo server executeOperation in tests?

There is mention about passing a headers object here

I'm trying to pass an auth header with or without a JWT token to test access control.

const result = await server.executeOperation({ query: query, http: { headers: { authorization: "" } } })

// Type '{ authorization: string; }' is not assignable to type 'Headers'.
//  Object literal may only specify known properties, and 'authorization' does not exist in type 'Headers'.ts(2322)

This results in a type error. There is a Headers class defined in the Apollo server types in fetch.d.ts but I'm un able to import to instantiate it.

Using "apollo-server": "^2.25.2". Any hints or links to get this going?


Update: as a work around I'm decrypting and decoding the JWT in the server context and passing an authenticated user around in there. Then I'm able to mock the whole context and create a new test server with the mocked context. It'd be nice to be able to user headers for more production like experience but this works for now.

import { mockDeep, mockReset } from 'jest-mock-extended'

interface Context {
  prisma: PrismaClient
  user: () => User|null
}

export const context = mockDeep<Context>()

export const testServer = new ApolloServer({
  typeDefs,
  resolvers,
  context
});

// ...

context.user.mockReturnValue({
  id: 1,
  name: "Foo",
  slug: "foo",
})

const res = await testServer.executeOperation({ query: query })
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

To solve this, I have created a 'fake' request object with the already decrypted JWT token that I used to initialize my third party auth object (keycloak) and pass to the apollo context. I need to initialize the keycloak object because I have authentication schema directives that require the keycloak object to be initialized.

 const req = {
      kauth: {
        grant: {
          access_token: {
            isExpired: () => {
              return false;
            },
            token: "abc",
            content: {
              email: "me@me.com",
              resource_access: {
                "my-api": {
                  roles: ["admin"],
                },
              },
            },
          },
        },
      },
    };

    server = new ApolloServer({
      schema,
      resolvers,
      dataSources: () => ({
        users,
      }),
      context: () => {
        return { kauth: new KeycloakContext({ req }) };
      },
    });
  });

Still, I would like a solution that is more native to apollo and not a work around.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!