Apollo Server TypeScript How can I map snake_case database fields to camelCase GraphQL fields?

I’m using Apollo Server with TypeScript and GraphQL Code Generator.

My GraphQL schema uses camelCase field names:

type User {
  id: ID!
  createdAt: String!
}

The data returned from my database uses snake_case:

const user = {
  id: 1,
  created_at: "2025-01-01T10:00:00Z",
};

My query resolver simply returns the database object:

const resolvers: Resolvers = {
  Query: {
    user: async () => {
      return user;
    },
  },
};

I would like to resolve createdAt from created_at using a field resolver:

const resolvers: Resolvers = {
  User: {
    createdAt: (parent) => {
      return parent.created_at;
    },
  },
};

However, TypeScript reports an error:

Property 'created_at' does not exist on type 'User'.

It seems that the generated resolver types use the GraphQL type shape (createdAt), while at runtime the resolver receives the raw database object (created_at).

As a result, TypeScript only exposes:

parent.createdAt

but the actual value I need is:

parent.created_at

Questions

  1. What is the recommended way to map snake_case database fields to camelCase GraphQL fields when using Apollo Server and TypeScript?

  2. Is it possible to configure the generated resolver parent type so that parent reflects the database model rather than the GraphQL type?

  3. Should I instead transform all database results from snake_case to camelCase before returning them from the query resolver?

  4. How do people typically handle this pattern in Apollo Server + GraphQL Code Generator projects while keeping full type safety?

For example, what would be the idiomatic way to expose created_at from the database as createdAt in the GraphQL schema?

Read more here: Source link