enigmatis / graphql-java-annotations Goto Github PK
View Code? Open in Web Editor NEWGraphQL Annotations for Java
License: Other
GraphQL Annotations for Java
License: Other
I've noticed that GraphQL will, for fields starting with "is" (e.g. isAvailable), set the DataFetcher to FieldDataFetcher as opposed to PropertyDataFetcher.
Native boolean fields starting by "isXxxx" are allowed to have a getter method of the same name as defined in the Java Beans spec section 8.3.2:
public boolean is<PropertyName>();
The same is not necessarily true for Boolean object types.
Although it is discussible if such fields should be using FieldDataFetcher by default, the graphql-java-annotations should allow graphql fields to be easily overwritten by another DataFetcher basic type.
The current case generates a FieldDataFetcher by default:
@ApiModelProperty(value = "")
@GraphQLField
var isAvailable: Boolean? = null
But instead I would like it to use the PropertyDataFetcher:
@ApiModelProperty(value = "")
@GraphQLField
@GraphQLDataFetcher(PropertyDataFetcher::class)
var isAvailable: Boolean? = null
And this code fails to compile because PropertyDataFetcher annotation does not have a public default constructor. To get around it I had to specialize the PropertyDataFetcher for that specific field and it can become quite repetitive having to do the same for other similar fields.
Instead, it would be nice to allow to pass argument for a given specialized constructor as in:
@GraphQLDataFetcher(value=PropertyDataFetcher::class, args="isAvailable")
public Boolean isAvailable = null
"C:\Program Files\Java\jdk1.8.0_25\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:49887,suspend=y,server=n -ea -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.3.1\lib\idea_rt.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.3.1\plugins\testng\lib\testng-plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_25\jre\lib\rt.jar;C:\shishuwu\Saturn\jason\graphql\graphqljavaannotationsample\target\test-classes;C:\shishuwu\Saturn\jason\graphql\graphqljavaannotationsample\target\classes;C:\Users\shishu.AUTH.m2\repository\com\graphql-java\graphql-java-annotations\0.13.2\graphql-java-annotations-0.13.2.jar;C:\Users\shishu.AUTH.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;C:\Users\shishu.AUTH.m2\repository\com\graphql-java\graphql-java\2.2.0\graphql-java-2.2.0.jar;C:\Users\shishu.AUTH.m2\repository\org\antlr\antlr4-runtime\4.5.1\antlr4-runtime-4.5.1.jar;C:\Users\shishu.AUTH.m2\repository\org\slf4j\slf4j-api\1.7.12\slf4j-api-1.7.12.jar;C:\Users\shishu.AUTH.m2\repository\org\antlr\antlr4\4.5.1\antlr4-4.5.1.jar;C:\Users\shishu.AUTH.m2\repository\org\testng\testng\6.9.10\testng-6.9.10.jar;C:\Users\shishu.AUTH.m2\repository\com\beust\jcommander\1.48\jcommander-1.48.jar;C:\Users\shishu.AUTH.m2\repository\org\beanshell\bsh\2.0b4\bsh-2.0b4.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 2016.3.1\plugins\testng\lib\jcommander.jar" org.testng.RemoteTestNGStarter -usedefaultlisteners false -socket49886 @w@C:\Users\shishu.AUTH\AppData\Local\Temp\idea_working_dirs_testng.tmp -temp C:\Users\shishu.AUTH\AppData\Local\Temp\idea_testng.tmp
Connected to the target VM, address: '127.0.0.1:49887', transport: 'socket'
[TestNG] Running:
C:\Users\shishu.AUTH.IdeaIC2016.3\system\temp-testng-customsuite.xml
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
java.lang.AssertionError: expected [true] but found [false]
Expected :true
Actual :false
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertTrue(Assert.java:42)
at org.testng.Assert.assertTrue(Assert.java:52)
at graphql.annotations.GraphQLObjectTest.defaultArg(GraphQLObjectTest.java:474)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
java.lang.AssertionError: expected [a] but found [arg0]
Expected :a
Actual :arg0
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertEqualsImpl(Assert.java:135)
at org.testng.Assert.assertEquals(Assert.java:116)
at org.testng.Assert.assertEquals(Assert.java:190)
at org.testng.Assert.assertEquals(Assert.java:200)
at graphql.annotations.GraphQLObjectTest.fields(GraphQLObjectTest.java:189)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
java.lang.NullPointerException
at graphql.annotations.GraphQLObjectTest.inputObjectArgument(GraphQLObjectTest.java:571)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
java.lang.AssertionError: expected [true] but found [false]
Expected :true
Actual :false
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.failNotEquals(Assert.java:513)
at org.testng.Assert.assertTrue(Assert.java:42)
at org.testng.Assert.assertTrue(Assert.java:52)
at graphql.annotations.GraphQLObjectTest.query(GraphQLObjectTest.java:436)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
java.lang.AssertionError: expected object to not be null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.assertNotNull(Assert.java:423)
at org.testng.Assert.assertNotNull(Assert.java:408)
at graphql.annotations.RelayTest.argMutation(RelayTest.java:174)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
java.lang.AssertionError: expected object to not be null
at org.testng.Assert.fail(Assert.java:94)
at org.testng.Assert.assertNotNull(Assert.java:423)
at org.testng.Assert.assertNotNull(Assert.java:408)
at graphql.annotations.RelayTest.argVariableMutation(RelayTest.java:212)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
Disconnected from the target VM, address: '127.0.0.1:49887', transport: 'socket'
Default Suite
Total tests run: 61, Failures: 6, Skips: 0
Process finished with exit code 0
I have a simple schema with two object types: Task, and User. A Task references a list of Users and a User references a list of Tasks. Both these associations are marked with @GraphQLConnection. When the schema is built at runtime, only one of these ends up as a connection, and the other ends up as a GraphQLList.
When I construct the schema manually (not using annotations) with GraphQLTypeReference as both edge types, the result is correct.
I've attached a simple test case and my schema code as an example.
When using an object as field argument, an IllegalArgumentException
will be thrown at execution.
Example:
public class GraphQLTest {
@GraphQLName("TestObjectInputArgument")
public static class TestObjectInputArgument {
@GraphQLField public String a;
@GraphQLField public int b;
}
public static class TestObjectInput {
@GraphQLField
public TestObjectInputArgument test(@GraphQLName("args") TestObjectInputArgument args) {
return args;
}
}
public static void main(String[] args) throws Exception {
GraphQLSchema schema =
GraphQLSchema.newSchema().query(GraphQLAnnotations.object(TestObjectInput.class)).build();
GraphQL graphql2 = new GraphQL(schema);
ExecutionResult result2 =
graphql2.execute("{ test(args:{ a:\"x\", b:1 }) { a } }", new TestObjectInput());
}
}
The reason is that MethodDataFetcher
will send a HashMap
instead of a TestObjectInputArgument
as the first argument of test
.
One workaround for this to use @GraphQLDataFetcher
and provide a custom data fetcher. However, I think that to make the API more terse, MethodDataFetcher
should be able to handle this. One way to do it is to have a convention of making input object provide a constructor that could take a HashMap
as argument and it's able to create the proper argument for the method.
If you think this is a good idea I could provide a PR for this.
The reflection exceptions are re-thrown in the API to create Graph objects
try {
GraphQLObjectType IssueFieldQL = GraphQLAnnotations.object(IssueField.class);
} catch (IllegalAccessException | NoSuchMethodException | InstantiationException e) {
throw new RuntimeException(e);
}
This clutters the usage of the annotations code for no great gain. Even your own tests use the @SneakyThrows to get around the annoying nature of this signature.
I suggest it would better to have GraphQLAnnotationsReflectionException as a runtime exception in the case where consumers provide a bad class
or is it 0.13.3 or 0.14.0?
A graphql query which uses fragments fails:
Example: ({items { ... on MyObject {a, my {b}} ... on MyObject2 {a, b} }}
The return type of getItems is an interface.
MyObject and MyObject2 are two different implementations of that interface.
I've created two demo classes (plain, annotations) which demonstrates the problem:
https://github.com/mattesja/graphql-helloworld
HelloWorldPlain.java shows how the fragment query works in plain graphql-java.
HelloWorldAnnotation.java shows how the fragment query fails with graphql-java-annotations.
Hints
It might be a dumb question but it is not clear to me whether GraphQLID
is supported or not.
I see it defined in graphql-java here I'm not sure how it use since I use only annotations ;-)
Does it make sense to have a GraphQLID
annotation ? Something that I could use it like:
public class LogBatch {
private final String id;
public LogBatch(final String id) {
this.id = id;
}
@GraphQLID
public String getId() {
return this.id;
}
}
I cant find any example how i can add an argument with graphql-annotations?
can anybody help ?
Hello,
Thanks for your great work! Using this library has definitely made working with GraphQL in Java a pleasure.
I have the following mutation:
@GraphQLField
@GraphQLName("createPart")
@GraphQLRelayMutation
@GraphQLDataFetcher(CreatePartDataFetcher.class)
public static CreatePartPayload createPart(@GraphQLName("name")
@GraphQLNonNull String name,
@GraphQLName("description")
@GraphQLNonNull String description) {
return null;
}
I have the following data fetcher:
@Service
@Log4j2
public class CreatePartDataFetcher extends BaseDataFetcher {
@Override
public CreatePartPayload get(final DataFetchingEnvironment environment) {
log.debug("Creating part");
final Object input = environment.getArgument("input");
final Part part = mapper.map(input, Part.class);
final PartService partService = applicationContext.getBean(PartService.class);
partService.save(part);
return new CreatePartPayload(mapper.map(part, PartType.class));
}
}
The data fetcher is not called....
I have used the same implementation for queries and the data fetcher is successfully called. What am I doing wrong?
EDIT: It would be nice to have an annotation called @GraphQLDataIngester for this purpose.
EDIT: Looking at this code it appears it is not possible to specify a custom data fetcher. :( I use spring, so I can't inject a service into the type as normally would. I have to use this data fetcher work around to get access to the spring environment:
public abstract class BaseDataFetcher implements DataFetcher, ApplicationContextAware {
protected static Mapper mapper = new DozerBeanMapper();
protected static ApplicationContext applicationContext;
@Override
public void setApplicationContext(final ApplicationContext context) {
applicationContext = context;
}
}
Hi there,
First, thanks for this wonderful library!
I noticed this while generating schema dumps for Relay. The ordering of methods and fields returned by Class.getMethods and Class.getFields is undefined, which ends up causing GraphQL Annotations to generate schemas with arbitrary field orderings.
This unfortunately ends up making schema dumps look like they were modified after each execution. I was wondering if perhaps it would be preferable to sort the methods by name / signature prior to generating graphql fields so that the ordering remains stable. Thoughts?
Could this project consider NOT use Lombok in the implementation of the library
Good libraries tend to have a limited set of dependencies.
Lombok is one of the most contentious dependencies out there because of its "magic" nature.
We have an informal ban on it inside my area in Atlassian because of debugging problems at runtime and the byte code weaving side of it at compile time.
I would ask that this otherwise excellent library not use Lombok in its implementation. I think this will increase its uptake, not just in my company but with others.
I am in the process of creating a PR to see what the impact of removing Lombok would be.
If I want to have a field on an object that takes an Array of Strings as an argument, how do I do that with annotations?
The code seems to ignore all parameterized types, so List<String>
doesn't work.
I'm a little bit confused about how you're meant to implement a stateful data fetcher. For example, suppose I declare a data fetcher method that requires interrogating a Spring JPA repository as part of its implementation. How would I go about fetching the repository, apart from exposing it in a public global accessor? Is it possible to "wire in" non GraphQL parameters on the data fetcher method that I can specify at schema creation time?
For example:
...
@GraphQLField
public String field(DataFetchingEnvironment env, MyJpaRepository repo, String value) {
return repo.getByField(value);
}
...
Hi, first I wanna thank you all for the library, it's quite cool and makes working with GraphQL a way better experience.
I've noticed that though there was quite a decent amount of activity and commits during the last months, the last version is from Nov 2016. Is there any reason you didn't decide to release a new version just yet?
I am particularly interested in latest changes that add args to the DataFetcher annotation. And though I could potentially use latest commits in order to gain that support, given our current configuration could be kind of a hassle (and maybe even impossible to not be attached to a release - which kind of makes sense sometimes - )
Thanks!
Hello.
I'm not completely sure if this is an issue of graphql-java-annotations
or graphql-java
. I am cross posting the issue just in case. I apologize if this causes inconveniences.
I'm having the error described in the title randomly with this simple recursive schema:
public class Building {
private String name;
public Building(String name) {
this.name = name;
}
@GraphQLField
@GraphQLName("name")
public String getName() {
return name;
}
@GraphQLField
@GraphQLName("apartments")
public List<Apartment> getApartments() {
List<Apartment> apartments = new ArrayList();
apartments.add(new Apartment(this, "one", 10));
apartments.add(new Apartment(this, "two", 20));
apartments.add(new Apartment(this, "three", 30));
return apartments;
}
}
public class Apartment {
private String name;
private Integer size;
private Building building;
public Apartment(Building building, String name, Integer size) {
this.building = building;
this.name = name;
this.size = size;
this.building = building;
}
@GraphQLField
@GraphQLName("building")
public Building getBuilding() {
return building;
}
@GraphQLField
@GraphQLName("size")
public Integer getSize() {
return size;
}
@GraphQLField
@GraphQLName("name")
public String getName() {
return name;
}
}
public class Query {
@GraphQLField
public static Building building() {
return new Building("Building");
}
@GraphQLField
public static Apartment apartment() {
return new Apartment(new Building("New building"), "New apartment", 20);
}
}
I'm running it in this way:
public static void main(String[] args) throws Exception {
GraphQLObjectType annotationsType = GraphQLAnnotations.object(Query.class);
GraphQLSchema schema = GraphQLSchema.newSchema()
.query(annotationsType)
.build();
Object result = new GraphQL(schema).execute("{ building { apartments { name, building { name } } } }").getData();
System.out.println(result);
}
The problem I have is that the test here runs okay 9 out of 10 times aprox. However, every once in a while, I get the following exception:
Exception in thread "main" java.lang.ClassCastException: graphql.schema.GraphQLTypeReference cannot be cast to graphql.schema.GraphQLUnmodifiedType
at graphql.schema.SchemaUtil.getUnmodifiedType(SchemaUtil.java:30)
at graphql.schema.SchemaUtil.isLeafType(SchemaUtil.java:12)
at graphql.validation.rules.ScalarLeafs.checkField(ScalarLeafs.java:21)
at graphql.validation.RulesVisitor.checkField(RulesVisitor.java:92)
at graphql.validation.RulesVisitor.enter(RulesVisitor.java:51)
at graphql.validation.LanguageTraversal.traverseImpl(LanguageTraversal.java:19)
at graphql.validation.LanguageTraversal.traverseImpl(LanguageTraversal.java:22)
at graphql.validation.LanguageTraversal.traverseImpl(LanguageTraversal.java:22)
at graphql.validation.LanguageTraversal.traverseImpl(LanguageTraversal.java:22)
at graphql.validation.LanguageTraversal.traverseImpl(LanguageTraversal.java:22)
at graphql.validation.LanguageTraversal.traverseImpl(LanguageTraversal.java:22)
at graphql.validation.LanguageTraversal.traverseImpl(LanguageTraversal.java:22)
at graphql.validation.LanguageTraversal.traverseImpl(LanguageTraversal.java:22)
at graphql.validation.LanguageTraversal.traverse(LanguageTraversal.java:14)
at graphql.validation.Validator.validateDocument(Validator.java:20)
at graphql.GraphQL.execute(GraphQL.java:73)
at graphql.GraphQL.execute(GraphQL.java:55)
at graphql.GraphQL.execute(GraphQL.java:47)
at graphql.GraphQL.execute(GraphQL.java:43)
at GraphQLTest.main(GraphQLTest.java:63)
I believe this has to do with the order in which graphql-java-annotations
processes the annotations of the Query
class. When I print schema.getAllTypesAsList()
I noticed that every time the error happens, the Apartment
type appears before the Building
type.
I am using the following versions:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java</artifactId>
<version>2016-08-21T01-09-05</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-annotations</artifactId>
<version>0.9.0</version>
</dependency>
Sorry for not having a concrete test case, but as I said before, this is something that happens randomly. Please let me know if you need more details.
I'm using Spring & graphql-java (graphql-java-annotation) in my project. For retrieving data part, i'm using a DataFetcher to get data from a service bean (from database).
The weird thing is that: myService is always null.
More detailed information, please refer to my question and the workaround on stackoverflow.
Only direct implemented interfaces are supported when building a GraphQLObjectType.
class MyObject implements MyInterface {} => OK
class MyObject2 extends MyObject {} => No interface in ObjectType
ClassUtils.getAllInterfaces(object) should be used instead of object.getInterfaces()
Would you accept such a pull request adding such a dependency on apache-commons-lang?
Is there a reason why graphql-java is still using version 2.2.0 ?
Why not using 2.3.0 ?
I'm trying to port over some code written in vanilla graphql-java to start using annotations.
I've got code that looks like this:
@GraphQLName("VersionType")
@GraphQLDescription("VersionType")
private static class Version {
@GraphQLField
public @NotNull String version() {
return "1.0.0";
}
}
@SneakyThrows
private static GraphQLObjectType getVersionType() {
return GraphQLAnnotations.object(Version.class);
}
private static GraphQLObjectType queryType = newObject()
.name("Query")
.field(newFieldDefinition()
.name("Version")
.type(GraphQLString)
.staticValue("1.0.0")
.build()
)
...
.build()
protected static GraphQLSchema schema = newSchema()
.query(queryType)
.mutation(mutationType)
.build();
public static GraphQL graphQL = new GraphQL(schema);
Now, the object builds correctly and the schema for the object type shows up. But when I try to do any query, I always get null back. The String version()
method isn't even called.
I'm using the latest version of GraphQL-java (2.0.0) and everything is working fine in the other parts of the scheme written without annotations.
Thanks for the help in advance.
I used @GraphQLField for the field name and it returned the Optional type too
{"query":
{
"name": "Optional[John Doe]"
}
}
Hi All,
Thank you for this project. It looks promising. I am trying to launch an exemplary spring boot jpa application and add graphql-java-annotations to it. If someone knows how to connect them in a simple way, I would be grateful for an example.
Meanwhile, when I was tinkering with the project I noticed that ExecutionResult returns something like:
{defaultUser={name=Test Name}}
It is taken from your tests. Could you tell me why it is not like JSON? What I should do to receive JSON as a result?
Is it possible to use observables/futures/etc with method data fetchers? I have an execution strategy that supports observables, but I'm having trouble wiring it up when I'm returning data from a method.
For example: I have a service that returns an Observable<Model>
. I can wire it up using a field builder like so:
newFieldDefinition().
.type(Model)
.dataFetcher(env -> service.getModelById(1234)) // returns an Observable<Model>
.build()
However, if I want to define my field with a MethodDataFetcher, I don't see a clean way to define my return type as Model
, but really return an Observable<Model>
:
// Doesn't work... doesn't know what to do with a return type of observable...?
@GraphQLField
public Observable<Model> getModel(int id) {
return service.getModelById(1234);
}
// Doesn't work... incompatible return types
@GraphQLField
public Model getModel(int id) {
return service.getModelById(1234);
}
Is this something I can add with a TypeResolver? (i.e., if the return type is Observable<T>
, then the return type should map to T
). Or does it have to be baked into the library? As always, thanks for your time.
Alex
Hello, I try to use graphql-java-annotations in my project.
But @GraphQLField
does not work in private field.
Do you have a plan support this feature?
My graphql-java-annotations's version is 0.11.0
When using a BatchedExecutionStrategy
, graphql-java
requires the get
method of the data fetcher to be decorated with the @Batched
annotation.
Currently the only way of using batched fetchers with graphql-java-annotations
is to use a custom @GraphQLDataFetcher
. I think this can be solved by adding a new annotation called GraphQLBatched
that can be added to methods and then a special subclass of MethodDataFetcher
should internally use the @Batched
annotation.
If you agree with this approach I can provide a PR.
Hi,
I would like to know if the annotations supports introspecting a List
of complex input type.
Here's (simplified) example of what I'm trying to achieve which leads to a Introspection must provide input type for arguments
error in chrome GraphiQL client .
I wonder if I'm doing something wrong or if there is no support of list of complex input types through annotations.
The SpaceInstanceInput
below is what I'm passing as a parameter of this mutation
@GraphQLField
public MutationResult updateSpaceInstance(
@GraphQLName("spaceInstanceInput") final SpaceInstanceInput spaceInstanceInput) {
where SpaceInstanceInput
data structure looks like:
public class SpaceInstanceInput {
private final List<LogSubscriptionInput> logSubscriptions = null;
private LogSubscriptionInput firstLogSubscriptions;
// WORKS FINE
@GraphQLField
public LogSubscriptionInput getFirstLogSubscription() {
return this.firstLogSubscriptions;
}
// DOES NOT WORK
@GraphQLField
public List<LogSubscriptionInput> getLogSubscriptions() {
return this.logSubscriptions;
}
}
public class LogSubscriptionInput {
private LogTypeInput logTypeInput;
private String subscriptionId;
@GraphQLField
public LogTypeInput getLogTypeInput() {
return this.logTypeInput;
}
@GraphQLField
public String getSubscriptionId() {
return this.subscriptionId;
}
}
public class LogTypeInput {
private final String format;
private final String type;
@GraphQLField
public String getFormat() {
return this.format;
}
@GraphQLField
public String getType() {
return this.type;
}
}
I started to use your library, and it's really interesting. But I got a problem : I need to provide loose coupled interfaces. My domain model is provided as set of interfaces, with different implementations in different services. So domain interfaces can't be aware of their implementation.
Or is it actually impossible to generate interfaces without a GraphQLTypeResolver interface, and so a known TypeResolver. So with the actual architecture, it's impossible to generate loose coupled interfaces.
A solution could be to use ServiceLoader system, so TypeResolver could be provided by the implementation jar. Actually , the default comportement for GraphQLAnnotations#getInterface if ther's no GraphQLTypeResolver on the interface is to fallback to an object (there's an error in the javadoc by the way). Fallback to a TypeResolvers class which use ServiceLoader solve the tight coupling problem. But it's breaking change... Another solution is to handle the absence of the annotation only in getIfaceBuilder, at the end the method. It does not break the contract, but I think it's a bit strange, as it's contradictory with the comportement of getInterface.
If one of the solution looks good for you, I'll submit a PR. If not, I'll use my own fork for my project...
Hi, I'm wondering how to set the possible types that implement an interface. Similar to @GraphQLUnion(possibleTypes = TestObject1.class) on Union Types.
Trying to run an inline fragment query to fetch implementation specific fields, but getting an error like this: "Fragment cannot be spread here as objects of type GraphQLInterfaceType can never be of type GraphQLObjectType"
The query doesn't pass validation, probably because the interface definition has empty "possibleTypes": []
Introspection query result snippet:
{
"kind": "INTERFACE",
"name": "Character",
"description": null,
"fields": [.........],
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": []
}
I must be missing something.... if anyone has gotten this to work, please let me know!
Currently, the schema supports int
String
... Any plan for the support of Map
?
When used with graphql-java 2.4.0
java.lang.IncompatibleClassChangeError: Found interface graphql.schema.DataFetchingEnvironment is thrown during data fetching.
DataFetchingEnvironmnet has been changed from a class to an interface in graphql-java 2.4.0.
can you please make this available in a public repo such as Maven central?
I can't find a place in the code where this method is called. Is it normal? Isn't the attended behavior to do this check before all TypeFunction#apply ?
EnhancedExecutionStrategy (really Relay.js execution strategy) doesn't really seem to belong in the same module as the rest of the annotation processing, since it can be used separately.
I'd like to include it as a dependency in graphql-java-servlet
without requiring the use of annotations - would it be possible to create a new module or project (graphql-java-executioncontexts? :P ) for it that deploys a separate jarfile?
Hi yrashk,
I'm using your MethodDataFetcher
to get my data from a service. My service throws exceptions on failures, but the MethodDataFetcher
is consuming anything I throw. The result is I have no data, but executionResult.getErrors()
is empty.
Is there a reason that the exceptions are not rethrown?
Thanks,
Alex
I have a field which is a List of chicks. This last is annotated with @GraphQLConnection.java:
@GraphQLConnection
@GraphQLDataFetcher(CatDataFetcher.class)
@GraphQLField
public List<Cat> cats;
My usecase is to fetch available cats from a database. For this purpose, I have created a CatDataFetcher class that builds and executes a query against my database. It is working but this basic approach fetch all cats in memory before filtering them based on criteria passed with the connection (e.g. first=10, last=3, etc.).
Since I have a lot of entries, I would prefer to apply pagination at the database level directly. By default, filtering for a Connection is performed by SimpleListConnection after the execution of a GraphQLDataFetcher. I have seen it is possible to specify a DispatchingConnection with the @GraphQLConnection annotation.
What would be the recommended way to proceed with my use case? should I move the code that builds the query and execute it in a dedicated DispatchingConnection and simply remove the GraphQLDataFetcher?
Should @GraphQLDataFetcher never be used when @GraphQLConnection is already set on a field?
In the case I use a custom DispatchingConnection with the @GraphQLConnection annotation, can I assume that additional arguments that will be defined on the field will be accessible from my custom DispatchingConnection (once #33 is fixed)?
if I have a bean with
@GraphQlField
public List<Foo> getFoos() ...
it works as expected and creates GraphqlList(Foo)
But what about anything Iterable
@GraphQlField
public Collection<Foo> getFoos() ...
@GraphQlField
public Set<Foo> getFoos() ...
@GraphQlField
public Iterable<Foo> getFoos() ...
They can ALL be mapped to GraphqlList no? Am I missing something?
Currently its defined as
@Override public Collection<Class<?>> getAcceptedTypes() {
return Arrays.asList(List.class, AbstractList.class);
}
But I think anything that can be iterated can be represented by a List in graphql no?
I'm trying to have a model return a field of the same type. Ex:
public class Model {
@GraphQLField
public List<Model> similarModels() {
return ...
}
}
When I inspect the resulting schema, I see that the similarModels()
field is left a reference type and is never resolved to a model. When I try to make a query on this field, I get the following error:
graphql.schema.GraphQLTypeReference cannot be cast to graphql.schema.GraphQLUnmodifiedType
I have tried passing in types that I reference to schema.build() without success. At what point should this reference type be resolved to a concrete GraphQLObjectType?
Thanks,
Alex
This lib is awesome, make GraphQL less verbose to use in Java, thanks!
But it looks like that this lib depends on jdk 1.8, is there any particular reason for this? our production code is still using jdk1.6, but want to use this lib.
I have a query type with a field named jobs:
@GraphQLField
@GraphQLConnection
public List<Job> jobs(JobInput jobInput) {
....
}
This last field defines an InputType in order to pass some arguments.
@GraphQLType
@Getter
public class JobInput {
@GraphQLName("id")
private long id;
@GraphQLName("test")
private String name;
}
Unfortunately, when settings the JobInput type, any GraphQL query that is submitted leads to a NoSuchElementException in MethodDataFetcher#invocationArgs. The problem seems due to the environment that no longer contains the JobInput argument that is defined with the field. This argument is removed from the environment when GraphQLAnnotations.ConnectionDataFetcher#get is invoked. There is even an explicit comment saying Exclude arguments. I am not sure to understand if this removal is a bug (i.e. only arguments related to relay pagination should be removed) or a voluntary action.
Is there a standard way to support arguments with a GraphQLConnection?
How would you define a Union type?
I am trying to leverage your package to make a schema builder. To do this, I scan my package for a certain class annotation, and construct a GraphQLObjectType
from any of those classes found.
I'm having a problem with the MethodDataFetcher on methods using this approach (the GraphQLDataFetcher
annotation with a separate data fetching class works). When the MethodDataFetcher tries to resolve the field, environment.getSource()
is null
and the fetcher returns null
. This only appears to be a problem for fields defined on the QueryRoot.
A root fields node may be defined like this:
class ModelRootQueries {
public Model modelById(@GraphQLName("id") String id) {
// never invoked
return ModelService.get(id);
}
}
I am constructing my QueryRoot like so:
GraphQLObjectType node = GraphQLAnnotations.newObject(ModelRootQueries.class);
root.fields(node.getFieldDefinitions();
...
GraphQLSchema schema = newSchema.query(root).build();
In this case, the body of the method is never called, and data
ends up being null
.
I'd appreciate some insight into whether I'm wiring it incorrectly, or if there's a problem with the MethodDataFetcher's implementation.
Thanks,
Alex
Inside the sourcecode i stumbled upon the @GraphQLInvokeDetached
annotation. Could you explain what it is used for?
Hi,
This is not really an issue but a request.
Could be possible to have an example of a complete schema using graphql-java-annotations?
I have tried to look for examples but I haven't found anything I could be based on.
Thanks in advance.
Ane
Lets say I want to write an extension "container" TypeFunction
Imagine its like the build in List one where it detects the "outer type" and then wants to recurse into the inner types and run them back thought the default type processor again.
For example this is the current List type function
@Override
public GraphQLType apply(Class<?> aClass, AnnotatedType annotatedType) {
if (!(annotatedType instanceof AnnotatedParameterizedType)) {
throw new IllegalArgumentException("List type parameter should be specified");
}
AnnotatedParameterizedType parameterizedType = (AnnotatedParameterizedType) annotatedType;
AnnotatedType arg = parameterizedType.getAnnotatedActualTypeArguments()[0];
Class<?> klass;
if (arg.getType() instanceof ParameterizedType) {
klass = (Class<?>) ((ParameterizedType) (arg.getType())).getRawType();
} else {
klass = (Class<?>) arg.getType();
}
return new GraphQLList(DefaultTypeFunction.this.apply(klass, arg));
}
Notice how after unwrapping the "container" it can call back into sub types via
DefaultTypeFunction.this.apply(klass, arg)
How if you write your own TypeFunction you cant get a hold of the default TypeFunction
Its defined as
protected TypeFunction defaultTypeFunction;
So you can make a sub class and hence extract it out but I think it should be public and hence available for re-use for "container" types.
Here
https://github.com/graphql-java/graphql-java-annotations#type-inference
Was trying to implement v0.13 and this came out.
Could you please put an example on how this can be achieved with this improvement?
When I build annotations with graphql-java master branch, the following error occur.
GraphQLAnnotations.java:82: error: incompatible types: invalid method reference
}).forEach(builder::possibleType);
^
incompatible types: GraphQLType cannot be converted to GraphQLObjectType
I believe that it is related to this change in graphql-java
graphql-java/graphql-java#129
The input parameter of function GraphQLUnionType -> Builder -> possibleType is changed.
- public Builder possibleType(GraphQLType type) {
+ public Builder possibleType(GraphQLObjectType type) {
This will help with further introduction of Relay support for Streams (which will be quite handy)
If i don't add @GraphQLName("name")
, then method would not be recognized correctly! (version: 0.13.2)
@GraphQLField
@GraphQLDataFetcher(MyDataFetcher.class)
public String findByName(@GraphQLName("name") String name){
return null;
}
I was having this issue because Enum definitions were only working with ordinals and not objects. Then I finally figured out it was the EnumFunction in DefaultTypeFunction it's only constructing newEnum using ordinals instead of objects.
Should that be the default behavior?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.