Test GraphQL Java

A library to convert a GraphQL file to request payload string

License

License

Categories

Categories

Java Languages
GroupId

GroupId

com.vimalselvam
ArtifactId

ArtifactId

test-graphql-java
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Test GraphQL Java
A library to convert a GraphQL file to request payload string
Project URL

Project URL

http://www.vimalselvam.com
Source Code Management

Source Code Management

https://github.com/vimalrajselvam/test-graphql-java

Download test-graphql-java

How to add to project

<!-- https://jarcasting.com/artifacts/com.vimalselvam/test-graphql-java/ -->
<dependency>
    <groupId>com.vimalselvam</groupId>
    <artifactId>test-graphql-java</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.vimalselvam/test-graphql-java/
implementation 'com.vimalselvam:test-graphql-java:1.0.0'
// https://jarcasting.com/artifacts/com.vimalselvam/test-graphql-java/
implementation ("com.vimalselvam:test-graphql-java:1.0.0")
'com.vimalselvam:test-graphql-java:jar:1.0.0'
<dependency org="com.vimalselvam" name="test-graphql-java" rev="1.0.0">
  <artifact name="test-graphql-java" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.vimalselvam', module='test-graphql-java', version='1.0.0')
)
libraryDependencies += "com.vimalselvam" % "test-graphql-java" % "1.0.0"
[com.vimalselvam/test-graphql-java "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.9.9

test (2)

Group / Artifact Type Version
com.squareup.okhttp3 : okhttp jar 4.0.0-alpha02
org.testng : testng jar 6.8

Project Modules

There are no modules declared in this project.

Build Status Maven Central

Test GraphQL Java

This library let's you to convert the GraphQL file to a simple string which can be used as a request payload using any HTTP client library.

I've written this library for the simple GraphQL API Testing. There is a graphql-java library which let's you to implement GraphQL server in Java. My main goal is not to introduce Spring Boot just for the sake of testing the GraphQL API. I wanted to keep it simple!

To use this library, you can download from Maven:

<dependency>
    <groupId>com.vimalselvam</groupId>
    <artifactId>test-graphql-java</artifactId>
    <version>1.0.0</version>
</dependency>

I don't use Gradle, but it should be simple to add this as a gradle dependency.

Getting Started

There are two ways to load the GraphQL file.

  • using InputStream:

    InputStream iStream = getClass().getResourceAsStream("/graphql/pokemon.graphql");

    Here the pokemon.graphql file in under src/test/resources/graphql/pokemon.graphql.

  • using File:

    File file = new File("src/test/resources/graphql/pokemon.graphql");

Once you read the file, just pass it to GraphqlTemplate class to parse as follows:

String graphqlPayload = GraphqlTemplate.parseGraphql(file, variables);

Here the variables is the com.fasterxml.jackson.databind.node.ObjectNode. If no variables, you just pass null.

To build the variables, you can do:

ObjectNode variables = new ObjectMapper().createObjectNode();
variables.put("name", "Pikachu");

Then you can use any HTTP Client and pass the graphqlPayload string as a body.

A sample test can be found here

Versions

Version
1.0.0