Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

feat(samples): adds a Java version of the sample #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<modules>
<module>nodes</module>
<module>samples/java-sample</module>
<module>samples/kotlin-sample</module>
<module>samples/scala-sample</module>
</modules>
Expand Down
40 changes: 40 additions & 0 deletions samples/java-sample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<artifactId>parent</artifactId>
<groupId>io.aexp.nodes.graphql</groupId>
<version>0.5.0</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>java-sample</artifactId>
<name>Java Sample</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nodes</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
49 changes: 49 additions & 0 deletions samples/java-sample/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2018 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;

import io.aexp.nodes.graphql.Argument;
import io.aexp.nodes.graphql.Arguments;
import io.aexp.nodes.graphql.GraphQLRequestEntity;
import io.aexp.nodes.graphql.GraphQLTemplate;
import io.aexp.nodes.graphql.Variable;
import models.User;

public class Main {

// https://developer.github.com/v4/guides/forming-calls/#authenticating-with-graphql
private static final String YOUR_AUTH_TOKEN = "eeb7987aef9ccd7440a49cae2acc7f58bb415059";

public static void main(String[] args) throws Exception {
var headers = new HashMap<String, String>();
headers.put("Authorization", "bearer " + YOUR_AUTH_TOKEN);

var template = new GraphQLTemplate();
var requestEntity = GraphQLRequestEntity.Builder()
.url("https://api.github.com/graphql")
.request(User.class)
.headers(headers)
.arguments(new Arguments("user", new Argument<>("login", "chemdrew")))
.variables(new Variable<Boolean>("isFork", false))
.scalars(BigDecimal.class, BigInteger.class)
.build();
var responseEnitity = template.query(requestEntity, User.class);

System.out.println("request: " + requestEntity.getRequest());
System.out.println("response: " + responseEnitity.getResponse());
}

}
32 changes: 32 additions & 0 deletions samples/java-sample/src/main/java/models/RepositoryConnection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2018 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package models;

public class RepositoryConnection {
private int totalCount;

public int getTotalCount() {
return totalCount;
}

public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}

@Override
public String toString() {
return String.format("RepositoryConnection(totalCount=%d)", totalCount);
}

}
56 changes: 56 additions & 0 deletions samples/java-sample/src/main/java/models/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2018 American Express Travel Related Services Company, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package models;

import io.aexp.nodes.graphql.annotations.GraphQLArgument;
import io.aexp.nodes.graphql.annotations.GraphQLProperty;
import io.aexp.nodes.graphql.annotations.GraphQLVariable;

@GraphQLProperty(name = "user", arguments = { @GraphQLArgument(name = "login") })
public class User {
private String name;
private String location;
@GraphQLVariable(name = "isFork", scalar = "Boolean!")
private RepositoryConnection repositories;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public RepositoryConnection getRepositories() {
return repositories;
}

public void setRepositories(RepositoryConnection repositories) {
this.repositories = repositories;
}

@Override
public String toString() {
return String.format("name=%s, location=%s, repositories=%s", name, location, repositories);
}

}