apollostack/graphql-server-如何从解析器获取查询中请求的字段 [英] apollostack/graphql-server - how to get the fields requested in a query from resolver
问题描述
我试图找出一种干净的方法来处理查询和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
}
为了简单起见,我有一个User
和一个email
和一个username
.如果我发送查询但只想检索电子邮件,则可以执行以下操作:
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 } }
但是在解析器中,我的数据库查询仍然会检索username
和email
,但是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()
进行搜索,但这有点脏.据我所知,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 })
一如既往,感谢这个了不起的社区. p>
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
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屋!