1111import com .google .common .collect .Maps ;
1212import com .google .common .collect .Sets ;
1313import com .intellij .lang .jsgraphql .endpoint .psi .*;
14+ import com .intellij .lang .jsgraphql .ide .project .graphqlconfig .GraphQLConfigManager ;
15+ import com .intellij .lang .jsgraphql .ide .project .graphqlconfig .GraphQLNamedScope ;
1416import com .intellij .lang .jsgraphql .schema .GraphQLSchemaChangeListener ;
1517import com .intellij .lang .jsgraphql .schema .GraphQLSchemaEventListener ;
1618import com .intellij .lang .jsgraphql .schema .TypeDefinitionRegistryWithErrors ;
4042public class JSGraphQLEndpointNamedTypeRegistry implements JSGraphQLNamedTypeRegistry {
4143
4244 private final JSGraphQLConfigurationProvider configurationProvider ;
45+ private final GraphQLConfigManager graphQLConfigManager ;
4346 private final Project project ;
4447
45- private final Map <Project , Map <String , JSGraphQLNamedType >> endpointTypesByName = Maps .newConcurrentMap ();
46- private final Map <Project , PsiFile > endpointEntryPsiFile = Maps .newConcurrentMap ();
47- private final Map <Project , TypeDefinitionRegistryWithErrors > projectToRegistry = Maps .newConcurrentMap ();
48+ private final Map <GraphQLNamedScope , Map <String , JSGraphQLNamedType >> endpointTypesByName = Maps .newConcurrentMap ();
49+ private final Map <GraphQLNamedScope , PsiFile > endpointEntryPsiFile = Maps .newConcurrentMap ();
50+ private final Map <GraphQLNamedScope , TypeDefinitionRegistryWithErrors > projectToRegistry = Maps .newConcurrentMap ();
4851
4952 public static JSGraphQLEndpointNamedTypeRegistry getService (@ NotNull Project project ) {
5053 return ServiceManager .getService (project , JSGraphQLEndpointNamedTypeRegistry .class );
@@ -53,6 +56,7 @@ public static JSGraphQLEndpointNamedTypeRegistry getService(@NotNull Project pro
5356 public JSGraphQLEndpointNamedTypeRegistry (Project project ) {
5457 this .project = project ;
5558 this .configurationProvider = JSGraphQLConfigurationProvider .getService (project );
59+ graphQLConfigManager = GraphQLConfigManager .getService (project );
5660 project .getMessageBus ().connect ().subscribe (GraphQLSchemaChangeListener .TOPIC , new GraphQLSchemaEventListener () {
5761 @ Override
5862 public void onGraphQLSchemaChanged () {
@@ -63,13 +67,17 @@ public void onGraphQLSchemaChanged() {
6367 });
6468 }
6569
66- public boolean hasEndpointEntryFile () {
67- return getEndpointEntryPsiFile () != null ;
70+ public boolean hasEndpointEntryFile (PsiElement scopedPsiElement ) {
71+ return getEndpointEntryPsiFile (scopedPsiElement ) != null ;
6872 }
6973
70- private PsiFile getEndpointEntryPsiFile () {
71- return endpointEntryPsiFile .computeIfAbsent (project , p -> {
72- final VirtualFile endpointEntryFile = configurationProvider .getEndpointEntryFile ();
74+ private PsiFile getEndpointEntryPsiFile (PsiElement scopedPsiElement ) {
75+ final GraphQLNamedScope schemaScope = graphQLConfigManager .getSchemaScope (scopedPsiElement .getContainingFile ().getVirtualFile ());
76+ if (schemaScope == null ) {
77+ return null ;
78+ }
79+ return endpointEntryPsiFile .computeIfAbsent (schemaScope , p -> {
80+ final VirtualFile endpointEntryFile = configurationProvider .getEndpointEntryFile (scopedPsiElement .getContainingFile ());
7381 if (endpointEntryFile != null ) {
7482 return PsiManager .getInstance (project ).findFile (endpointEntryFile );
7583 }
@@ -78,25 +86,29 @@ private PsiFile getEndpointEntryPsiFile() {
7886 }
7987
8088 @ Override
81- public JSGraphQLNamedType getNamedType (String typeNameToGet ) {
82- return computeNamedTypes ().get (typeNameToGet );
89+ public JSGraphQLNamedType getNamedType (String typeNameToGet , PsiElement scopedElement ) {
90+ return computeNamedTypes (scopedElement ).get (typeNameToGet );
8391 }
8492
85- public void enumerateTypes (Consumer <JSGraphQLNamedType > consumer ) {
86- computeNamedTypes ().forEach ((key , jsGraphQLNamedType ) -> consumer .accept (jsGraphQLNamedType ));
93+ public void enumerateTypes (PsiElement scopedElement , Consumer <JSGraphQLNamedType > consumer ) {
94+ computeNamedTypes (scopedElement ).forEach ((key , jsGraphQLNamedType ) -> consumer .accept (jsGraphQLNamedType ));
8795 }
8896
89- public TypeDefinitionRegistryWithErrors getTypesAsRegistry () {
90- return projectToRegistry .computeIfAbsent (project , p -> doGetTypesAsRegistry ());
97+ public TypeDefinitionRegistryWithErrors getTypesAsRegistry (PsiElement scopedElement ) {
98+ final GraphQLNamedScope schemaScope = graphQLConfigManager .getSchemaScope (scopedElement .getContainingFile ().getVirtualFile ());
99+ if (schemaScope == null ) {
100+ return new TypeDefinitionRegistryWithErrors (new TypeDefinitionRegistry (), Collections .emptyList ());
101+ }
102+ return projectToRegistry .computeIfAbsent (schemaScope , p -> doGetTypesAsRegistry (scopedElement ));
91103 }
92104
93- private TypeDefinitionRegistryWithErrors doGetTypesAsRegistry () {
105+ private TypeDefinitionRegistryWithErrors doGetTypesAsRegistry (PsiElement scopedElement ) {
94106
95107 final TypeDefinitionRegistry registry = new TypeDefinitionRegistry ();
96108 final List <GraphQLException > errors = Lists .newArrayList ();
97109 final TypeDefinitionRegistryWithErrors registryWithErrors = new TypeDefinitionRegistryWithErrors (registry , errors );
98110
99- final Map <String , JSGraphQLNamedType > namedTypes = computeNamedTypes ();
111+ final Map <String , JSGraphQLNamedType > namedTypes = computeNamedTypes (scopedElement );
100112
101113 final PsiRecursiveElementVisitor errorsVisitor = new PsiRecursiveElementVisitor () {
102114 @ Override
@@ -353,10 +365,14 @@ private SourceLocation getSourceLocation(PsiElement psiSourceElement) {
353365 return new SourceLocation (-1 , -1 , psiSourceElement .getContainingFile ().getName ());
354366 }
355367
356- private Map <String , JSGraphQLNamedType > computeNamedTypes () {
357- return endpointTypesByName .computeIfAbsent (project , p -> {
368+ private Map <String , JSGraphQLNamedType > computeNamedTypes (PsiElement scopedPsiElement ) {
369+ final GraphQLNamedScope schemaScope = graphQLConfigManager .getSchemaScope (scopedPsiElement .getContainingFile ().getVirtualFile ());
370+ if (schemaScope == null ) {
371+ return Collections .emptyMap ();
372+ }
373+ return endpointTypesByName .computeIfAbsent (schemaScope , p -> {
358374 final Map <String , JSGraphQLNamedType > result = Maps .newConcurrentMap ();
359- final PsiFile entryPsiFile = getEndpointEntryPsiFile ();
375+ final PsiFile entryPsiFile = getEndpointEntryPsiFile (scopedPsiElement );
360376 if (entryPsiFile != null ) {
361377 Collection <JSGraphQLEndpointNamedTypeDefinition > endpointNamedTypeDefinitions = JSGraphQLEndpointPsiUtil .getKnownDefinitions (
362378 entryPsiFile ,
0 commit comments