在查询中,我可以使用参数的结果来获取该查询中的更多信息吗? [英] In my query, could I use the result of a parameter to get more info in that query?
问题描述
请原谅我措辞不佳的问题,但这是一些代码来说明我要做什么(此查询之外提供了slug
和value
):
const query = `{
post(slug: "${slug}") {
content
createdAt
id <--- I want this id for my reply query
slug
}
reply(replyTo: "id") { <--- The second query in question
content
createdAt
id
slug
}
user(id: "${value}") {
username
}
}`;
我 just 开始使用GraphQL,我喜欢我可以一次查询多个数据库的事实.如果我还可以执行一些查询接收",那就太好了,但是我不确定这是否可能.
考虑GraphQL时,重要的是要记住,给定类型的每个字段都由GraphQL 同时解析. >
例如,当您的post
查询返回Post
类型时,GraphQL将同时解析content
和createdAt
字段.解析完这些字段后,它会转到查询的下一个级别"(例如,如果content
返回的是类型而不是标量,则它将尝试解析这些字段.
每个单独的查询(post
,reply
和user
)实际上都是Root Query类型的字段,并且相同的逻辑也适用于它们.这意味着无法在reply
中引用post
返回的ID –两个查询将同时触发.
以上所述的例外情况是以突变形式存在的,这些突变实际上是顺序解决的,而不是同时解决的.这意味着,即使您仍然无法在reply
查询中将post
的结果用作变量,也可以使用上下文将ID从一个传递到另一个如果两者都是突变.但是,这非常骇人听闻,需要客户端按特定顺序请求突变.
一种更可行的解决方案是简单地在客户端处理此问题,方法是将其分解为两个请求,然后等待触发第二个请求,直到第一个请求返回为止.
最后,您可以考虑重新设计架构,以防止首先要进行多个查询.例如,您的帖子类型可以只包含一个replies
字段,该字段将解析为与返回的帖子的ID对应的所有回复.
Forgive my terribly-worded question but here's some code to explain what I'm trying to do (slug
and value
are provided outside this query):
const query = `{
post(slug: "${slug}") {
content
createdAt
id <--- I want this id for my reply query
slug
}
reply(replyTo: "id") { <--- The second query in question
content
createdAt
id
slug
}
user(id: "${value}") {
username
}
}`;
I just got started with GraphQL and I'm loving the fact that I can query multiple databases in one go. It'd be great if I could also perform some "queryception" but I'm not sure if this is possible.
When thinking in terms of GraphQL, it's important to remember that each field for a given type is resolved by GraphQL simultaneously.
For example, when your post
query returns a Post
type, GraphQL will resolve the content
and createdAt
fields at the same time. Once those fields are resolved, it moved on to the next "level" of the query (for example, if content
returned a type instead of a scalar, it would then try to resolve those fields.
Each of your individual queries (post
, reply
, and user
) are actually fields of the Root Query type, and the same logic applies to them as well. That means there's no way to reference the id returned by post
within reply
-- both queries will be fired off at the same time.
An exception to the above exists in the form of mutations, which are actually resolved sequentially instead of simultaneously. That means, even though you still wouldn't be able to use the result of post
as a variable inside your reply
query, you could use context to pass the id from one to the other if both were mutations. This, however, is very hackish and requires the client to request the mutations in a specific order.
A more viable solution would be to simply handle this on the client side by breaking it up into two requests, and waiting to fire the second until the first one returns.
Lastly, you may consider reworking your schema to prevent having to have multiple queries in the first place. For example, your Post type could simply have a replies
field that would resolve to all replies that correspond with the returned post's id.
这篇关于在查询中,我可以使用参数的结果来获取该查询中的更多信息吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!