Skip to content

Commit 560a39d

Browse files
author
etienne-sf
committed
Issue #166: Error in Request exec field's name are java keywords
1 parent 060b6fb commit 560a39d

File tree

12 files changed

+423
-243
lines changed

12 files changed

+423
-243
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ Whether the application uses the _graphql_, the _generateClientCode_ or the _gen
99
* separateUtilityClasses: true _(both client and server mode)_
1010
* skipGenerationIfSchemaHasNotChanged: true _(both client and server mode)_
1111

12+
13+
# Not released yet
14+
15+
16+
Both modes:
17+
* Issue #166 : Corrected an issue that prevents to request data when GraphQL field's name are java reserved keywords
18+
19+
1220
# 1.18.8
1321

1422
Dependency upgrade:

graphql-java-client-runtime/src/main/java/com/graphql_java_generator/client/GraphqlClientUtils.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,8 @@ else if ("Float".equals(graphQLTypeName) || "Double".equals(graphQLTypeName))
225225
// this name in the given package.
226226
try {
227227
// lookup the java class corresponding to the graphql type from the generated GraphQLTypeMapping
228-
return (Class<?>) getClass().getClassLoader()
229-
.loadClass(packageName + ".GraphQLTypeMapping")
230-
.getMethod("getJavaClass", String.class)
231-
.invoke(null, graphQLTypeName);
228+
return (Class<?>) getClass().getClassLoader().loadClass(packageName + ".GraphQLTypeMapping")
229+
.getMethod("getJavaClass", String.class).invoke(null, graphQLTypeName);
232230
} catch (ClassNotFoundException e) {
233231
// If GraphqlTypeMapping does not exist (in some tests), fallback to using the type name.
234232
final String className = packageName + "." + GraphqlUtils.graphqlUtils.getJavaName(graphQLTypeName);
@@ -239,8 +237,8 @@ else if ("Float".equals(graphQLTypeName) || "Double".equals(graphQLTypeName))
239237
"Could not load class '" + className + "' for type '" + graphQLTypeName + "'", e);
240238
}
241239
} catch (ClassCastException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
242-
throw new RuntimeException(
243-
"Could not get the class for type '" + graphQLTypeName + "' from '" + (packageName + ".GraphQLTypeMapping") + "'" , e);
240+
throw new RuntimeException("Could not get the class for type '" + graphQLTypeName + "' from '"
241+
+ (packageName + ".GraphQLTypeMapping") + "'", e);
244242
}
245243
}
246244

graphql-java-common-runtime/src/main/java/com/graphql_java_generator/util/GraphqlUtils.java

Lines changed: 213 additions & 164 deletions
Large diffs are not rendered by default.

graphql-maven-plugin-logic/src/main/resources/templates/server_GraphQLWiring.vm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ protected RuntimeWiring buildWiring() {
122122
*/
123123
protected TypeRuntimeWiring.Builder addWiringFor${dataFetchersDelegate.type.javaName}(TypeRuntimeWiring.Builder typeWiring) {
124124
#foreach ($dataFetcher in $dataFetchersDelegate.dataFetchers)
125-
typeWiring.dataFetcher("${dataFetcher.field.javaName}", graphQLDataFetchers.${dataFetchersDelegate.camelCaseName}${dataFetcher.pascalCaseName}#if(${dataFetcher.completableFuture})WithDataLoader#end());
125+
typeWiring.dataFetcher("${dataFetcher.field.name}", graphQLDataFetchers.${dataFetchersDelegate.camelCaseName}${dataFetcher.pascalCaseName}#if(${dataFetcher.completableFuture})WithDataLoader#end());
126126
#end
127127
return typeWiring;
128128
}

graphql-maven-plugin-logic/src/test/java/com/graphql_java_generator/plugin/generate_code/DocumentParser_allGraphQLCases_Server_Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ void test_parseOneDocument_allGraphQLCases() throws IOException {
7777
int i = generateCodeDocumentParser.parseGraphQLSchemas();
7878

7979
// Verification
80-
assertEquals(57, i, "Nb java files are generated");
80+
assertEquals(58, i, "Nb java files are generated");
8181
assertEquals(10, generateCodeDocumentParser.getDirectives().size(), "Nb directives");
82-
assertEquals(34, generateCodeDocumentParser.getObjectTypes().size(), "Nb objects");
82+
assertEquals(35, generateCodeDocumentParser.getObjectTypes().size(), "Nb objects");
8383
assertEquals(5, generateCodeDocumentParser.getCustomScalars().size(), "Nb custom scalars");
8484
assertEquals(19, generateCodeDocumentParser.getInterfaceTypes().size(), "Nb interfaces");
8585
assertEquals(4, generateCodeDocumentParser.getEnumTypes().size(), "Nb enums");
@@ -680,7 +680,7 @@ void test_readObjectType_QueryType() throws IOException {
680680

681681
// Verification
682682
assertEquals("MyQueryType", type.getName());
683-
assertEquals(52, type.getFields().size());
683+
assertEquals(53, type.getFields().size());
684684

685685
int j = 0; // The first query is 0, see ++j below
686686

graphql-maven-plugin-samples/graphql-maven-plugin-samples-allGraphQLCases-client/src/graphqls/allGraphQLCases/allGraphQLCases.graphqls

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type MyQueryType {
101101
param1: EnumWithReservedJavaKeywordAsValues=abstract,
102102
param2: [EnumWithReservedJavaKeywordAsValues]=[assert,boolean]
103103
): [EnumWithReservedJavaKeywordAsValues]
104+
reservedJavaKeywordAllFieldCases: ReservedJavaKeywordAllFieldCases
104105
if: String
105106
implements: String
106107
import: String
@@ -188,39 +189,6 @@ interface UnusedInterface {
188189
aNonUSedField: String
189190
}
190191

191-
# This class contains fields that are Java reserved words. A query allows to check that mapping properly occurs on both client and server side.
192-
#type JavaReservedWords {
193-
# if: String
194-
# implements: String
195-
# import: String
196-
# instanceof: String
197-
# int: String
198-
# interface: String
199-
# long: String
200-
# native: String
201-
# new: String
202-
# package: String
203-
# private: String
204-
# protected: String
205-
# public: String
206-
# return: String
207-
# short: String
208-
# static: String
209-
# strictfp: String
210-
# super: String
211-
# switch: String
212-
# synchronized: String
213-
# this: String
214-
# throw: String
215-
# throws: String
216-
# transient: String
217-
# try: String
218-
# void: String
219-
# volatile: String
220-
# while: String
221-
#}
222-
223-
224192
##############################################################################################################
225193
################ ALL GRAPHQL CASES TEST CASES################################################################
226194
##############################################################################################################
@@ -413,7 +381,7 @@ interface Character @testDirective(value:"on Character interface") @anotherTestD
413381
}
414382

415383
union AnyCharacter @testDirective(value:"on Union")
416-
= Human | Droid
384+
= Human | Droid
417385

418386

419387

@@ -788,4 +756,24 @@ type ReservedJavaKeywordAsObjectField {
788756
void: Human
789757
volatile: Human
790758
while: Human
759+
}
760+
761+
#############################
762+
# test for issue #166:
763+
# This class contains fields that are Java reserved words. A query allows to check that mapping properly occurs on both client and server side.
764+
type ReservedJavaKeywordAllFieldCases {
765+
# A field with a java reserved name which is an enum
766+
if: Unit
767+
# A field with a _non_ java reserved name which is an interface (to check standard behavior)
768+
nonJavaKeywordField: WithID
769+
# A field with a java reserved name which is an interface
770+
implements: WithID
771+
# A field with a java reserved name which is a scalar (standard)
772+
import: String
773+
# A field with a java reserved name which is a scalar (custom)
774+
instanceof: Date
775+
# A field with a java reserved name which is an object type
776+
int: Human
777+
# A field with a java reserved name which is a union
778+
interface: AnyCharacter
791779
}

graphql-maven-plugin-samples/graphql-maven-plugin-samples-allGraphQLCases-client/src/test/java/org/allGraphQLCases/IntrospectionIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ void testSchema() throws GraphQLRequestExecutionException, GraphQLRequestPrepara
5454
CTP___Schema_CTS schema = myQuery.__schema("{types {name fields(includeDeprecated:true) {name type {name}}}}");
5555

5656
// Verification
57-
assertEquals(82, schema.getTypes().size());
5857
assertEquals("AllFieldCases", schema.getTypes().get(0).getName());
5958
// As the order of fields seems to depend on the way this is compile, let's use a rough method to check the
6059
// field names

graphql-maven-plugin-samples/graphql-maven-plugin-samples-allGraphQLCases-client/src/test/java/org/allGraphQLCases/PartialQueryIT.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import static org.junit.jupiter.api.Assertions.assertTrue;
1010

1111
import java.util.ArrayList;
12-
import java.util.Arrays;
1312
import java.util.Calendar;
1413
import java.util.Date;
1514
import java.util.List;
1615

17-
import org.allGraphQLCases.client.CTP_AllFieldCases_CTS;
1816
import org.allGraphQLCases.client.CEP_EnumWithReservedJavaKeywordAsValues_CES;
1917
import org.allGraphQLCases.client.CINP_FieldParameterInput_CINS;
18+
import org.allGraphQLCases.client.CTP_AllFieldCases_CTS;
19+
import org.allGraphQLCases.client.CTP_ReservedJavaKeywordAllFieldCases_CTS;
2020
import org.allGraphQLCases.client.util.AnotherMutationTypeExecutorAllGraphQLCases;
2121
import org.allGraphQLCases.client.util.GraphQLRequest;
2222
import org.allGraphQLCases.client.util.MyQueryTypeExecutorAllGraphQLCases;
@@ -167,8 +167,8 @@ void test_Issue139_EnumValueIf() throws GraphQLRequestPreparationException, Grap
167167
void test_Issue139_EnumValueListOfJavaReservedKeywords_withNullParams()
168168
throws GraphQLRequestPreparationException, GraphQLRequestExecutionException {
169169
// Go, go, go
170-
List<CEP_EnumWithReservedJavaKeywordAsValues_CES> response = queryType.listOfEnumWithReservedJavaKeywordAsValues("",
171-
null, null);
170+
List<CEP_EnumWithReservedJavaKeywordAsValues_CES> response = queryType
171+
.listOfEnumWithReservedJavaKeywordAsValues("", null, null);
172172
// the two parameters are null. Their default values are:
173173
// param1: CEP_EnumWithReservedJavaKeywordAsValues_CES=abstract,
174174
// param2: [CEP_EnumWithReservedJavaKeywordAsValues_CES]=[assert,boolean]
@@ -184,20 +184,34 @@ void test_Issue139_EnumValueListOfJavaReservedKeywords_withNullParams()
184184

185185
@Test
186186
@Execution(ExecutionMode.CONCURRENT)
187-
void test_Issue139_EnumValueListOfJavaReservedKeywords()
187+
void test_Issue166_FieldAsJavaReservedKeywords()
188188
throws GraphQLRequestPreparationException, GraphQLRequestExecutionException {
189189
// Go, go, go
190-
List<CEP_EnumWithReservedJavaKeywordAsValues_CES> response = queryType.listOfEnumWithReservedJavaKeywordAsValues("", //
191-
/* param1 */CEP_EnumWithReservedJavaKeywordAsValues_CES._return, //
192-
/* param2 */ Arrays.asList(CEP_EnumWithReservedJavaKeywordAsValues_CES._byte,
193-
CEP_EnumWithReservedJavaKeywordAsValues_CES._const));
190+
CTP_ReservedJavaKeywordAllFieldCases_CTS response = queryType.reservedJavaKeywordAllFieldCases(""//
191+
+ "{" //
192+
+ " if "// the if type is an enum (Unit)
193+
+ " nonJavaKeywordField {id} " // The nonJavaKeywordField field is an interface (WithID)
194+
+ " implements {id} " // The implements field is an interface (WithID)
195+
+ " import "// The import field is a scalar (String)
196+
+ " instanceof "// The instanceof field is custom scalar (Date)
197+
+ " int {id name} "// The int field is an object type (Human)
198+
+ " interface {"// The interface field is an union (AnyCharacter). Queries on union must specify what
199+
// field should be returned, depending on the returned type.
200+
+ " ... on Human {id name}" //
201+
+ " ... on Droid {id name}" //
202+
+ " }" //
203+
+ "}");
194204

195205
// Verification
196206
assertNotNull(response);
197-
assertEquals(3, response.size());
198-
assertEquals(CEP_EnumWithReservedJavaKeywordAsValues_CES._return, response.get(0));
199-
assertEquals(CEP_EnumWithReservedJavaKeywordAsValues_CES._byte, response.get(1));
200-
assertEquals(CEP_EnumWithReservedJavaKeywordAsValues_CES._const, response.get(2));
207+
assertNotNull(response.get__typename());
208+
assertNotNull(response.getIf());
209+
assertNotNull(response.getNonJavaKeywordField());
210+
assertNotNull(response.getImplements());
211+
assertNotNull(response.getImport());
212+
assertNotNull(response.getInstanceof());
213+
assertNotNull(response.getInt());
214+
assertNotNull(response.getInterface());
201215
}
202216

203217
}

graphql-maven-plugin-samples/graphql-maven-plugin-samples-allGraphQLCases-server/generated_schema.graphqls

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ type MyQueryType {
349349
# test for issue #139 (use of java reserved keyword)
350350
enumWithReservedJavaKeywordAsValues: EnumWithReservedJavaKeywordAsValues
351351
listOfEnumWithReservedJavaKeywordAsValues (param1: EnumWithReservedJavaKeywordAsValues=abstract, param2: [EnumWithReservedJavaKeywordAsValues]=[assert,boolean]): [EnumWithReservedJavaKeywordAsValues]
352+
reservedJavaKeywordAllFieldCases: ReservedJavaKeywordAllFieldCases
352353
if: String
353354
implements: String
354355
import: String
@@ -740,6 +741,26 @@ type ReservedJavaKeywordAsObjectField {
740741
while: Human
741742
}
742743

744+
#############################
745+
# test for issue #166:
746+
# This class contains fields that are Java reserved words. A query allows to check that mapping properly occurs on both client and server side.
747+
type ReservedJavaKeywordAllFieldCases {
748+
# A field with a java reserved name which is an enum
749+
if: Unit
750+
# A field with a _non_ java reserved name which is an interface (to check standard behavior)
751+
nonJavaKeywordField: WithID
752+
# A field with a java reserved name which is an interface
753+
implements: WithID
754+
# A field with a java reserved name which is a scalar (standard)
755+
import: String
756+
# A field with a java reserved name which is a scalar (custom)
757+
instanceof: Date
758+
# A field with a java reserved name which is an object type
759+
int: Human
760+
# A field with a java reserved name which is a union
761+
interface: AnyCharacter
762+
}
763+
743764
type PageInfo {
744765
hasNextPage: Boolean!
745766
hasPreviousPage: Boolean!

graphql-maven-plugin-samples/graphql-maven-plugin-samples-allGraphQLCases-server/src/main/java/org/allGraphQLCases/server/impl/DataFetchersDelegateMyQueryTypeImpl.java

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,25 @@
77

88
import javax.annotation.Resource;
99

10-
import org.allGraphQLCases.server.*;
11-
import org.allGraphQLCases.server.STP_AllFieldCases_STS;
12-
import org.allGraphQLCases.server.SUP_AnyCharacter_SUS;
10+
import org.allGraphQLCases.server.SEP_EnumWithReservedJavaKeywordAsValues_SES;
11+
import org.allGraphQLCases.server.SEP_Episode_SES;
12+
import org.allGraphQLCases.server.SEP_extends_SES;
13+
import org.allGraphQLCases.server.SINP_AllFieldCasesInput_SINS;
1314
import org.allGraphQLCases.server.SINP_CharacterInput_SINS;
1415
import org.allGraphQLCases.server.SINP_DroidInput_SINS;
15-
import org.allGraphQLCases.server.SEP_EnumWithReservedJavaKeywordAsValues_SES;
16-
import org.allGraphQLCases.server.STP_Foo140_STS;
1716
import org.allGraphQLCases.server.SINP_HumanInput_SINS;
17+
import org.allGraphQLCases.server.SIP_CharacterConnection_SIS;
18+
import org.allGraphQLCases.server.SIP_Character_SIS;
19+
import org.allGraphQLCases.server.SIP_Client_SIS;
20+
import org.allGraphQLCases.server.STP_AllFieldCases_STS;
21+
import org.allGraphQLCases.server.STP_Droid_STS;
22+
import org.allGraphQLCases.server.STP_Foo140_STS;
23+
import org.allGraphQLCases.server.STP_HumanConnection_STS;
24+
import org.allGraphQLCases.server.STP_Human_STS;
1825
import org.allGraphQLCases.server.STP_MyQueryType_STS;
26+
import org.allGraphQLCases.server.STP_ReservedJavaKeywordAllFieldCases_STS;
27+
import org.allGraphQLCases.server.STP_break_STS;
28+
import org.allGraphQLCases.server.SUP_AnyCharacter_SUS;
1929
import org.allGraphQLCases.server.util.DataFetchersDelegateMyQueryType;
2030
import org.springframework.stereotype.Component;
2131

@@ -48,7 +58,8 @@ public List<SIP_Character_SIS> withoutParameters(DataFetchingEnvironment dataFet
4858
}
4959

5060
@Override
51-
public SIP_Character_SIS withOneOptionalParam(DataFetchingEnvironment dataFetchingEnvironment, SINP_CharacterInput_SINS character) {
61+
public SIP_Character_SIS withOneOptionalParam(DataFetchingEnvironment dataFetchingEnvironment,
62+
SINP_CharacterInput_SINS character) {
5263
if (character == null) {
5364
return generator.generateInstance(STP_Human_STS.class);
5465
} else {
@@ -67,25 +78,30 @@ public SIP_Character_SIS withOneOptionalParam(DataFetchingEnvironment dataFetchi
6778
}
6879

6980
@Override
70-
public SIP_Character_SIS withOneMandatoryParam(DataFetchingEnvironment dataFetchingEnvironment, SINP_CharacterInput_SINS character) {
81+
public SIP_Character_SIS withOneMandatoryParam(DataFetchingEnvironment dataFetchingEnvironment,
82+
SINP_CharacterInput_SINS character) {
7183
SIP_Character_SIS c = mapper.map(character, getClassFromName(SIP_Character_SIS.class, character.getType()));
7284
c.setId(UUID.randomUUID());
7385
return c;
7486
}
7587

7688
@Override
77-
public SIP_Character_SIS withEnum(DataFetchingEnvironment dataFetchingEnvironment, SEP_Episode_SES SEP_Episode_SES) {
89+
public SIP_Character_SIS withEnum(DataFetchingEnvironment dataFetchingEnvironment,
90+
SEP_Episode_SES SEP_Episode_SES) {
7891
SIP_Character_SIS c = generator.generateInstance(STP_Droid_STS.class);
7992

80-
// The SEP_Episode_SES list (appearsIn) will be filled by another call (the graphql manages the joins).
81-
// To check the given parameter, we put the SEP_Episode_SES name in the returned character's name
93+
// The SEP_Episode_SES list (appearsIn) will be filled by another call (the
94+
// graphql manages the joins).
95+
// To check the given parameter, we put the SEP_Episode_SES name in the returned
96+
// character's name
8297
c.setName(SEP_Episode_SES.name());
8398

8499
return c;
85100
}
86101

87102
@Override
88-
public STP_AllFieldCases_STS withListOfList(DataFetchingEnvironment dataFetchingEnvironment, List<List<Double>> matrix) {
103+
public STP_AllFieldCases_STS withListOfList(DataFetchingEnvironment dataFetchingEnvironment,
104+
List<List<Double>> matrix) {
89105
STP_AllFieldCases_STS ret = new STP_AllFieldCases_STS();
90106
ret.setMatrix(matrix);
91107
return ret;
@@ -128,7 +144,8 @@ public SIP_Character_SIS error(DataFetchingEnvironment dataFetchingEnvironment,
128144
}
129145

130146
@Override
131-
public STP_AllFieldCases_STS allFieldCases(DataFetchingEnvironment dataFetchingEnvironment, SINP_AllFieldCasesInput_SINS input) {
147+
public STP_AllFieldCases_STS allFieldCases(DataFetchingEnvironment dataFetchingEnvironment,
148+
SINP_AllFieldCasesInput_SINS input) {
132149
STP_AllFieldCases_STS ret;
133150
if (input != null) {
134151
ret = mapper.map(input, STP_AllFieldCases_STS.class);
@@ -142,7 +159,8 @@ public STP_AllFieldCases_STS allFieldCases(DataFetchingEnvironment dataFetchingE
142159
public STP_break_STS aBreak(DataFetchingEnvironment dataFetchingEnvironment) {
143160
STP_break_STS ret = new STP_break_STS();
144161

145-
// Let's retrieve the input parameter test, that contains the expected value to return
162+
// Let's retrieve the input parameter test, that contains the expected value to
163+
// return
146164
Field aBreak = (Field) dataFetchingEnvironment.getOperationDefinition().getSelectionSet().getSelections()
147165
.get(0);
148166
Field aCase = (Field) aBreak.getSelectionSet().getSelections().get(0);
@@ -237,8 +255,9 @@ public SIP_Character_SIS directiveOnField(DataFetchingEnvironment dataFetchingEn
237255
}
238256

239257
@Override
240-
public List<SUP_AnyCharacter_SUS> unionTest(DataFetchingEnvironment dataFetchingEnvironment, SINP_HumanInput_SINS human1,
241-
SINP_HumanInput_SINS human2, SINP_DroidInput_SINS droid1, SINP_DroidInput_SINS droid2) {
258+
public List<SUP_AnyCharacter_SUS> unionTest(DataFetchingEnvironment dataFetchingEnvironment,
259+
SINP_HumanInput_SINS human1, SINP_HumanInput_SINS human2, SINP_DroidInput_SINS droid1,
260+
SINP_DroidInput_SINS droid2) {
242261
List<SUP_AnyCharacter_SUS> ret = new ArrayList<>();
243262

244263
if (human1 != null) {
@@ -277,8 +296,8 @@ public Integer withOneMandatoryParamDefaultValue(DataFetchingEnvironment dataFet
277296
}
278297

279298
@Override
280-
public STP_Droid_STS withTwoMandatoryParamDefaultVal(DataFetchingEnvironment dataFetchingEnvironment, SINP_DroidInput_SINS theHero,
281-
Integer num) {
299+
public STP_Droid_STS withTwoMandatoryParamDefaultVal(DataFetchingEnvironment dataFetchingEnvironment,
300+
SINP_DroidInput_SINS theHero, Integer num) {
282301
// TODO Auto-generated method stub
283302
return null;
284303
}
@@ -491,4 +510,10 @@ public String _while(DataFetchingEnvironment dataFetchingEnvironment) {
491510
return "a value for _while";
492511
}
493512

513+
@Override
514+
public STP_ReservedJavaKeywordAllFieldCases_STS reservedJavaKeywordAllFieldCases(
515+
DataFetchingEnvironment dataFetchingEnvironment) {
516+
return generator.generateInstance(STP_ReservedJavaKeywordAllFieldCases_STS.class);
517+
}
518+
494519
}

0 commit comments

Comments
 (0)