how to send rest request graphql java

https‮w//:‬ww.lautturi.com
how to send rest request graphql java

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:

  1. Add the GraphQL Java dependency to your project. If you are using Maven, you can add the following to your pom.xml file:
<dependency>
  <groupId>com.graphql-java</groupId>
  <artifactId>graphql-java</artifactId>
  <version>11.0</version>
</dependency>
  1. Create a GraphQL client to send the request to your GraphQL server. You can use the GraphQL class from the GraphQL Java library to create a client:
import graphql.GraphQL;

// ...

GraphQL graphQL = GraphQL.newGraphQL(schema).build();
  1. Create a GraphQL query or mutation as a String. For example:
String query = "{ hello }";
  1. Execute the query or mutation using the execute() method of the GraphQL client:
ExecutionResult result = graphQL.execute(query);
  1. If the execution was successful, you can access the data returned by the server using the 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.

Created Time:2017-11-01 20:43:00  Author:lautturi