Skip to content

Commit 2ebd09b

Browse files
committed
Upgraded graphql-java to 11.0 (#164)
- Various refactorings due to breaking changes and immutable AST nodes API - Ignored slf4j due to conflict with IntelliJ classpath
1 parent cedfc02 commit 2ebd09b

13 files changed

+234
-151
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ repositories {
2424
}
2525

2626
dependencies {
27-
compile ("com.graphql-java:graphql-java:9.0") {
27+
compile ("com.graphql-java:graphql-java:11.0") {
2828
exclude group: "org.reactivestreams", module: "reactive-streams"
2929
exclude group: "com.graphql-java", module: "java-dataloader"
30+
exclude group: "org.slf4j", module: "slf4j-api"
3031
}
3132
compile "commons-io:commons-io:2.6"
3233
compile "com.atlassian.commonmark:commonmark:0.12.1"

src/main/com/intellij/lang/jsgraphql/endpoint/ide/project/JSGraphQLEndpointNamedTypeRegistry.java

Lines changed: 63 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,13 @@ public void visitElement(PsiElement element) {
150150
} else {
151151
interfaces = Collections.emptyList();
152152
}
153-
ObjectTypeDefinition definition = withSourceLocation(new ObjectTypeDefinition(endpointType.getName(), interfaces, Collections.emptyList(), fieldDefinitions), typeDefinition);
154153

155-
if (typeDefinition.getNamedTypeDef() instanceof JSGraphQLEndpointDocumentationAware) {
156-
final String documentation = ((JSGraphQLEndpointDocumentationAware) typeDefinition.getNamedTypeDef()).getDocumentation(false);
157-
if (StringUtils.isNotBlank(documentation)) {
158-
definition.setDescription(new Description(documentation, definition.getSourceLocation(), true));
159-
}
160-
}
154+
final ObjectTypeDefinition.Builder builder = ObjectTypeDefinition.newObjectTypeDefinition();
155+
final SourceLocation sourceLocation = getSourceLocation(typeDefinition);
156+
final Description description = getDescription(typeDefinition, sourceLocation);
157+
builder.name(endpointType.getName()).implementz(interfaces).fieldDefinitions(fieldDefinitions).sourceLocation(sourceLocation).description(description);
161158

162-
registry.add(definition);
159+
registry.add(builder.build());
163160

164161
} else if (psiDefinition instanceof JSGraphQLEndpointInterfaceTypeDefinition) {
165162

@@ -173,16 +170,12 @@ public void visitElement(PsiElement element) {
173170
addFieldDefinition(fieldDefinitions, addedFieldNames, endpointFieldDefinition, errors);
174171
}
175172
}
176-
final InterfaceTypeDefinition definition = withSourceLocation(new InterfaceTypeDefinition(psiInterfaceDefinition.getNamedTypeDef().getName(), fieldDefinitions, Collections.emptyList()), psiDefinition);
177-
178-
if (psiInterfaceDefinition.getNamedTypeDef() instanceof JSGraphQLEndpointDocumentationAware) {
179-
final String documentation = ((JSGraphQLEndpointDocumentationAware) psiInterfaceDefinition.getNamedTypeDef()).getDocumentation(false);
180-
if (StringUtils.isNotBlank(documentation)) {
181-
definition.setDescription(new Description(documentation, definition.getSourceLocation(), true));
182-
}
183-
}
184173

185-
registry.add(definition);
174+
final InterfaceTypeDefinition.Builder builder = InterfaceTypeDefinition.newInterfaceTypeDefinition();
175+
final SourceLocation sourceLocation = getSourceLocation(psiDefinition);
176+
final Description description = getDescription(psiInterfaceDefinition, sourceLocation);
177+
builder.name(psiInterfaceDefinition.getNamedTypeDef().getName()).definitions(fieldDefinitions).sourceLocation(sourceLocation).description(description);
178+
registry.add(builder.build());
186179
}
187180

188181
} else if (psiDefinition instanceof JSGraphQLEndpointInputObjectTypeDefinition) {
@@ -193,11 +186,18 @@ public void visitElement(PsiElement element) {
193186
if (psiInputObjectDefinition.getFieldDefinitionSet() != null) {
194187
for (JSGraphQLEndpointFieldDefinition fieldDefinition : psiInputObjectDefinition.getFieldDefinitionSet().getFieldDefinitionList()) {
195188
if (fieldDefinition.getCompositeType() != null) {
196-
inputValueDefinitions.add(new InputValueDefinition(fieldDefinition.getProperty().getName(), createType(fieldDefinition.getCompositeType())));
189+
final InputValueDefinition inputValueDefinition = InputValueDefinition.newInputValueDefinition()
190+
.name(fieldDefinition.getProperty().getName())
191+
.type(createType(fieldDefinition.getCompositeType()))
192+
.build();
193+
inputValueDefinitions.add(inputValueDefinition);
197194
}
198195
}
199196
}
200-
registry.add(withSourceLocation(new InputObjectTypeDefinition(psiInputObjectDefinition.getNamedTypeDef().getName(), Collections.emptyList(), inputValueDefinitions), psiDefinition));
197+
final InputObjectTypeDefinition.Builder builder = InputObjectTypeDefinition.newInputObjectDefinition();
198+
final SourceLocation sourceLocation = getSourceLocation(psiDefinition);
199+
builder.name(psiInputObjectDefinition.getNamedTypeDef().getName()).inputValueDefinitions(inputValueDefinitions).sourceLocation(sourceLocation);
200+
registry.add(builder.build());
201201

202202
}
203203
} else if (psiDefinition instanceof JSGraphQLEndpointEnumTypeDefinition) {
@@ -210,7 +210,11 @@ public void visitElement(PsiElement element) {
210210
enumValueDefinitions.add(new EnumValueDefinition(psiEnumValueDefinition.getIdentifier().getText()));
211211
}
212212
}
213-
registry.add(withSourceLocation(new EnumTypeDefinition(psiEnumTypeDefinition.getNamedTypeDef().getName(), enumValueDefinitions, Collections.emptyList()), psiDefinition));
213+
final EnumTypeDefinition.Builder enumTypeDefinition = EnumTypeDefinition.newEnumTypeDefinition()
214+
.name(psiEnumTypeDefinition.getNamedTypeDef().getName())
215+
.enumValueDefinitions(enumValueDefinitions)
216+
.sourceLocation(getSourceLocation(psiDefinition));
217+
registry.add(enumTypeDefinition.build());
214218
}
215219

216220
} else if (psiDefinition instanceof JSGraphQLEndpointUnionTypeDefinition) {
@@ -224,7 +228,11 @@ public void visitElement(PsiElement element) {
224228
memberTypes.add(new TypeName(psiUnionMember.getIdentifier().getText()));
225229
}
226230
}
227-
registry.add(withSourceLocation(new UnionTypeDefinition(psiUnionTypeDefinition.getNamedTypeDef().getName(), Collections.emptyList(), memberTypes), psiDefinition));
231+
final UnionTypeDefinition.Builder builder = UnionTypeDefinition.newUnionTypeDefinition()
232+
.name(psiUnionTypeDefinition.getNamedTypeDef().getName())
233+
.memberTypes(memberTypes)
234+
.sourceLocation(getSourceLocation(psiDefinition));
235+
registry.add(builder.build());
228236

229237
}
230238

@@ -238,7 +246,12 @@ public void visitElement(PsiElement element) {
238246
for (Introspection.DirectiveLocation directiveLocation : Introspection.DirectiveLocation.values()) {
239247
directiveLocations.add(new DirectiveLocation(directiveLocation.name()));
240248
}
241-
registry.add(new DirectiveDefinition(psiAnnotationDefinition.getNamedTypeDef().getName(), inputValueDefinitions, directiveLocations));
249+
final DirectiveDefinition.Builder builder = DirectiveDefinition.newDirectiveDefinition()
250+
.name(psiAnnotationDefinition.getNamedTypeDef().getName())
251+
.inputValueDefinitions(inputValueDefinitions)
252+
.directiveLocations(directiveLocations)
253+
.sourceLocation(getSourceLocation(psiDefinition));
254+
registry.add(builder.build());
242255
}
243256

244257
}
@@ -247,22 +260,36 @@ public void visitElement(PsiElement element) {
247260
return registryWithErrors;
248261
}
249262

263+
private Description getDescription(JSGraphQLEndpointNamedTypeDefinition typeDefinition, SourceLocation sourceLocation) {
264+
if (typeDefinition.getNamedTypeDef() instanceof JSGraphQLEndpointDocumentationAware) {
265+
final String documentation = ((JSGraphQLEndpointDocumentationAware) typeDefinition.getNamedTypeDef()).getDocumentation(false);
266+
if (StringUtils.isNotBlank(documentation)) {
267+
return new Description(documentation, sourceLocation, true);
268+
}
269+
}
270+
return null;
271+
}
272+
250273
private void addFieldDefinition(List<FieldDefinition> fieldDefinitions, Set<String> addedFieldNames, JSGraphQLEndpointFieldDefinition endpointFieldDefinition, List<GraphQLException> errors) {
251274
final JSGraphQLEndpointProperty property = endpointFieldDefinition.getProperty();
252275
final String fieldName = property.getName();
253276
if (endpointFieldDefinition.getCompositeType() != null) {
254277
final Type fieldType = createType(endpointFieldDefinition.getCompositeType());
255278
if (fieldType != null) {
256279
if (addedFieldNames.add(fieldName)) {
257-
FieldDefinition fieldDefinition = new FieldDefinition(fieldName, fieldType, createInputValueDefinitions(endpointFieldDefinition.getArgumentsDefinition(), errors), Collections.emptyList());
258-
withSourceLocation(fieldDefinition, endpointFieldDefinition);
280+
final SourceLocation sourceLocation = getSourceLocation(endpointFieldDefinition);
281+
final FieldDefinition.Builder builder = FieldDefinition.newFieldDefinition()
282+
.name(fieldName)
283+
.type(fieldType)
284+
.inputValueDefinitions(createInputValueDefinitions(endpointFieldDefinition.getArgumentsDefinition(), errors))
285+
.sourceLocation(sourceLocation);
259286
if (property instanceof JSGraphQLEndpointDocumentationAware) {
260287
final String documentation = ((JSGraphQLEndpointDocumentationAware) property).getDocumentation(false);
261288
if (StringUtils.isNotBlank(documentation)) {
262-
fieldDefinition.setDescription(new Description(documentation, fieldDefinition.getSourceLocation(), true));
289+
builder.description(new Description(documentation, sourceLocation, true));
263290
}
264291
}
265-
fieldDefinitions.add(fieldDefinition);
292+
fieldDefinitions.add(builder.build());
266293
}
267294
} else {
268295
errors.add(new JSGraphQLEndpointSchemaError("Field '" + fieldName + "' has no valid output type", endpointFieldDefinition));
@@ -295,38 +322,35 @@ private List<InputValueDefinition> createInputValueDefinitions(JSGraphQLEndpoint
295322
}
296323

297324
private Type createType(JSGraphQLEndpointCompositeType endpointCompositeType) {
298-
final boolean isNomNull = endpointCompositeType.getText().contains("!");
325+
final boolean isNonNull = endpointCompositeType.getText().contains("!");
299326
if (endpointCompositeType.getListType() != null) {
300327
final JSGraphQLEndpointNamedType listElementType = endpointCompositeType.getListType().getNamedType();
301328
if (listElementType != null) {
302329
final String name = listElementType.getName();
303330
if (name != null) {
304-
Type type = withSourceLocation(new TypeName(name), listElementType);
305-
type = withSourceLocation(new ListType(type), endpointCompositeType);
306-
if (isNomNull) {
307-
type = withSourceLocation(new NonNullType(type), endpointCompositeType);
331+
Type type = TypeName.newTypeName(name).sourceLocation(getSourceLocation(listElementType)).build();
332+
type = ListType.newListType(type).sourceLocation(getSourceLocation(endpointCompositeType)).build();
333+
if (isNonNull) {
334+
type = NonNullType.newNonNullType(type).sourceLocation(getSourceLocation(endpointCompositeType)).build();
308335
}
309336
return type;
310337
}
311338
}
312339
} else if (endpointCompositeType.getNamedType() != null) {
313340
final String name = endpointCompositeType.getNamedType().getName();
314341
if (name != null) {
315-
Type type = withSourceLocation(new TypeName(name), endpointCompositeType.getNamedType());
316-
if (isNomNull) {
317-
type = withSourceLocation(new NonNullType(type), endpointCompositeType);
342+
Type type = TypeName.newTypeName(name).sourceLocation(getSourceLocation(endpointCompositeType.getNamedType())).build();
343+
if (isNonNull) {
344+
type = NonNullType.newNonNullType(type).sourceLocation(getSourceLocation(endpointCompositeType)).build();
318345
}
319346
return type;
320347
}
321348
}
322349
return null;
323350
}
324351

325-
private <T extends Node<?>> T withSourceLocation(T node, PsiElement psiSourceElement) {
326-
if (node instanceof AbstractNode) {
327-
((AbstractNode<?>) node).setSourceLocation(new SourceLocation(-1, -1, psiSourceElement.getContainingFile().getName()));
328-
}
329-
return node;
352+
private SourceLocation getSourceLocation(PsiElement psiSourceElement) {
353+
return new SourceLocation(-1, -1, psiSourceElement.getContainingFile().getName());
330354
}
331355

332356
private Map<String, JSGraphQLNamedType> computeNamedTypes() {

src/main/com/intellij/lang/jsgraphql/ide/GraphQLValidationAnnotator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder a
8686
typeScope = typeScopeProvider.getTypeScope();
8787
if (typeScope != null) {
8888
// unwrap non-nulls and lists for type and field hints
89-
typeScope = new SchemaUtil().getUnmodifiedType(typeScope);
89+
typeScope = GraphQLUtil.getUnmodifiedType(typeScope);
9090
}
9191
}
9292

@@ -195,21 +195,21 @@ public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder a
195195
}
196196
}
197197

198-
final Parser parser = new Parser();
199198
try {
200199
final GraphQLSchemaWithErrors schema = GraphQLTypeDefinitionRegistryServiceImpl.getService(project).getSchemaWithErrors(psiElement);
201200
if (!schema.isErrorsPresent()) {
202-
final Document document = parser.parseDocument(replacePlaceholdersWithValidGraphQL(containingFile));
203-
204201
// adjust source locations for injected GraphQL since the annotator works on the entire editor buffer (e.g. tsx with graphql tagged templates)
202+
int lineDelta = 0;
203+
int firsteLineColumDelta = 0;
205204
if (containingFile.getContext() != null) {
206205
final LogicalPosition logicalPosition = editor.offsetToLogicalPosition(containingFile.getContext().getTextOffset());
207206
if (logicalPosition.line > 0 || logicalPosition.column > 0) {
208207
// logical positions can be used as deltas between graphql-java and intellij since graphql-java is 1-based and intellij is 0-based
209-
GraphQLUtil.adjustSourceLocations(document, logicalPosition.line, logicalPosition.column);
208+
lineDelta = logicalPosition.line;
209+
firsteLineColumDelta = logicalPosition.column;
210210
}
211211
}
212-
212+
final Document document = GraphQLUtil.parseDocument(replacePlaceholdersWithValidGraphQL(containingFile), lineDelta, firsteLineColumDelta);
213213
userData = new Validator().validateDocument(schema.getSchema(), document);
214214
} else {
215215

0 commit comments

Comments
 (0)