To send a GraphQL request using Java, you can use a library such as Apollo GraphQL or GraphQL Java. Here's an example of how to send a GraphQL request using the GraphQL Java library:
pom.xml file:<dependency> <groupId>com.graphql-java</groupId> <artifactId>graphql-java</artifactId> <version>11.0</version> </dependency>
GraphQL class from the GraphQL Java library to create a client:import graphql.GraphQL; // ... GraphQL graphQL = GraphQL.newGraphQL(schema).build();
String query = "{ hello }";
execute() method of the GraphQL client:ExecutionResult result = graphQL.execute(query);
getData() method of the ExecutionResult object:Map<String, Object> data = result.getData();
System.out.println(data.get("hello"));
If the execution was not successful, you can check for errors using the getErrors() method of the ExecutionResult object.