如何在不创建Mongoose模型的情况下将GraphQL与Mongoose和MongoDB结合使用 [英] How do I use GraphQL with Mongoose and MongoDB without creating Mongoose models

查看:195
本文介绍了如何在不创建Mongoose模型的情况下将GraphQL与Mongoose和MongoDB结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mongoose中创建模型是没有意义的,因为此类模型已经使用GraphQL和现有构造(即TypeScript接口)创建.

Creating models in Mongoose is quite pointless since such models are already created with GraphQL and existing constructs (ie TypeScript interface).

我们如何让GraphQL在GraphQL提供的模型上使用Mongoose的操作,而不必在Mongoose中重新创建模型?

How can we get GraphQL to use Mongoose's operations on models supplied from GraphQL without having to recreate models in Mongoose?

而且,似乎似乎应该为GraphQL提供一个包装程序,使其仅与数据库进行通信,而不必编写MyModel.findById等

Also, it almost seems as if there should be a wrapper for GraphQL that just communicates with the database, avoiding having to write MyModel.findById etc

那是怎么做到的?

Internet上谈论GraphQL和Mongodb的每个示例都使用Mongoose.

Every example on the Internet that talks about GraphQL and Mongodb uses Mongoose.

推荐答案

您应该查看

You should look at GraphQL-to-MongoDB, or how I learned to stop worrying and love generated query APIs. It talks about a middleware package that leverages GraphQL's types to generate your GraphQL API and parses requests sent from clients into MongoDB queries. It more or less skips over Mongoose.

免责声明:这是我的博客文章.

Disclaimer: this is my blog post.

程序包为您的架构字段args生成GraphQL输入类型,并且环绕resolve函数将其解析为MongoDB查询.

The package generates GraphQL input types for your schema field args, and wraps around the resolve function to parse them into MongoDB queries.

给出一个简单的GraphQLType:

Given a simple GraphQLType:

const PersonType = new GraphQLObjectType({
    name: 'PersonType',
    fields: () => ({
        age: { type: GraphQLInt },
        name: {
            type: new GraphQLNonNull(new GraphQLObjectType({
                name: 'NameType',
                fields: () => ({
                    firstName: { type: GraphQLString },
                    lastName: { type: GraphQLString }
                })
            }))
        }
    })
}); 

对于最常见的用例,您将使用getMongoDbQueryResolvergetGraphQLQueryArgs在GraphQL模式中构建一个字段.包装器提供的filterprojectionoptions可以直接传递给find函数.

For the most common use case, you'll build a field in the GraphQL schema with a getMongoDbQueryResolver and getGraphQLQueryArgs. The filter, projection, and options provided by the wrapper can be passed directly to the find function.

person: {
    type: new GraphQLList(PersonType),
    args: getGraphQLQueryArgs(PersonType),
    resolve: getMongoDbQueryResolver(PersonType,
        async (filter, projection, options, source, args, context) =>
            await context.db.collection('person').find(filter, projection, options).toArray()
    )
}

您可以发送到此类字段的查询示例:

An example of a query you could send to such a field:

{
    person (
        filter: {
            age: { GT: 18 },
            name: { 
                firstName: { EQ: "John" } 
            }
        },
        sort: { age: DESC },
        pagination: { limit: 50 }
    ) {
        name { 
            lastName
        }
        age
    }
}

还有一个用于突变字段的包装器和参数类型生成器.

There's also a wrapper and argument types generator for mutation fields.

这篇关于如何在不创建Mongoose模型的情况下将GraphQL与Mongoose和MongoDB结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆