# objection-graphql-resolver

> objection.js plugin: resolve GraphQL query using withGraphFetched

Latest version **7.7.1** (published 2026-11-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install objection-graphql-resolver
pnpm add objection-graphql-resolver
yarn add objection-graphql-resolver
bun add objection-graphql-resolver
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 7.7.1 |
| Published | 2026-11-28 |
| First published | 2021-01-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=14 |
| Dependencies | 1 |
| Unpacked size | 62.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 18 |
| Author | Ilya Semenov |
| Maintainers | ilyasemenov |

## Links

- npm: https://www.npmjs.com/package/objection-graphql-resolver
- Repository: https://github.com/IlyaSemenov/graphql-orm
- Homepage: https://ilyasemenov.github.io/graphql-orm/objection/
- Issues: https://github.com/IlyaSemenov/graphql-orm/issues
- npm.io page: https://npm.io/package/objection-graphql-resolver

## Dependencies (1)

- [graphql-parse-resolve-info](https://npm.io/package/graphql-parse-resolve-info.md) ^4.13.0

## Recent versions

- 7.7.1 (latest) — 2026-11-28
- 7.7.0 — 2026-11-28
- 7.6.2 — 2026-11-28
- 7.6.1 — 2025-07-23
- 7.6.0 — 2025-07-22
- 7.5.1 — 2024-12-13
- 7.5.0 — 2024-12-10
- 7.4.0 — 2024-06-21
- 7.3.1 — 2024-02-24
- 7.3.0 — 2024-02-23
- 7.2.2 — 2023-10-23
- 7.2.1 — 2023-08-07
- 7.2.0 — 2023-08-07
- 7.1.0 — 2023-05-30
- 7.0.0 — 2023-05-29
- … 13 more at https://npm.io/package/objection-graphql-resolver/versions

## README

# objection-graphql-resolver

A helper library to resolve GraphQL queries directly with [Objection.js](https://vincit.github.io/objection.js/) models and relations.

- Selects the columns and relations that the GraphQL query asks for, using `withGraphFetched`.
- Resolves relations nested to any depth by traversing `relationMappings`.
- Paginates root queries and to-many relations with a cursor.
- Resolves model getters the same way as database columns.
- Filters queries with GraphQL arguments like `{ date: "2020-10-01", category__in: ["News", "Politics"] }`.
- Limits the exposed fields, restricts rows with query modifiers, and post-processes field values per viewer.

_Note that there is a sister project: `orchid-graphql` which does the same for [Orchid ORM](https://orchid-orm.netlify.app)._

See the [documentation](https://ilyasemenov.github.io/graphql-orm/objection/).

## Install

```sh
npm i objection-graphql-resolver objection knex graphql
```

The example below also uses Apollo Server, an SQLite driver, and a GraphQL client:

```sh
npm i @apollo/server graphql-tag sqlite3 graphql-request
```

## Minimal all-in-one example

Run the GraphQL server:

```ts
// Everything is put into a single file for demonstration purposes.
//
// In real projects, you will want to separate models, typedefs,
// model resolvers, and the server into their own modules.

import type { ApolloServerOptions } from "@apollo/server"
import { ApolloServer } from "@apollo/server"
import { startStandaloneServer } from "@apollo/server/standalone"
import gql from "graphql-tag"
import Knex from "knex"
import { Model } from "objection"
import * as r from "objection-graphql-resolver"

// Define Objection.js models

class PostModel extends Model {
  static tableName = "post"

  declare id?: number
  declare text?: string
}

// Define GraphQL schema

const typeDefs = gql`
  type Post {
    id: Int!
    text: String!
  }

  type Mutation {
    create_post(text: String!): Post!
  }

  type Query {
    posts: [Post!]!
  }
`

// Map GraphQL types to model resolvers

const graph = r.graph({
  Post: r.model(PostModel),
})

// Define resolvers

const resolvers: ApolloServerOptions<any>["resolvers"] = {
  Mutation: {
    async create_post(_parent, args, context, info) {
      const post = await PostModel.query().insert(args)
      return graph.resolve(post.$query(), { context, info })
    },
  },
  Query: {
    posts(_parent, _args, context, info) {
      return graph.resolve(PostModel.query().orderBy("id"), { context, info })
    },
  },
}

// Configure database backend

const knex = Knex({ client: "sqlite3", connection: ":memory:" })
Model.knex(knex)

await knex.schema.createTable("post", post => {
  post.increments("id")
  post.text("text").notNullable()
})

// Start GraphQL server

const server = new ApolloServer({ typeDefs, resolvers })
const { url } = await startStandaloneServer(server, {
  listen: { port: 4000 },
})
console.log(`Listening on ${url}`)
```

Query it with a GraphQL client:

```ts
import { GraphQLClient } from "graphql-request"
import gql from "graphql-tag"

const client = new GraphQLClient("http://127.0.0.1:4000")

await client.request(
  gql`
    mutation create_post($text: String!) {
      new_post: create_post(text: $text) {
        id
      }
    }
  `,
  { text: "Hello, world!" },
)

const { posts } = await client.request(gql`
  query {
    posts {
      id
      text
    }
  }
`)

console.log(posts)
```

---
_Source: https://npm.io/package/objection-graphql-resolver · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
