objection-graphql
Automatic GraphQL API generator for objection.js models.
Usage
objection-graphql automatically generates a GraphQL schema
for objection.js models. The schema is created based on the jsonSchema
and relationMappings
properties of the models. It creates a rich set of filter arguments for the
relations and provides a simple way to add custom filters.
The following example creates a schema for three models Person
, Movie
and Review
and executes a GraphQL query:
const graphql = graphql;const graphQlBuilder = builder; // Objection.js models.const Movie = ;const Person = ;const Review = ; // This is all you need to do to generate the schema.const graphQlSchema = ; // Or: // const models = [Movie, Person, Review]// const graphQlSchema = graphQlBuilder().allModels(models).build(); // Execute a GraphQL query.;
The example query used some of the many default filter arguments. For example the nameLike: "%erminato%"
filter is mapped into a where clause where name like '%erminato%'
. Similarily the ageLte: 100
is mapped into
a where age <= 100
clause. In addition to the property filters there are some special arguments like orderBy
and
range
. Check out this table for a full list of filter arguments available by default.
Getting started
If you are already using objection.js the example in the usage section is all you need to get started. If you are unfamiliar with objection.js you should try our example project.
Filters
argument | type | action |
---|---|---|
prop: value |
property type | prop = value |
propEq: value |
property type | prop = value |
propGt: value |
property type | prop > value |
propGte: value |
property type | prop >= value |
propLt: value |
property type | prop < value |
propLte: value |
property type | prop <= value |
propLike: value |
string | prop LIKE value |
propIsNull: value |
boolean | prop IS NULL or prop IS NOT NULL |
propIn: value |
Array | prop IN value |
propNotIn: value |
Array | prop NOT IN value |
propLikeNoCase: value |
string | lower(prop) LIKE lower(value) |
Special arguments
argument | action |
---|---|
orderBy: prop |
Order the result by some property |
orderByDesc: prop |
Order the result by some property in descending order |
range: [start, end] |
Select a range. Doesn't work for relations! |
limit: prop |
Select a given number of records. |
offset: prop |
Skip a given number of records. |
Adding your own custom arguments
Here's an example how you could implement a NotEq
filter for primitive values:
const graphql = ; const graphQlSchema = ;
Extending your schema with mutations
Often you need to provide mutations in your GraphQL schema. At the same time mutations can be quite opinionated with side effects and complex business logic, so plain CUD implementation is not always a good idea.
Therefore we provide a method extendWithMutations
which allows you to extend the generated query schema with mutations. You can provide a root GraphQLObjectType
or a function as a first argument for this method.
Function in this case plays as a strategy which receives current builder as a first argument and returns GraphQLObjectType
.
//...const personType = name: 'PersonType' description: 'Use this object to create new person' id: type: GraphQLInt description: 'First Name' firstName: type: GraphQLString description: 'First Name' lastName: type: GraphQLString description: 'Last Name' ; const createPersonInputType = name: 'CreatePersonType' description: 'Person' firstName: type: GraphQLString description: 'First Name' lastName: type: GraphQLString description: 'Last Name' ; const mutationType = name: 'RootMutationType' description: 'Domain API actions' createPerson: description: 'Creates a new person' type: personType args: input: type: createPersonInputType { const firstName lastName = inputPersoninput; return id: 1 firstName lastName ; } ; //Here you can use a GraphQLObjectType or function as an argument for extendWithMutationsschema = mainModule ;
Extending your schema with subscriptions
When you want to implement a real-time behavior in your app like push notifications, you basically have two options in graphql: subscriptions and live queries. The first approach is focused on events and granular control over updates, while the other is based on smart live queries, where most of real-rime magic is hidden from the client. We'd like to stick with the first approach since there are some decent implementations out there like graphql-subscriptions by Apollo.
The implementation is similar to mutations extention point: you've got an extendWithSubscriptions
method where you can pass the root GraphQLObjectType
or a function which can bahave as a strategy which receives current builder as an argument.
//...;const pubsub = ;//...const personType = name: 'PersonType' description: 'Person' id: type: GraphQLInt description: 'First Name' firstName: type: GraphQLString description: 'First Name' lastName: type: GraphQLString description: 'Last Name' ; const subscriptionType = name: 'RootSubscriptionType' description: 'Domain subscriptions' personCreated: description: 'A new person created' type: personType payload pubsub ; //Here you can use a GraphQLObjectType or function as an argument for extendWithSubscriptionsschema = mainModule ;
Misc
defaultArgNames
You can change the default filter suffixes and special filter names using the defaultArgNames
method:
const graphQlSchema = ;
Now you would have myProp_lt: value
instead of the default myPropLt: value
.
By default the model names are pluralized by adding an s
to the end of the camelized table name. You can set a custom
plural and singular names for the root fields like so:
const graphQlSchema =
onQuery
You can modify the root query by passing an object with onQuery
method as the third argument for graphql
method:
const graphQlSchema = ; expressApp;
setBuilderOptions
Allows you to customize Objection query builder behavior. For instance, you can pass { skipUndefined: true }
as an options argument. So, each time the builder is called, it will be called with skipUndefined enabled.
This can be useful when you use graphql-tools schema stitching.