在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Vincit/objection-graphql开源软件地址(OpenSource Url):https://github.com/Vincit/objection-graphql开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):objection-graphqlAutomatic GraphQL API generator for objection.js models. Usageobjection-graphql automatically generates a GraphQL schema
for objection.js models. The schema is created based on the The following example creates a schema for three models const graphql = require('graphql').graphql;
const graphQlBuilder = require('objection-graphql').builder;
// Objection.js models.
const Movie = require('./models/Movie');
const Person = require('./models/Person');
const Review = require('./models/Review');
// This is all you need to do to generate the schema.
const graphQlSchema = graphQlBuilder()
.model(Movie)
.model(Person)
.model(Review)
.build();
// Or:
// const models = [Movie, Person, Review]
// const graphQlSchema = graphQlBuilder().allModels(models).build();
// Execute a GraphQL query.
graphql(graphQlSchema, `{
movies(nameLike: "%erminato%", range: [0, 2], orderBy: releaseDate) {
name,
releaseDate,
actors(gender: Male, ageLte: 100, orderBy: firstName) {
id
firstName,
age
}
reviews(starsIn: [3, 4, 5], orderByDesc: stars) {
title,
text,
stars,
reviewer {
firstName
}
}
}
}`).then(result => {
console.log(result.data.movies);
}); The example query used some of the many default filter arguments. For example the Getting startedIf 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
Special arguments
Adding your own custom argumentsHere's an example how you could implement a const graphql = require('graphql');
const graphQlSchema = graphQlBuilder()
.model(Movie)
.model(Person)
.model(Review)
.argFactory((fields, modelClass) => {
const args = {};
_.forOwn(fields, (field, propName) => {
// Skip all non primitive fields.
if (field.type instanceof graphql.GraphQLObjectType
|| field.type instanceof graphql.GraphQLList) {
return;
}
args[propName + 'NotEq'] = {
// For our filter the type of the value needs to be
// the same as the type of the field.
type: field.type,
query: (query, value) => {
// query is an objection.js QueryBuilder instance.
query.where(propName, '<>', value);
}
};
});
return args;
})
.build(); Extending your schema with mutationsOften 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 //...
const personType = new GraphQLObjectType({
name: 'PersonType',
description: 'Use this object to create new person',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLInt),
description: 'First Name',
},
firstName: {
type: new GraphQLNonNull(GraphQLString),
description: 'First Name',
},
lastName: {
type: new GraphQLNonNull(GraphQLString),
description: 'Last Name',
},
}),
});
const createPersonInputType = new GraphQLInputObjectType({
name: 'CreatePersonType',
description: 'Person',
fields: () => ({
firstName: {
type: new GraphQLNonNull(GraphQLString),
description: 'First Name',
},
lastName: {
type: new GraphQLNonNull(GraphQLString),
description: 'Last Name',
},
}),
});
const mutationType = new GraphQLObjectType({
name: 'RootMutationType',
description: 'Domain API actions',
fields: () => ({
createPerson: {
description: 'Creates a new person',
type: personType,
args: {
input: { type: new GraphQLNonNull(createPersonInputType) },
},
resolve: (root, inputPerson) => {
const { firstName, lastName } = inputPerson.input;
return {
id: 1,
firstName,
lastName,
};
},
},
}),
});
//Here you can use a GraphQLObjectType or function as an argument for extendWithMutations
schema = mainModule
.builder()
.model(Person)
.extendWithMutations(mutationType)
.build(); Extending your schema with subscriptionsWhen 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 //...
import { PubSub } from 'graphql-subscriptions';
const pubsub = new PubSub();
//...
const personType = new GraphQLObjectType({
name: 'PersonType',
description: 'Person',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLInt),
description: 'First Name',
},
firstName: {
type: new GraphQLNonNull(GraphQLString),
description: 'First Name',
},
lastName: {
type: new GraphQLNonNull(GraphQLString),
description: 'Last Name',
},
}),
});
const subscriptionType = new GraphQLObjectType({
name: 'RootSubscriptionType',
description: 'Domain subscriptions',
fields: () => ({
personCreated: {
description: 'A new person created',
type: personType,
resolve: (payload: any) => payload,
subscribe: () => pubsub.asyncIterator('PERSON_CREATED'),
},
}),
});
//Here you can use a GraphQLObjectType or function as an argument for extendWithSubscriptions
schema = mainModule
.builder()
.model(Person)
.extendWithSubscriptions(subscriptionType)
.build(); MiscdefaultArgNamesYou can change the default filter suffixes and special filter names using the const graphQlSchema = graphQlBuilder()
.model(Movie)
.model(Person)
.model(Review)
.defaultArgNames({
eq: '_eq',
gt: '_gt',
gte: '_gte',
lt: '_lt',
lte: '_lte',
like: '_like',
isNull: '_is_null',
likeNoCase: '_like_no_case',
in: '_in',
notIn: '_not_in',
orderBy: 'order_by',
orderByDesc: 'order_by_desc',
range: 'range',
limit: 'limit',
offset: 'offset'
})
.build(); Now you would have By default the model names are pluralized by adding an const graphQlSchema = graphQlBuilder()
.model(Movie)
.model(Person, {
listFieldName: 'people',
fieldName: 'person'
})
.model(Review) onQueryYou can modify the root query by passing an object with const graphQlSchema = graphQlBuilder()
.model(Movie)
.model(Person)
.model(Review)
.build();
expressApp.get('/graphql', (req, res, next) => {
graphql(graphQlSchema, req.query.graph, {
// builder is an objection.js query builder.
onQuery(builder) {
// You can for example store the the logged in user to builder context
// so that it can be accessed from model hooks.
builder.mergeContext({
user: req.user
});
// Or change the eager fetching algorithm.
builder.eagerAlgorithm(Model.JoinEagerAlgorithm);
}
}).then(result => {
res.send(result);
}).catch(err => {
next(err);
});
}); setBuilderOptionsAllows you to customize Objection query builder behavior. For instance, you can pass |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论