Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No request in apollo context (context is an empty object) #1

Closed
mattlgroff opened this issue Jan 29, 2023 · 2 comments
Closed

No request in apollo context (context is an empty object) #1

mattlgroff opened this issue Jan 29, 2023 · 2 comments

Comments

@mattlgroff
Copy link

When I set the context in the Apollo Server Plugin, it doesn't get the request object. I believe this is because of the way Apollo Server 4 changed their context object.

My code:

// Apollo Server Plugin for Elysia
app.use(
  apollo({
      path: '/graphql',
      enablePlayground: true, // Default is disabled in production
      typeDefs, // Import schema from .graphql file
      resolvers, // Import resolvers from resolvers.js,
      context: async (foo) => {

        console.log('foo', foo); // {}

        return {
          dataSources: {

          }
        }
      }
  }),
);

In this case, any request I make the foo object is empty {} with no request information.

https://www.apollographql.com/docs/apollo-server/migration/#context-initialization-function
image

Could this be done in a similar way to how the express middleware for Apollo Server 4 handles the change?

Or maybe I'm missing something obvious to capture the request object in my context, if so, sorry!

@SaltyAom
Copy link
Member

Because Elysia is based on Web Standard Request and Response which is different from Node's HttpRequest and HttpResponse that Express use, result in req, and res being undefined in context.

However, we can add a web standard's Request instead to make a suitable equivalent.

@SaltyAom
Copy link
Member

SaltyAom commented Jan 29, 2023

Should be working in 0.2.1 with e26dc99.

However, do note that the req and res is not as same as Apollo, but it's Elysia's Context instead.

You should be able to get Request header by accessing context.request.headers.get('Authorization')

const app = new Elysia()
    .use(
        apollo({
            typeDefs,
            resolvers,
            context: async (context) => {
                const authorization = context.request.headers.get('Authorization')

                return {}
            }
        })
    )
    .listen(3000)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants