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!
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}`
}
})
})
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 paraneters of the callback, can be sent directly to the MongoDB find function as shown. The rest of the parameter are the standard recieved from the GraphQL api.
fullName: {
type: GraphQLString,
resolve: (obj, args, { db }) => `${obj.name.first} ${obj.name.last}`,
dependencies: ['name'] // or ['name.first', 'name.Last'], whatever tickles your fancy
}
mongodb
package version 3.0, you should implement the resolve callback as:
return await context.db.collection('people').find(filter, options).toArray();
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 tyoe 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
如果你想了解NoSQL DB和GraphQL,推荐一个非常好的资料。依照文档里边的步骤,在Atlas上免费搭建一个使用到MongoDB和GraphQL的web app项目。详细的搭建步骤来自MongoDB developer center。内容非常详尽: GraphQL: The Easy Way to Do the Hard Stuff 拥有真正的MongoDB和和GraphQL
怎么说, 用python简直是一种享受. 以为python提供了神奇的魔术方法, 基于这些方法写出的框架非常”智能”. 代码冗余少, 实现优雅 ! 本篇文章将简述如何用python提供mongodb+GraphQL的基本服务. mongoengine MongoEngine is a Document-Object Mapper (think ORM, but for document datab
MongoDB Logo ServerDriversCloudToolsGuides Get MongoDB Close × MongoDB Stitch Introduction Tutorials Overview Basic Blog Tutorial Blog App Overview Build the Blog Backend
MongoDB Logo ServerDriversCloudToolsGuides Get MongoDB Close × MongoDB Stitch Introduction Tutorials Users & Authentication MongoDB Atlas GraphQL GraphQL API Overview Expose Data in a Collecti
MongoDB Logo ServerDriversCloudToolsGuides Get MongoDB Close × MongoDB Stitch Introduction Tutorials Users & Authentication MongoDB Atlas Overview Configure MongoDB Link a MongoDB Atla
2017-07-08 03:45:08 1 I´m trying to store a UNIX timestamp in MongoDB using GraphQL, but it seens that GraphQL has a limit to handle integers. See the mutation below: const addUser = { type: UserType,
MongoDB Logo ServerDriversCloudToolsGuides Get MongoDB Close × MongoDB Stitch Introduction Tutorials Overview Basic Blog Tutorial Blog App Overview Build the Blog Backend
Swagger-to-GraphQL Swagger-to-GraphQL converts your existing Swagger schema to an executableGraphQL schema where resolvers perform HTTP calls to certain real endpoints. Itallows you to move your API t
OpenAPI-to-GraphQL Translate APIs described by OpenAPI Specifications (OAS) or Swagger into GraphQL. Getting started OpenAPI-to-GraphQL can be used in two ways: CLI The Command Line Interface (CLI) pr
Intro to GraphQL Scott Moss & Frontend Masters Resources Slides Nodejs MongoDB Apollo GraphQL Course This course has two parts, slides and excercises. The slides describe the excerices in detail. Each
快速开始 GraphQL 是一种用于 API 的查询语言。这是 GraphQL 和 REST 之间一个很好的比较 (译者注: GraphQL 替代 REST 是必然趋势)。在这组文章中, 我们不会解释什幺是 GraphQL, 而是演示如何使用 @nestjs/GraphQL 模块。 GraphQLModule 只不过是 Apollo 服务器的包装器。我们没有造轮子, 而是提供一个现成的模块, 这让
GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时。 GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地获得它需要的数据,而且没有任何冗余,也让 API 更容易地随着时间推移而演进,还能用于构建强大的开发者工具。 向你的 API 发出一个 GraphQL 请求就能准确获得你想要的数据,不多不少。 GraphQL 查询总是返回可预测
koa-graphql-mongodb is being sponsored by the following tool; please help to support us by taking a look and signing up to a free trial. koa-graphql-mongodb Tutorial how to set up koa with graphql and mongodb