apollostack/graphql-server - 如何从解析器获取查询中请求的字段 [英] apollostack/graphql-server - how to get the fields requested in a query from resolver

查看:40
本文介绍了apollostack/graphql-server - 如何从解析器获取查询中请求的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一种干净的方式来处理查询和 mongdb 投影,这样我就不必从数据库中检索过多的信息.所以假设我有:

I am trying to figure out a clean way to work with queries and mongdb projections so I don't have to retrieve excessive information from the database. So assuming I have:

// the query
type Query {
  getUserByEmail(email: String!): User
}

我有一个带有 emailusernameUser,以保持简单.如果我发送查询并且只想检索电子邮件,我可以执行以下操作:

And I have a User with an email and a username, to keep things simple. If I send a query and I only want to retrieve the email, I can do the following:

query { getUserByEmail(email: "test@test.com") { email } }

但在解析器中,我的数据库查询仍然检索usernameemail,但只有其中之一作为查询结果被apollo 服务器传回.

But in the resolver, my DB query still retrieves both username and email, but only one of those is passed back by apollo server as the query result.

我只希望数据库检索查询要求的内容:

I only want the DB to retrieve what the query asks for:

// the resolver
getUserByEmail(root, args, context, info) {
  // check what fields the query requested
  // create a projection to only request those fields
  return db.collection('users').findOne({ email: args.email }, { /* projection */ });
}

当然,问题是,获取有关客户请求的信息并不是那么简单.

Of course the problem is, getting information on what the client is requesting isn't so straightforward.

假设我将请求作为上下文传递 - 我考虑使用具有查询字符串的 context.payload (hapi.js),并通过各种 .split() 进行搜索code>s,但这感觉有点脏.据我所知, info.fieldASTs[0].selectionSet.selections 有字段列表,我可以检查它是否存在.我不确定这有多可靠.尤其是当我开始使用更复杂的查询时.

Assuming I pass in request as context - I considered using context.payload (hapi.js), which has the query string, and searching it through various .split()s, but that feels kind of dirty. As far as I can tell, info.fieldASTs[0].selectionSet.selections has the list of fields, and I could check for it's existence in there. I'm not sure how reliable this is. Especially when I start using more complex queries.

有没有更简单的方法?

如果您不使用 mongDB,则投影是您传入的附加参数,明确告诉它要检索什么:

In case you don't use mongDB, a projection is an additional argument you pass in telling it explicitly what to retrieve:

// telling mongoDB to not retrieve _id
db.collection('users').findOne({ email: 'test@test.com' }, { _id: 0 })

一如既往,感谢出色的社区.

As always, thanks to the amazing community.

推荐答案

当然可以.这实际上与在基于 SQL 的数据库的 join-monster 包上实现的功能相同.他们的创作者有一段演讲:https://www.youtube.com/watch?v=Y7AdMIuXOgs

Sure you can. This is actually the same functionality that is implemented on join-monster package for SQL based db's. There's a talk by their creator: https://www.youtube.com/watch?v=Y7AdMIuXOgs

查看他们的 info 分析代码以帮助您入门 - https://github.com/stems/join-monster/blob/master/src/queryASTToSqlAST.js#L6-L30

Take a look on their info analysing code to get you started - https://github.com/stems/join-monster/blob/master/src/queryASTToSqlAST.js#L6-L30

很想为我们的 mongo 用户看到一个投影怪物包:)

Would love to see a projection-monster package for us mongo users :)

更新:有一个包可以从 npm 上的 info 创建一个投影对象:https://www.npmjs.com/package/graphql-mongodb-projection

UPDATE: There is a package that creates a projection object from info on npm: https://www.npmjs.com/package/graphql-mongodb-projection

这篇关于apollostack/graphql-server - 如何从解析器获取查询中请求的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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