java 在java中发送GraphQl查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36914918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending GraphQl query in java
提问by HemaSundar
I am new to GraphQL. I know it is very basic question. But tried spending lot of time and i couldn't make it.
我是 GraphQL 的新手。我知道这是非常基本的问题。但是尝试了很多时间,我无法做到。
My requirement is i need to send GraphQL query by using graphql-java api methods from a java class.
我的要求是我需要使用 java 类中的 graphql-java api 方法发送 GraphQL 查询。
Here is the query:
这是查询:
{
contentItem(itemId: 74152479) {
slug
updatedAt
contributors {
id
isFreeForm
name
}
}
}
回答by ch33hau
First, you have to illustrate more on your problem, from your sample query I can't actually see which part you are having problem, it could be in argument, nested object, or data fetcher
首先,您必须更多地说明您的问题,从您的示例查询中,我实际上无法看到您遇到问题的部分,它可能在参数、嵌套对象或数据提取器中
I'm new to GraphQL(java) as well, instead of sharing the direct answer with you, I intended to show you how I resolve the similar problem.
我也是 GraphQL(java) 的新手,而不是与您分享直接答案,我打算向您展示我如何解决类似问题。
graphql-javaactually did a great job in their test cases. You can refer here: https://github.com/andimarek/graphql-java/tree/master/src/test/groovy/graphqlto get some ideas on how to create and query GraphQL schema.
graphql-java实际上在他们的测试用例中做得很好。您可以参考这里:https: //github.com/andimarek/graphql-java/tree/master/src/test/groovy/graphql以获取有关如何创建和查询 GraphQL 模式的一些想法。
Arguments
参数
I found a similar case like yours in here: https://github.com/andimarek/graphql-java/blob/master/src/test/groovy/graphql/StarWarsSchema.java#L131
我在这里发现了一个类似的案例:https: //github.com/andimarek/graphql-java/blob/master/src/test/groovy/graphql/StarWarsSchema.java#L131
newFieldDefinition()
.name("human")
.type(humanType)
.argument(newArgument()
.name("id")
.description("id of the human")
.type(new GraphQLNonNull(GraphQLString))
.build())
.dataFetcher(StarWarsData.getHumanDataFetcher())
.build())
In this case, only one argument is defined, which is id. new GraphQLNonNull(GraphQLString)
tells us this is is a mandatory string argument.
在这种情况下,只定义了一个参数,即id。new GraphQLNonNull(GraphQLString)
告诉我们这是一个强制性的字符串参数。
Fields
字段
For fields, it is defining in humanType
, you can refer to https://github.com/andimarek/graphql-java/blob/master/src/test/groovy/graphql/StarWarsSchema.java#L51.
Nested fields is just another type with some fields, eg, .type(nestedHumanType)
对于字段,定义在 中humanType
,可以参考https://github.com/andimarek/graphql-java/blob/master/src/test/groovy/graphql/StarWarsSchema.java#L51。嵌套字段只是具有某些字段的另一种类型,例如,.type(nestedHumanType)
Data Fetcher
数据提取器
After all, you might to process the argument idand return some data. You can refer to the example here: https://github.com/andimarek/graphql-java/blob/master/src/test/groovy/graphql/StarWarsData.groovy#L84
毕竟,您可能会处理参数id并返回一些数据。你可以参考这里的例子:https: //github.com/andimarek/graphql-java/blob/master/src/test/groovy/graphql/StarWarsData.groovy#L84
To make my code looks cleaner, normally I will create a separate class for DataFetcher, eg:
为了让我的代码看起来更简洁,通常我会为 DataFetcher 创建一个单独的类,例如:
public class HumanDataFetcher implements DataFetcher {
@Override
public Object get(DataFetchingEnvironment environment) {
String id = (String)environment.get("id");
// Your code here
}
}
Hope this helps.
希望这可以帮助。
回答by anhldbk
I've got a solution which is implemented in vertx-graphql-client.
我有一个在vertx-graphql-client 中实现的解决方案。
The process to universally make a GraphQL query is:
通用进行 GraphQL 查询的过程是:
- Rewriting your query using variables
- 使用变量重写查询
So your query may look like:
因此,您的查询可能如下所示:
query contentItem($itemId: Int){
contentItem(itemId: $itemId) {
slug
updatedAt
contributors {
id
isFreeForm
name
}
}
}
Send your query via HTTP POST requests with
header
: setcontent-type
toapplication/json
body
: the body is set by serializing following JSON data:
通过 HTTP POST 请求发送您的查询
header
: 设置content-type
为application/json
body
:主体是通过序列化以下 JSON 数据来设置的:
{
"query": "the-templated-query-above",
"operationName": "contentItem",
"variables": {
"itemId": 74152479
}
}
With curl, it's easy as:
使用 curl,它很容易:
curl \
-X POST \
-H "Content-Type: application/json" \
--data '{ "query": "the-templated-query-above", "operationName": "contentItem", "variables": { "itemId": 74152479 }}' \
http://www.yoursite.com/your/graphql/api
回答by yantaq
Firstly, make sure that you can get result by curling like so. if this works (make sure to change the verb to GETdepending on what verb server expects) then all you need to do is send the query part in http request body with content-type as application/json.
首先,确保你可以像这样卷曲得到结果。如果这有效(确保根据动词服务器的期望将动词更改为GET),那么您需要做的就是在 http 请求正文中发送查询部分,内容类型为 application/json。
curl \
-X POST \
-H "Content-Type: application/json" \
--data '{ "query": "{ contentItem(itemId: \"74152479\") { slug updatedAt contributors { id isFreeForm name } } }" }' \
http://www.yoursite.com/your/graphql/api
once successful you can build the query and send the request using Java http clients. I have successfully done so using Jersey client. only challenge is building the query if you want to have some generic query builder. but there are query builder on github like this ONE, and you can tweak it to suit your needs.
一旦成功,您就可以构建查询并使用 Java http 客户端发送请求。我使用 Jersey 客户端成功地做到了这一点。如果您想要一些通用查询构建器,唯一的挑战是构建查询。但是 github 上有像这样的查询生成器ONE,您可以对其进行调整以满足您的需求。
All in all, following is what you need to send in http body. I put his in one liner,which is exactly how the request body looks like. you have to remove any extra format like new line etc.
总而言之,以下是您需要在 http 正文中发送的内容。我把他放在一个班轮里,这正是请求正文的样子。你必须删除任何额外的格式,比如换行等。
{ "query": "{ contentItem(itemId: \"74152479\") { slug updatedAt contributors { id isFreeForm name } } }" }
回答by saif ali
You can make use of the
您可以使用
environment.getFieldDefinition().getName().equals(expected_query_name)
This will get the query name and you can distinguish between queries
这将获得查询名称,您可以区分查询
回答by etienne-sf
You can check the graphql-maven-plugin-project, which generates all the necessary code to make it easy to execute GraphQL requests in Java.
您可以查看 graphql-maven-plugin-project,它会生成所有必要的代码,以便在 Java 中轻松执行 GraphQL 请求。
It's available at https://github.com/graphql-java-generator/graphql-maven-plugin-project. It's web site is https://graphql-maven-plugin-project.graphql-java-generator.com
它可以在https://github.com/graphql-java-generator/graphql-maven-plugin-project 上找到。它的网站是https://graphql-maven-plugin-project.graphql-java-generator.com
Etienne
艾蒂安