GraphQL helpers for quickly crafting resolvers with Objection.JS
MIT License
@sammarks/objection-graphql
is a set of helper methods for quickly crafting resolvers with GraphQL and Objection.JS.
npm install @sammarks/objection-graphql
yarn add @sammarks/objection-graphql
On each of your models, decorate them with withGraphQLSupport
import { withGraphQLSupport } from '@sammarks/objection-graphql'
@withGraphQLSupport()
export default class User extends Model {
static tableName = 'users'
static get relationMappings () {
return {
posts: {
modelClass: Post,
relation: Model.HasManyRelation,
join: {
from: 'users.id',
to: 'posts.user_id'
}
}
}
}
}
Now, in your GraphQL resolvers:
import { getResolver, nodeResolver } from '@sammarks/objection-graphql'
import User from './models/User'
const resolvers = {
Node: nodeResolver,
User: getResolver(User)
}
And then the following GraphQL schema will automatically be supported.
enum OrderDirection {
ASC
DESC
}
type User {
# Standard user fields...
id: ID!
firstName: String!
lastName: String!
# Relationships!
posts(
first: Int = 10,
after: String,
orderBy: String = "id",
orderDirection: OrderDirection = ASC
): PostsConnection!
}
See objection-graphql-relay for some more information.
Automatically generates a resolver for the passed Objection.JS model. Currently does not support extra customization.
Will automatically generate resolvers for single and multiple relationships. See the source code
for orderedPagedRelationQuery()
for more information.
This is just a pretty standard GraphQL Node resolver. See the code sample above for how to use this. It just takes the constructor name of the Model and uses that to determine the GraphQL type (so make sure those are the same!)
Decorator for adding helper methods required for the automatic connection creator inside getResolver()
.
Returns a function that accepts the model to decorate. There are currently no configuration options.