npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@eloquentjs/graphql

v0.0.3

Published

Auto-generated GraphQL schema and resolvers from EloquentJS models

Readme

@eloquentjs/graphql

Auto-generate a complete GraphQL schema and resolvers from your EloquentJS models. Works with Apollo Server, GraphQL Yoga, Mercurius, and any spec-compliant GraphQL server.

npm install @eloquentjs/core @eloquentjs/codegen @eloquentjs/graphql graphql

Powered by @eloquentjs/codegen: SDL generation is handled by the shared codegen engine, ensuring consistency with TypeScript types, OpenAPI specs, and CLI-generated schema files.


Two Ways to Build a Schema

1. From live Model classes

import { buildSchema } from '@eloquentjs/graphql'
import { ApolloServer } from '@apollo/server'
import { User, Post, Comment } from './models/index.js'

const { typeDefs, resolvers } = buildSchema([User, Post, Comment])
const server = new ApolloServer({ typeDefs, resolvers })

2. From a models directory (auto-loads all model files)

import { buildSchemaFromDir } from '@eloquentjs/graphql'

const { typeDefs, resolvers } = await buildSchemaFromDir('./app/models', {
  auth: async (ctx) => authenticateUser(ctx),
})
const server = new ApolloServer({ typeDefs, resolvers })

3. Write schema.graphql to disk (CLI)

# Requires @eloquentjs/cli and @eloquentjs/codegen
eloquent generate:graphql
eloquent generate:graphql --pagination=relay --out=src/schema.graphql
eloquent generate:graphql --models=User,Post --no-subscriptions

Generated Schema

Given a User model with casts = { name: 'string', is_admin: 'boolean' }:

scalar JSON
scalar DateTime

type User {
  id: ID
  name: String
  is_admin: Boolean
  created_at: DateTime
  updated_at: DateTime
}

input CreateUserInput { name: String  is_admin: Boolean }
input UpdateUserInput { name: String  is_admin: Boolean }
input UserWhereInput  { id: ID  name: String  AND: [UserWhereInput]  OR: [UserWhereInput] }

type UserPage { data: [User!]!  meta: PaginationMeta! }

type Query {
  user(id: ID!): User
  users(where: UserWhereInput, orderBy: String, orderDir: String, page: Int, perPage: Int): UserPage
  usersCount(where: UserWhereInput): Int!
}

type Mutation {
  createUser(input: CreateUserInput!): User!
  updateUser(id: ID!, input: UpdateUserInput!): User!
  deleteUser(id: ID!): Boolean!
  upsertUser(where: UserWhereInput!, input: CreateUserInput!): User!
}

type Subscription {
  userCreated: User!
  userUpdated: User!
  userDeleted: ID!
}

For models with softDeletes = true, also generates restoreUser and forceDeleteUser mutations.


Options

const { typeDefs, resolvers } = buildSchema([User, Post, Comment], {
  pagination:    'offset',     // 'offset' (default) | 'relay'
  subscriptions: true,
  auth: async (ctx) => {
    const token = ctx.req.headers.authorization?.replace('Bearer ', '')
    return token ? User.where('api_token', token).firstOrFail() : null
  },
  scalars: ['Upload', 'BigInt'],
})

Per-Model Configuration

class Post extends Model {
  static graphql = {
    fields:       { secret_hash: false, internal_notes: false },  // hide fields
    queries:      { deletePost: false, forceDeletePost: false },   // disable operations
    subscription: false,                                           // no subscriptions
    middleware:   [requireAuth, logQuery],                         // per-resolver middleware
  }
}

Relay Pagination

const { typeDefs, resolvers } = buildSchema([User], { pagination: 'relay' })
// type UserEdge { node: User!  cursor: String! }
// type UserConnection { edges: [UserEdge!]!  pageInfo: PageInfo!  totalCount: Int! }

Extending Auto-Generated Resolvers

const { typeDefs, resolvers } = buildSchema([User, Post], { auth })

resolvers.Query.feed = async (_, { page = 1, perPage = 10 }, ctx) => {
  return Post.published().with('user', 'tags').latest().paginate(page, perPage)
}

resolvers.Mutation.likePost = async (_, { postId }, ctx) => {
  if (!ctx.user) throw new Error('Unauthorized')
  await Like.updateOrCreate(
    { likeable_type: 'Post', likeable_id: postId, user_id: ctx.user.id }, {}
  )
  return Post.find(postId)
}

resolvers.User.postsCount = async (parent) => Post.where('user_id', parent.id).count()

With GraphQL Yoga

import { createYoga } from 'graphql-yoga'
import { buildSchema } from '@eloquentjs/graphql'

const { typeDefs, resolvers } = buildSchema([User, Post])
const yoga = createYoga({ typeDefs, resolvers })
server.use('/graphql', yoga)

With Mercurius (Fastify)

import Fastify from 'fastify'
import mercurius from 'mercurius'
import { buildSchema } from '@eloquentjs/graphql'

const app = Fastify()
const { typeDefs, resolvers } = buildSchema([User, Post])
app.register(mercurius, { schema: typeDefs, resolvers })

Using Codegen Directly

For advanced use — generate SDL without resolvers, inspect field types, or combine with TypeScript generation:

import { introspect, generateGraphqlSchema, generateTypeScriptFile } from '@eloquentjs/codegen'

const schemas = [User, Post].map(introspect)

// SDL only (no resolvers)
const sdl = generateGraphqlSchema(schemas, { pagination: 'relay' })

// TypeScript types from the same schema objects
const types = generateTypeScriptFile(schemas)

See the @eloquentjs/codegen README for the full API.


License

MIT