title: Apollo Server: How to access 'context' outside of resolvers in Dataloader from REST API Datasource
body: <p>hopefully someone can help me with this little problem, I just cannot figure it out right now.</p>
  <p><strong>Problem Statement:</strong></p>
  <p>I want to access 'context' for the sake of authentication in my <code>DataLoader</code>. This <code>DataLoader</code>is defined in a seperate path <code>/loaders</code>.  In my <code>resolvers.js</code> file I can access my context nicely with <code>dataSources.userAPI.getAllUsers()</code>.
  But how to access it anywhere else in my serverside application, like f.e. in my <code>/loaders</code> folder?
  I just cant get it how to get access to my context object to then pass the token to the <code>DataLoader</code> to then load the data from my API and then pass this data to my <code>resolvers.js</code> file.
  Every help is highly appreciated, I don't know how to solve this simple thing .. Thanks!</p>
  <p><strong>Here comes the code:</strong></p>
  <p><strong>index.js</strong></p>
  <pre><code>const express = require('express');
  const connectDB = require('./config/db');
  const path = require('path');
  var app = express();
  const cors = require('cors')
  const axios = require('axios')
  
  // apollo graphql
  const { ApolloServer } = require('apollo-server-express');
  const DataLoader = require('dataloader')
  const { userDataLoader } = require('./loaders/index')
  
  // Connect Database
  connectDB();
  
  // gql import
  const typeDefs = require('./schema');
  const resolvers = require('./resolvers')
  
  // apis
  const UserAPI = require('./datasources/user')
  
  
  // datasources
  const dataSources = () =&gt; ({
      userAPI: new UserAPI(),
  });
  
  // context
  const context = ({ req, res }) =&gt; ({
  
      token: req.headers.authorization || null,
      loaders: {
          userLoader: userDataLoader,
      },
      res
  })
  
  
  // init server
  const server = new ApolloServer({
      typeDefs,
      resolvers,
      dataSources,
      context
  });
  
  // middleware
  app.use(express.json());
  
  
  // cors
  var corsOptions = {
      credentials: true
  }
  app.use(cors(corsOptions))
  
  
  // serve middleware
  server.applyMiddleware({
      app
  });
  
  
  // run server
  app.listen({ port: 4000 }, () =&gt;
      console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
  );
  
  module.exports = {
      dataSources,
      context,
      typeDefs,
      resolvers,
     loaders,
      ApolloServer,
      UserAPI,
      server,
  };
  </code></pre>
  <p><strong>loaders/index.js</strong></p>
  <pre><code>   const userDataLoader = require('./user')
  
  module.exports = {
      userDataLoader
  }
  </code></pre>
  <p><strong>loaders/user.js</strong></p>
  <pre><code>const UserAPI = require('../datasources/users')
  // init loader
  const userDataLoader = new DataLoader(keys =&gt; batchUser(keys))
  
  // batch
  const batchUsers = async (keys) =&gt; {
  
     // this part is not working!
     // How to access the UserAPI methods in my DataLoader?
     // Or lets say: How to access context from here,
     // so I can add auth for the server I am requesting data from?
  
      const userAPI = new UserAPI()
      const users = userAPI.getAllUsers()
          .then(res =&gt; {
              return res.data
          })
  
  
      return keys.map(userId =&gt; users.find(user=&gt; user._id === userId))
  }
  
  module.exports = userDataLoader
  </code></pre>
  <p><strong>resolvers.js</strong></p>
  <pre><code>// here is just my api call to get the data from my
  // dataloader with userLoader.load() and this works perfectly
  // if I just make API calls with axios in my loaders/user
  // here just a little snippet from the resolver file
  
  ....
  users: async (parent, args, { loaders }) =&gt; {
              const { userLoader } = loaders
              if (!parent.users) {
                  return null;
              }
              return await userLoader.load(parent.user)
          },
  ....
  
  </code></pre>
  <p><strong>datasources/user.js</strong></p>
  <pre><code>const { RESTDataSource } = require('apollo-datasource-rest');
  
  class UserAPI extends RESTDataSource {
      constructor() {
          super()
          this.baseURL = 'http://mybaseurl.com/api'
      }
  
  
      willSendRequest(request) {
          request.headers.set('Authorization',
              this.context.token
          );
      }
  
      async getUserById(id) {
          return this.get(`/users/${id}`)
      }
  
      async getAllUsers() {
          const data = await this.get('/users');
          return data;
      }
  }
  
  module.exports = UserAPI;
  </code></pre>