Skip to content

Commit 0e66102

Browse files
authored
Add optional support for GraphQLIDType (i.e. ID) for entity identifier attributes (#401)
1 parent 5c86b57 commit 0e66102

11 files changed

+2990
-2883
lines changed

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/GraphQLSchemaBuilder.java

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public interface GraphQLSchemaBuilder {
6767
*/
6868
GraphQLSchemaBuilder scalar(Class<?> javaType, GraphQLScalarType scalarType);
6969

70+
/**
71+
* Use GraphQLIDType attribute for entity identifier attributes. Defaults to false.
72+
*
73+
* @param useGraphQLIDType fl
74+
* @return this builder instance
75+
*/
76+
GraphQLSchemaBuilder graphQLIDType(boolean useGraphQLIDType);
77+
7078
/**
7179
* Builds {code #GraphQLSchema} instance
7280
*

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaQueryFactory.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1501,8 +1501,11 @@ protected Object convertValue(DataFetchingEnvironment environment, Argument argu
15011501
} else if (value instanceof StringValue) {
15021502
Object convertedValue = environment.getArgument(argument.getName());
15031503
if (convertedValue != null) {
1504+
Class javaType = getJavaType(environment, argument);
15041505
// Return real typed resolved value even if the Value is a StringValue
1505-
return convertedValue;
1506+
return javaType.isInstance(convertedValue)
1507+
? convertedValue
1508+
: JavaScalars.of(javaType).getCoercing().parseValue(convertedValue);
15061509
} else {
15071510
// Return provided StringValue
15081511
return ((StringValue) value).getValue();
@@ -1522,7 +1525,9 @@ protected Object convertValue(DataFetchingEnvironment environment, Argument argu
15221525
}
15231526
} else {
15241527
// Get resolved variable in environment arguments
1525-
return argumentValue;
1528+
return javaType.isInstance(argumentValue)
1529+
? argumentValue
1530+
: JavaScalars.of(javaType).getCoercing().parseValue(argumentValue);
15261531
}
15271532
} else if (value instanceof ArrayValue) {
15281533
Collection arrayValue = environment.getArgument(argument.getName());

schema/src/main/java/com/introproventures/graphql/jpa/query/schema/impl/GraphQLJpaSchemaBuilder.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public class GraphQLJpaSchemaBuilder implements GraphQLSchemaBuilder {
131131
private int defaultPageLimitSize = 100;
132132
private boolean enableDefaultMaxResults = true;
133133
private boolean enableResultStream = false;
134+
private boolean graphQLIDType = false;
134135

135136
private RestrictedKeysProvider restrictedKeysProvider = entityDescriptor -> Optional.of(Collections.emptyList());
136137
private Map<Class<?>, GraphQLScalarType> scalars = new LinkedHashMap<>();
@@ -1246,7 +1247,9 @@ private GraphQLOutputType getAttributeOutputType(Attribute<?, ?> attribute) {
12461247
@SuppressWarnings("rawtypes")
12471248
protected GraphQLType getAttributeType(Attribute<?, ?> attribute, boolean input, boolean searchByIdArgType) {
12481249
if (isBasic(attribute)) {
1249-
return getGraphQLTypeFromJavaType(attribute.getJavaType());
1250+
return searchByIdArgType && isGraphQLIDType()
1251+
? Scalars.GraphQLID
1252+
: getGraphQLTypeFromJavaType(attribute.getJavaType());
12501253
} else if (isEmbeddable(attribute)) {
12511254
EmbeddableType embeddableType = (EmbeddableType) ((SingularAttribute) attribute).getType();
12521255
return getEmbeddableType(embeddableType, input, searchByIdArgType);
@@ -1667,4 +1670,15 @@ public GraphQLJpaSchemaBuilder enableResultStream(boolean enableResultStream) {
16671670

16681671
return this;
16691672
}
1673+
1674+
@Override
1675+
public GraphQLJpaSchemaBuilder graphQLIDType(boolean graphQLIDType) {
1676+
this.graphQLIDType = graphQLIDType;
1677+
1678+
return this;
1679+
}
1680+
1681+
public boolean isGraphQLIDType() {
1682+
return this.graphQLIDType;
1683+
}
16701684
}

0 commit comments

Comments
 (0)