Using Better Auth in the NodeJS (Express) with Apollo GraphQL #197

Closed
opened 2026-03-13 07:37:10 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @alfUMi on GitHub (Nov 8, 2024).

Is your feature request related to a problem? Please describe.
I'm working with Express, Apollo GraphQL and Prisma and I'd like to use Better Auth to create authentication. The documentation has examples of setting it up in NodeJS and Express, but it describes a solution with a REST API.

Describe the solution you'd like
I would like to see examples of how this can be used with GraphQL, if it is even possible of course. I'm interested in how exactly to initialize betterAuth, how to pass it to the context for apollo server.

Based on the documentation, I can use this solution in the context of Apollo Server.

import { betterAuth } from "better-auth";
import { headers } from "next/headers";
 
export const auth = betterAuth({
    //...
})
 
// calling get session on the server
await auth.api.getSession({
    headers: headers()
})

But there is a question about what to do on the client side. The documentation says that I need to install and use betterAuth on both the server and the client, but in the case of GraphQL, I use Apollo Client and the useMutation hook, which passes GraphQL schemas.

Originally created by @alfUMi on GitHub (Nov 8, 2024). **Is your feature request related to a problem? Please describe.** I'm working with Express, Apollo GraphQL and Prisma and I'd like to use Better Auth to create authentication. The documentation has examples of setting it up in NodeJS and Express, but it describes a solution with a REST API. **Describe the solution you'd like** I would like to see examples of how this can be used with GraphQL, if it is even possible of course. I'm interested in how exactly to initialize betterAuth, how to pass it to the context for apollo server. Based on the documentation, I can use this solution in the context of Apollo Server. ```ts import { betterAuth } from "better-auth"; import { headers } from "next/headers"; export const auth = betterAuth({ //... }) // calling get session on the server await auth.api.getSession({ headers: headers() }) ``` But there is a question about what to do on the client side. The documentation says that I need to install and use betterAuth on both the server and the client, but in the case of GraphQL, I use Apollo Client and the useMutation hook, which passes GraphQL schemas.
Author
Owner

@acomito commented on GitHub (Apr 19, 2025):

were you able to do this? Is it just a matter of an apollo client link on the frontend to set the token in the headers and then using get session inside of the context creation function of apollo server?

@acomito commented on GitHub (Apr 19, 2025): were you able to do this? Is it just a matter of an apollo client link on the frontend to set the token in the headers and then using get session inside of the context creation function of apollo server?
Author
Owner

@alfUMi commented on GitHub (Apr 22, 2025):

@acomito

I did it this way:

In Apollo GraphQL in the context I take the token from headers, convert them using fromNodeHeaders to the required format and send them to the session getting method

const session = await auth.api.getSession({
   headers: fromNodeHeaders(headers),
})

And for all requests in betterAuth I wrote my own typedefs and resolvers and called them (I used their auth.api)

I didn't make any special changes to the frontend.

@alfUMi commented on GitHub (Apr 22, 2025): @acomito I did it this way: In Apollo GraphQL in the context I take the token from headers, convert them using ```fromNodeHeaders``` to the required format and send them to the session getting method ```ts const session = await auth.api.getSession({ headers: fromNodeHeaders(headers), }) ``` And for all requests in betterAuth I wrote my own typedefs and resolvers and called them (I used their auth.api) I didn't make any special changes to the frontend.
Author
Owner

@junwen-k commented on GitHub (Aug 18, 2025):

It would be fantastic if there were a way to automatically map all better-auth endpoints to GraphQL query and mutation operations — similar to how PayloadCMS does it. For example, their me endpoint works like this:

GET http://localhost:3000/api/[collection-slug]/me

query {
  me[collection-singular-label] {
    user {
      email
    }
    exp
  }
}

From what I understand, better-auth is powered by better-call, so I wonder if something similar could be achieved here.

@junwen-k commented on GitHub (Aug 18, 2025): It would be fantastic if there were a way to automatically map all better-auth endpoints to GraphQL query and mutation operations — similar to how PayloadCMS does it. For example, their [me](https://payloadcms.com/docs/authentication/operations#me) endpoint works like this: `GET http://localhost:3000/api/[collection-slug]/me` ```graphql query { me[collection-singular-label] { user { email } exp } } ``` From what I understand, `better-auth` is powered by [better-call](https://github.com/Bekacru/better-call), so I wonder if something similar could be achieved here.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#197