Skip to content
Closed
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
70 changes: 49 additions & 21 deletions src/main/java/graphql/annotations/GraphQLAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
* 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,
Expand All @@ -16,24 +16,8 @@

import graphql.TypeResolutionEnvironment;
import graphql.relay.Relay;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.DataFetchingEnvironmentImpl;
import graphql.schema.FieldDataFetcher;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInputType;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLList;
import graphql.schema.*;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLTypeReference;
import graphql.schema.GraphQLUnionType;
import graphql.schema.PropertyDataFetcher;
import graphql.schema.TypeResolver;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

Expand Down Expand Up @@ -209,7 +193,6 @@ private static Boolean isGraphQLField(AnnotatedElement element) {
* breadthFirst parental ascent looking for closest method declaration with explicit annotation
*
* @param method The method to match
*
* @return The closest GraphQLField annotation
*/
private boolean breadthFirstSearch(Method method) {
Expand Down Expand Up @@ -251,7 +234,6 @@ private boolean breadthFirstSearch(Method method) {
* direct parental ascent looking for closest declaration with explicit annotation
*
* @param field The field to find
*
* @return The closest GraphQLField annotation
*/
private boolean parentalSearch(Field field) {
Expand Down Expand Up @@ -459,7 +441,7 @@ protected GraphQLFieldDefinition getField(Field field) throws GraphQLAnnotations

private DataFetcher constructDataFetcher(String fieldName, GraphQLDataFetcher annotatedDataFetcher) {
final String[] args;
if ( annotatedDataFetcher.firstArgIsTargetName() ) {
if (annotatedDataFetcher.firstArgIsTargetName()) {
args = Stream.concat(Stream.of(fieldName), stream(annotatedDataFetcher.args())).toArray(String[]::new);
} else {
args = annotatedDataFetcher.args();
Expand Down Expand Up @@ -554,8 +536,12 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
graphql.schema.GraphQLType graphQLType = finalTypeFunction.buildType(t, parameter.getAnnotatedType());
if (graphQLType instanceof GraphQLObjectType) {
GraphQLInputObjectType inputObject = getInputObject((GraphQLObjectType) graphQLType, "input");
inputObject = (GraphQLInputObjectType) getOrPutInRegistry(inputObject);
graphQLType = inputObject;
} else if (graphQLType instanceof GraphQLList) {
graphQLType = handleGraphQLList(graphQLType);
}

return getArgument(parameter, graphQLType);
}).collect(Collectors.toList());

Expand Down Expand Up @@ -616,6 +602,26 @@ protected GraphQLFieldDefinition getField(Method method) throws GraphQLAnnotatio
return new GraphQLFieldDefinitionWrapper(builder.build());
}


private Stack<graphql.schema.GraphQLType> getWrappedTypesStack(graphql.schema.GraphQLType graphQLType, Stack<graphql.schema.GraphQLType> stack) {
stack.push(graphQLType);
if (graphQLType instanceof GraphQLList) {
return getWrappedTypesStack(((GraphQLList) graphQLType).getWrappedType(), stack);
} else {
return stack;
}
}

private graphql.schema.GraphQLType getOrPutInRegistry(graphql.schema.GraphQLType graphQLType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename method

graphql.schema.GraphQLType typeFromRegistry = typeRegistry.get(graphQLType.getName());
if (typeFromRegistry != null) {
return typeFromRegistry;
} else {
typeRegistry.put(graphQLType.getName(), graphQLType);
}
return graphQLType;
}

protected static GraphQLFieldDefinition field(Method method) throws InstantiationException, IllegalAccessException {
return getInstance().getField(method);

Expand All @@ -631,6 +637,8 @@ public GraphQLInputObjectType getInputObject(GraphQLObjectType graphQLType, Stri
GraphQLInputType inputType;
if (type instanceof GraphQLObjectType) {
inputType = getInputObject((GraphQLObjectType) type, newNamePrefix);
} else if (type instanceof GraphQLList) {
inputType = handleGraphQLList(type);
} else {
inputType = (GraphQLInputType) type;
}
Expand All @@ -640,6 +648,26 @@ public GraphQLInputObjectType getInputObject(GraphQLObjectType graphQLType, Stri
collect(Collectors.toList()));
}

private GraphQLInputType handleGraphQLList(graphql.schema.GraphQLType type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better that the function get a GraphQLList as an input

GraphQLInputType inputType;
Stack<graphql.schema.GraphQLType> typesStack = new Stack<>();
typesStack = getWrappedTypesStack(type, typesStack);
graphql.schema.GraphQLType wrappedType = typesStack.pop();
if (wrappedType instanceof GraphQLObjectType) {
GraphQLInputObjectType inputObject = getInputObject((GraphQLObjectType) wrappedType, "input");
inputObject = (GraphQLInputObjectType) getOrPutInRegistry(inputObject);
inputType = inputObject;
while (!typesStack.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract to different method

inputType = new GraphQLList(inputType);
typesStack.pop();
}

} else {
inputType = (GraphQLInputType) type;
}
return inputType;
}

public static GraphQLInputObjectType inputObject(GraphQLObjectType graphQLType, String newNamePrefix) {
return getInstance().getInputObject(graphQLType, newNamePrefix);
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/graphql/annotations/GraphQLObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 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
* 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,
Expand Down Expand Up @@ -74,12 +74,12 @@ String field() {
}

@GraphQLField
public String fieldWithArgs(@NotNull String a, @GraphQLDefaultValue(DefaultAValue.class) @GraphQLDescription("b") String b) {
public String fieldWithArgs(@GraphQLName("a") @NotNull String a, @GraphQLName("b") @GraphQLDefaultValue(DefaultAValue.class) @GraphQLDescription("b") String b) {
return b;
}

@GraphQLField
public String fieldWithArgsAndEnvironment(DataFetchingEnvironment env, String a, String b) {
public String fieldWithArgsAndEnvironment(DataFetchingEnvironment env,@GraphQLName("a") String a, @GraphQLName("b") String b) {
return a;
}

Expand Down Expand Up @@ -551,7 +551,7 @@ public TestInputArgument(HashMap<String, Object> args) {

private static class TestObjectInput {
@GraphQLField
public String test(int other, TestInputArgument arg) {
public String test(@GraphQLName("other") int other, @GraphQLName("arg") TestInputArgument arg) {
return arg.a;
}
}
Expand Down
18 changes: 12 additions & 6 deletions src/test/java/graphql/annotations/RelayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface IResult {
int getI();
}

public static class Result implements IResult{
public static class Result implements IResult {
private final int i;

public Result(int i) {
Expand All @@ -57,22 +57,28 @@ public int getI() {
}

public static class WrongReturnType {
@GraphQLField @GraphQLRelayMutation
@GraphQLField
@GraphQLRelayMutation
public int doSomething() {
return 0;
}
}

public static class TestObject {
@GraphQLField @GraphQLRelayMutation
@GraphQLField
@GraphQLRelayMutation
public Result doSomething() {
return new Result(0);
}
@GraphQLField @GraphQLRelayMutation
public Result doSomethingElse(@GraphQLDescription("A") int a, int b) {

@GraphQLField
@GraphQLRelayMutation
public Result doSomethingElse(@GraphQLName("a") @GraphQLDescription("A") int a, @GraphQLName("b") int b) {
return new Result(a - b);
}
@GraphQLField @GraphQLRelayMutation

@GraphQLField
@GraphQLRelayMutation
public IResult doSomethingI() {
return new Result(0);
}
Expand Down