1
1
package nl .jrdie .idea .springql .svc ;
2
2
3
3
import com .intellij .lang .java .JavaLanguage ;
4
- import com .intellij .lang .jsgraphql .schema .GraphQLSchemaChangeTracker ;
5
4
import com .intellij .lang .jsgraphql .schema .GraphQLSchemaInfo ;
6
5
import com .intellij .lang .jsgraphql .schema .GraphQLSchemaProvider ;
7
- import com .intellij .lang .jsgraphql .schema .library .GraphQLLibraryTypes ;
8
6
import com .intellij .lang .jsgraphql .types .language .FieldDefinition ;
9
7
import com .intellij .lang .jsgraphql .types .language .InputValueDefinition ;
10
8
import com .intellij .lang .jsgraphql .types .language .Node ;
26
24
import com .intellij .psi .stubs .StubIndex ;
27
25
import com .intellij .psi .stubs .StubIndexKey ;
28
26
import com .intellij .psi .util .PsiModificationTracker ;
29
- import java .text .NumberFormat ;
30
- import java .time .Duration ;
31
- import java .util .Collections ;
32
- import java .util .List ;
33
- import java .util .Locale ;
34
- import java .util .Objects ;
35
- import java .util .Set ;
36
- import java .util .concurrent .atomic .AtomicLong ;
37
- import java .util .concurrent .atomic .AtomicReference ;
38
- import java .util .stream .Collectors ;
27
+ import com .intellij .util .messages .Topic ;
28
+ import nl .jrdie .idea .springql .GraphQLSchemaEventListener ;
39
29
import nl .jrdie .idea .springql .index .MutableQLIdeIndex ;
40
30
import nl .jrdie .idea .springql .index .QLIdeIndex ;
41
31
import nl .jrdie .idea .springql .index .entry .QLClassSchemaMappingIndexEntry ;
42
32
import nl .jrdie .idea .springql .index .entry .QLMethodSchemaMappingIndexEntry ;
43
33
import nl .jrdie .idea .springql .index .processor .QLAnnotationIndexProcessor ;
44
34
import nl .jrdie .idea .springql .types .SchemaMappingSummary ;
35
+ import nl .jrdie .idea .springql .utils .JSGraphQLPlugin ;
36
+ import nl .jrdie .idea .springql .utils .JSGraphQLVersionBypassUtils ;
45
37
import nl .jrdie .idea .springql .utils .QLIdeUtil ;
46
38
import nl .jrdie .idea .springql .utils .UExtKt ;
47
39
import org .jetbrains .annotations .NotNull ;
55
47
import org .jetbrains .uast .UMethod ;
56
48
import org .jetbrains .uast .UastContextKt ;
57
49
50
+ import java .text .NumberFormat ;
51
+ import java .time .Duration ;
52
+ import java .util .Collections ;
53
+ import java .util .List ;
54
+ import java .util .Locale ;
55
+ import java .util .Objects ;
56
+ import java .util .Set ;
57
+ import java .util .concurrent .atomic .AtomicLong ;
58
+ import java .util .concurrent .atomic .AtomicReference ;
59
+ import java .util .stream .Collectors ;
60
+
58
61
public class QLIdeServiceImpl implements QLIdeService , Disposable {
59
62
60
63
private static final Logger LOGGER = Logger .getInstance (QLIdeServiceImpl .class );
@@ -83,7 +86,7 @@ public class QLIdeServiceImpl implements QLIdeService, Disposable {
83
86
84
87
@ NotNull private final Project project ;
85
88
86
- @ Nullable private QLSchemaRegistry cachedSchemaRegistry ;
89
+ // TODO @Nullable private QLSchemaRegistry cachedSchemaRegistry;
87
90
88
91
@ Nullable private QLIdeIndex cachedIdeIndex ;
89
92
@@ -98,6 +101,50 @@ public QLIdeServiceImpl(Project project) {
98
101
this .javaModificationCount = new AtomicLong (0 );
99
102
this .kotlinModificationCount = new AtomicLong (0 );
100
103
this .graphQLModificationCount = new AtomicLong (0 );
104
+
105
+ tryRegisterGraphQLSchemaChangeListener ();
106
+ }
107
+
108
+ @ SuppressWarnings ({"unchecked" , "JavaReflectionMemberAccess" })
109
+ private void tryRegisterGraphQLSchemaChangeListener () {
110
+ // Only implement this for the 2020.3 compatible JS GraphQL version (3.0.0-2020.3)
111
+ if (!JSGraphQLPlugin .INSTANCE .is2020dot3version ()) {
112
+ return ;
113
+ }
114
+
115
+ // ----------------------------------------
116
+ // I DISGUST EVERYTHING ABOUT THIS FUNCTION
117
+ // ----------------------------------------
118
+
119
+ // https://github.com/jimkyndemeyer/js-graphql-intellij-plugin/blob/3.0.0/src/main/com/intellij/lang/jsgraphql/schema/GraphQLSchemaChangeListener.java
120
+ // com.intellij.lang.jsgraphql.schema
121
+ try {
122
+ Class <?> graphQLSchemaChangeListenerCls =
123
+ Class .forName ("com.intellij.lang.jsgraphql.schema.GraphQLSchemaChangeListener" );
124
+ Topic <? super GraphQLSchemaEventListener > targetTopic =
125
+ (Topic <? super GraphQLSchemaEventListener >)
126
+ graphQLSchemaChangeListenerCls .getField ("TOPIC" ).get (null );
127
+
128
+ if (targetTopic == null ) {
129
+ throw new IllegalStateException ("TOPIC Not found" ); // TODO Error message
130
+ }
131
+
132
+ // Automatically disposed when `this` service is disposed (we implement Disposable)
133
+ project
134
+ .getMessageBus ()
135
+ .connect (this )
136
+ .subscribe (
137
+ targetTopic ,
138
+ (GraphQLSchemaEventListener )
139
+ schemaVersion -> {
140
+ // Force reloading of the index when a schema file changes
141
+ QLIdeServiceImpl .this .getIndex (true );
142
+ });
143
+ } catch (Exception e ) {
144
+ throw new IllegalStateException (
145
+ "Failed to register schema change listener for 2020.3 JS GraphQL plugin version @ GraphQL Spring Support" ,
146
+ e );
147
+ }
101
148
}
102
149
103
150
@ NotNull
@@ -311,7 +358,8 @@ private boolean hasFieldWithNameAndType(
311
358
312
359
@ Override
313
360
public boolean isApolloFederationSupportEnabled () {
314
- return GraphQLLibraryTypes .FEDERATION .isEnabled (this .project );
361
+ return !JSGraphQLPlugin .INSTANCE .is2020dot3version ()
362
+ && JSGraphQLVersionBypassUtils .isApolloFederationEnabled (this .project );
315
363
}
316
364
317
365
@ Nullable
@@ -469,10 +517,8 @@ public List<UAnnotation> findNearestSchemaMappingAnnotations(UElement uElement)
469
517
@ NotNull
470
518
@ Override
471
519
public QLSchemaRegistry getSchemaRegistry () {
520
+ // TODO Cache registry?
472
521
return new QLSchemaRegistry (getTypeDefinitionRegistry (), getGraphQLSchemaInfo ());
473
- // return Objects.requireNonNullElseGet(cachedSchemaRegistry,
474
- // () -> cachedSchemaRegistry = new
475
- // QLSchemaRegistry(getTypeDefinitionRegistry()));
476
522
}
477
523
478
524
@ NotNull
@@ -523,14 +569,40 @@ private boolean updateAndCheckModified() {
523
569
boolean kotlinModified =
524
570
this .kotlinModificationCount .getAndSet (kotlinModificationCount ) != kotlinModificationCount ;
525
571
526
- long graphQLModificationCount =
527
- GraphQLSchemaChangeTracker .getInstance (this .project )
528
- .getSchemaModificationTracker ()
529
- .getModificationCount ();
530
-
531
- boolean graphQLModified =
532
- this .graphQLModificationCount .getAndSet (graphQLModificationCount )
533
- != graphQLModificationCount ;
572
+ boolean graphQLModified = false ;
573
+
574
+ // TODO Fix this 2020.3 support logic
575
+ if (!JSGraphQLPlugin .INSTANCE .is2020dot3version ()) {
576
+ //noinspection CommentedOutCode
577
+ try {
578
+ Class <?> changeTracker =
579
+ Class .forName ("com.intellij.lang.jsgraphql.schema.GraphQLSchemaChangeTracker" );
580
+ Object schemaTracker =
581
+ changeTracker .getMethod ("getInstance" , Project .class ).invoke (null , this .project );
582
+ Object modificationTracker =
583
+ schemaTracker
584
+ .getClass ()
585
+ .getMethod ("getSchemaModificationTracker" )
586
+ .invoke (schemaTracker );
587
+ long graphQLModificationCount =
588
+ (long )
589
+ schemaTracker
590
+ .getClass ()
591
+ .getMethod ("getModificationCount" )
592
+ .invoke (modificationTracker );
593
+
594
+ // long graphQLModificationCount =
595
+ // GraphQLSchemaChangeTracker.getInstance(this.project)
596
+ // .getSchemaModificationTracker()
597
+ // .getModificationCount();
598
+
599
+ graphQLModified =
600
+ this .graphQLModificationCount .getAndSet (graphQLModificationCount )
601
+ != graphQLModificationCount ;
602
+ } catch (Exception e ) {
603
+ throw new RuntimeException (e );
604
+ }
605
+ }
534
606
535
607
return javaModified || kotlinModified || graphQLModified ;
536
608
}
0 commit comments