Skip to content

Commit c859315

Browse files
committed
Initial code import
0 parents  commit c859315

24 files changed

+1686
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/build
2+
/.gradle
3+
*.iml
4+
*.ipr
5+
*.iws

.travis.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
language: java
2+
jdk:
3+
- oraclejdk8

LICENSE

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Copyright 2016 Yurii Rashkovskii
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and

README.md

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
[![Build Status](https://travis-ci.org/yrashk/graphql-java-annotations.svg?branch=master)](https://travis-ci.org/yrashk/graphql-java-annotations)
2+
[![Bintray](https://img.shields.io/bintray/v/yrashk/maven/graphql-java-annotations.svg)](https://bintray.com/yrashk/graphql-java-annotations)
3+
4+
# GraphQL Annotations for Java
5+
6+
[GraphQL-Java](https://github.com/andimarek/graphql-java) is a great library, but its syntax is a little bit verbose. This library offers an annotations-based
7+
syntax for GraphQL schema definition.
8+
9+
## Getting Started
10+
11+
12+
You can get packages from Bintray:
13+
14+
(Gradle syntax)
15+
16+
```groovy
17+
repositories {
18+
maven {
19+
url "http://dl.bintray.com/yrashk/maven"
20+
}
21+
}
22+
23+
dependencies {
24+
compile 'graphql-java-annotations:graphql-java-annotations:0.1.0'
25+
}
26+
```
27+
28+
29+
## Defining Objects
30+
31+
Any regular Java class can be converted to a GraphQL object type. Fields can
32+
be defined with a `@GraphQLField` (see more on fields below) annotation:
33+
34+
```java
35+
public class SomeObject {
36+
@GraphQLField
37+
public String field;
38+
}
39+
40+
// ...
41+
GraphQLObjectType object = GraphQLAnnotations.object(SomeObject.class);
42+
```
43+
44+
## Defining Interfaces
45+
46+
This is very similar to defining objects:
47+
48+
```java
49+
public interface SomeInterface {
50+
@GraphQLField
51+
String field();
52+
}
53+
54+
// ...
55+
GraphQLInterfaceType object = GraphQLAnnotations.iface(SomeInterface.class);
56+
```
57+
58+
## Fields
59+
60+
In addition to specifying a field over a Java class field, a field can be defined over a method:
61+
62+
```java
63+
public class SomeObject {
64+
@GraphQLField
65+
public String field() {
66+
return "field";
67+
}
68+
}
69+
```
70+
71+
Or a method with arguments:
72+
73+
```java
74+
public class SomeObject {
75+
@GraphQLField
76+
public String field(String value) {
77+
return value;
78+
}
79+
}
80+
```
81+
82+
You can also inject `DataFetchingEnvironment` as an argument, at any position:
83+
84+
```java
85+
public class SomeObject {
86+
@GraphQLField
87+
public String field(DataFetchingEnvironment env, String value) {
88+
return value;
89+
}
90+
}
91+
```
92+
93+
Additionally, `@GraphQLName` can be used to override field name. You can use `@GraphQLDescription` to set a description.
94+
95+
These can also be used for field parameters:
96+
97+
```java
98+
public String field(@GraphQLName("val") String value) {
99+
return value;
100+
}
101+
```
102+
103+
In addition, `@GraphQLDefaultValue` can be used to set a default value to a parameter. Due to limitations of annotations, the default valueu has to be provided by a class that implements `Supplier<Object>`:
104+
105+
```java
106+
public static class DefaultValue implements Supplier<Object> {
107+
@Override
108+
public Object get() {
109+
return "default";
110+
}
111+
}
112+
113+
@GraphQLField
114+
public String field(@GraphQLDefaultValue(DefaultValue.class) String value) {
115+
return value;
116+
}
117+
```
118+
119+
`@GraphQLDeprecate` and Java's `@Deprecated` can be used to specify a deprecated
120+
field.
121+
122+
You can specify a custom data fetcher for a field with `@GraphQLDataFetcher`
123+
124+
## Type Inference
125+
126+
By default, standard GraphQL types (String, Integer, Long, Float, Boolean, Enum, List) will be inferred from Java types. Also, it will respect `@javax.validation.constraints.NotNull` annotation with respect to value's nullability.
127+
128+
If you want to register an additional type (for example, UUID), you have to implement `TypeFunction` for it and register it with `DefaultTypeFunction`:
129+
130+
```java
131+
DefaultTypeFunction.register(UUID.class, new UUIDTypeFunction());
132+
```
133+
134+
You can also specify custom type function for any field with `@GraphQLType` annotation.

build.gradle

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
plugins {
2+
id "com.github.hierynomus.license" version "0.12.1"
3+
id "com.jfrog.bintray" version "1.6"
4+
}
5+
6+
apply plugin: 'java'
7+
apply plugin: 'idea'
8+
9+
apply plugin: 'com.jfrog.bintray'
10+
apply plugin: 'maven'
11+
apply plugin: 'maven-publish'
12+
13+
version = "0.1.0"
14+
15+
idea {
16+
project {
17+
languageLevel = '1.8'
18+
vcs = 'Git'
19+
ipr.withXml { xmlFile ->
20+
// enable 'Annotation Processors'
21+
xmlFile.asNode().component.find {
22+
println(it)
23+
return it.@name == 'CompilerConfiguration'
24+
}['annotationProcessing'][0].replaceNode {
25+
annotationProcessing {
26+
profile(default: true, name: 'Default', useClasspath: 'true', enabled: true)
27+
}
28+
}
29+
}
30+
// TODO: add -parameters to IDEA's javac
31+
}
32+
}
33+
34+
repositories {
35+
jcenter()
36+
}
37+
38+
publishing {
39+
publications {
40+
MyPublication(MavenPublication) {
41+
from components.java
42+
groupId project.name
43+
artifactId project.name
44+
version project.version
45+
}
46+
}
47+
}
48+
49+
bintray {
50+
user = System.getenv('BINTRAY_USER')
51+
key = System.getenv('BINTRAY_KEY')
52+
publications = ['MyPublication']
53+
pkg {
54+
repo = 'maven'
55+
name = project.name
56+
licenses = ['Apache-2.0']
57+
vcsUrl = 'https://github.com/yrashk/graphql-java-annotations'
58+
version {
59+
name = project.version
60+
}
61+
}
62+
}
63+
64+
65+
gradle.projectsEvaluated {
66+
tasks.withType(JavaCompile) {
67+
options.compilerArgs << "-parameters"
68+
}
69+
}
70+
71+
dependencies {
72+
compile 'javax.validation:validation-api:1.1.0.Final'
73+
compile 'com.graphql-java:graphql-java:2.0.0'
74+
75+
// Remove some boilerplate
76+
compile 'org.projectlombok:lombok:1.16.6'
77+
78+
testCompile 'org.testng:testng:6.9.10'
79+
}
80+
81+
test.useTestNG()

gradle/wrapper/gradle-wrapper.jar

50.9 KB
Binary file not shown.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Sat Mar 19 02:04:36 ICT 2016
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-all.zip

0 commit comments

Comments
 (0)