OS-Guild/graphql-to-mongodb
소개
기존 GraphQL 타입에 사용할 범용 필터 타입을 런타임에 생성하고, 클라이언트 요청을 MongoDB find 쿼리로 파싱합니다.
개발 도구측정된 스타 증가
- in 24h
- 0
- in 7 days
- 0
- in 15 days
- 0
- in 1 month
- 기록 수집 중
서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.
README.md
Originalgraphql-to-mongodb
If you want to grant your NodeJS GraphQL service a whole lot of the power of the MongoDB database standing behind it with very little hassle, you've come to the right place!
Examples
Change Log
Blog Post
Let's take a look at the most common use case, getMongoDbQueryResolver and getGraphQLQueryArgs:
Given a simple GraphQL type:
new GraphQLObjectType({
name: 'PersonType',
fields: () => ({
age: { type: GraphQLInt },
name: { type: new GraphQLObjectType({
name: 'NameType',
fields: () => ({
first: { type: GraphQLString },
last: { type: GraphQLString }
})
}),
fullName: {
type: GraphQLString,
resolve: (obj, args, { db }) => `${obj.name.first} ${obj.name.last}`
}
})
})
An example GraphQL query supported by the package:
Queries the first 50 people, oldest first, over the age of 18, and whose first name is John.
{
people (
filter: {
age: { GT: 18 },
name: {
first: { EQ: "John" }
}
},
sort: { age: DESC },
pagination: { limit: 50 }
) {
fullName
age
}
}
To implement, we'll define the people query field in our GraphQL scheme like so:
people: {
type: new GraphQLList(PersonType),
args: getGraphQLQueryArgs(PersonType),
resolve: getMongoDbQueryResolver(PersonType,
async (filter, projection, options, obj, args, context) => {
return await context.db.collection('people').find(filter, projection, options).toArray();
})
}
You'll notice that integrating the package takes little more than adding some fancy middleware over the resolve function. The filter, projection, options added as the first parameters of the callback, can be sent directly to the MongoDB find function as shown. The rest of the parameter are the standard received from the GraphQL api.
- Additionally, resolve fields' dependencies should be defined in the GraphQL type like so:
This is needed to ensure that the projection does not omit any necessary fields. Alternatively, if throughput is of no concern, the projection can be replaced with an empty object.fullName: { type: GraphQLString, resolve: (obj, args, { db }) => `${obj.name.first} ${obj.name.last}`, dependencies: ['name'] // or ['name.first', 'name.Last'], whatever tickles your fancy } - As of
mongodbpackage version 3.0, you should implement the resolve callback as:return await context.db.collection('people').find(filter, options).toArray();
That's it!
The following field is added to the schema (copied from graphiQl):
people(
filter: PersonFilterType
sort: PersonSortType
pagination: GraphQLPaginationType
): [PersonType]
PersonFilterType:
age: IntFilter
name: NameObjectFilterType
OR: [PersonFilterType]
AND: [PersonFilterType]
NOR: [PersonFilterType]
* Filtering is possible over every none resolve field!
NameObjectFilterType:
first: StringFilter
last: StringFilter
opr: OprExists
OprExists enum type can be EXISTS or NOT_EXISTS, and can be found in nested objects and arrays
StringFilter:
EQ: String
GT: String
GTE: String
IN: [String]
LT: String
LTE: String
NEQ: String
NIN: [String]
NOT: [StringFNotilter]
PersonSortType:
age: SortType
SortType enum can be either ASC or DESC
GraphQLPaginationType:
limit: Int
skip: Int
Functionality galore! Also permits update, insert, and extensiable custom fields.
관련 프로젝트 살펴보기
불러오는 중…