# Apollo Server 4 - Cannot read properties of undefined (reading 'headers')

**URL:** <https://community.apollographql.com/t/apollo-server-4-cannot-read-properties-of-undefined-reading-headers/6601>\
**Category:** Server\
**Tags:** server\
**Created:** [August 15, 2023, 7:01pm UTC](https://community.apollographql.com/t/apollo-server-4-cannot-read-properties-of-undefined-reading-headers/6601 "2023-08-15T19:01:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![graphqqqq](https://avatars.discourse-cdn.com/v4/letter/g/94ad74/32.png) [@graphqqqq](https://community.apollographql.com/u/graphqqqq)\
**Post date:** [August 15, 2023, 7:01pm UTC](https://community.apollographql.com/t/apollo-server-4-cannot-read-properties-of-undefined-reading-headers/6601/1 "2023-08-15T19:01:24Z")

</div>

hi, i upgraded to apollo 4 and i am using koa js. But there are some problems with context. Authentication process does not work. Authentication time I get an error like this:

```auto
"message": "Cannot read properties of undefined (reading 'headers')"
"code": "INTERNAL_SERVER_ERROR",

@apollo+server@4.9.1_graphql@16.7.1/node_modules/@apollo/server/dist/esm/utils/schemaInstrumentation.js:36:28)

backend/graphql/resolvers/posts.js:31:20

backend/util/check-auth.js:5:36

```

# Code:

# appjs file:

```auto
import { ApolloServer } from "@apollo/server";
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import corsKoa from "@koa/cors";
import graphqlUploadKoa from 'graphql-upload/graphqlUploadKoa.mjs';
import { koaMiddleware as apolloServerKoa } from "@as-integrations/koa";
import http from "http";
import Koa from "koa";
import bodyParserKoa from "koa-bodyparser";

const app = new Koa();

import typeDefs from "./graphql/typeDefs.js"
import resolvers from "./graphql/resolvers/index.js"

const httpServer = http.createServer(app.callback());

const server = new ApolloServer({
    typeDefs,
    resolvers,
     context: ({ req }) => ({ req }),
    plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});

await server.start();

app.use(corsKoa());
app.use(
  graphqlUploadKoa({
    maxFileSize: 10000000, // 10 MB
    maxFiles: 20,
  })
);
app.use(bodyParserKoa());
app.use(apolloServerKoa(server));

httpServer.listen(process.env.PORT, () => {
  console.info(
    `Serving http://localhost:${process.env.PORT} for ${process.env.NODE_ENV}.`
  );
});

```

# Check-auth file:

```auto
import { GraphQLError } from "graphql"
import jwt from "jsonwebtoken"

export default (context) => {
    const authHeader = context.req.headers.authorization;
    if(authHeader) {
        const token = authHeader.split("Bearer ")[1];
        if(token) {
            try {
                const user = jwt.verify(token, process.env.SECRET_KEY);
                return user
            } catch(err) {
                throw new GraphQLError('Invalid/Expired token');
            }
        } 
        throw new Error("Authentication token must be 'Bearer [token]");
    } 
    throw new Error('Authorization header must be provided');
}

```

---

<div class="post-metadata">

**Author:** ![trevor.scheer](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/trevor.scheer/32/5669_2.png) [@trevor.scheer](https://community.apollographql.com/u/trevor.scheer)\
**Post date:** [August 18, 2023, 7:17pm UTC](https://community.apollographql.com/t/apollo-server-4-cannot-read-properties-of-undefined-reading-headers/6601/2 "2023-08-18T19:17:13Z")

</div>

I’m not seeing the reference to your check-auth file anywhere in the app file, so it’s hard to know how that’s having an effect. Can you share a repo (or better yet a minimal reproduction) with instructions on how to reproduce the issue?

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:48pm UTC](https://community.apollographql.com/t/apollo-server-4-cannot-read-properties-of-undefined-reading-headers/6601/3 "2025-02-06T20:48:29Z")

</div>


