99
1010import com .google .common .collect .Sets ;
1111import com .intellij .ide .util .PropertiesComponent ;
12+ import com .intellij .lang .javascript .psi .JSExpression ;
1213import com .intellij .lang .javascript .psi .JSFile ;
1314import com .intellij .lang .javascript .psi .JSReferenceExpression ;
1415import com .intellij .lang .javascript .psi .ecma6 .JSStringTemplateExpression ;
@@ -42,6 +43,7 @@ public class JSGraphQLLanguageInjectionUtil {
4243
4344
4445 public static final String GRAPHQL_ENVIRONMENT = "graphql" ;
46+ public static final String GRAPHQL_TEMPLATE_ENVIRONMENT = "graphql-template" ;
4547 public static final String RELAY_ENVIRONMENT = "relay" ;
4648 public static final String APOLLO_ENVIRONMENT = "apollo" ;
4749 public static final String LOKKA_ENVIRONMENT = "lokka" ;
@@ -93,8 +95,14 @@ public static boolean isJSGraphQLLanguageInjectionTarget(PsiElement host, @Nulla
9395 tagExpression = (JSReferenceExpression ) template .getPrevSibling ();
9496 }
9597 if (tagExpression != null ) {
96- final String tagText = tagExpression .getText ();
97- if (SUPPORTED_TAG_NAMES .contains (tagText )) {
98+ String tagText = tagExpression .getText ();
99+ boolean isSupportedTag = SUPPORTED_TAG_NAMES .contains (tagText );
100+ if (!isSupportedTag && tagExpression .getQualifier () != null && tagExpression .getReferenceNameElement () != null ) {
101+ // also allow builder patterns to trigger injection, e.g. 'builder.foo.bar.graphql``'
102+ tagText = tagExpression .getReferenceNameElement ().getText ();
103+ isSupportedTag = SUPPORTED_TAG_NAMES .contains (tagText );
104+ }
105+ if (isSupportedTag ) {
98106 if (envRef != null ) {
99107 envRef .set (getEnvironmentFromTemplateTag (tagText , host ));
100108 }
@@ -110,6 +118,13 @@ public static String getEnvironmentFromTemplateTag(String tagText, PsiElement ho
110118 return RELAY_ENVIRONMENT ;
111119 }
112120 if (GRAPHQL_TEMPLATE_TAG .equals (tagText ) || GRAPHQL_EXPERIMENTAL_TEMPLATE_TAG .equals (tagText )) {
121+ if (host instanceof JSStringTemplateExpression ) {
122+ final JSExpression [] arguments = ((JSStringTemplateExpression ) host ).getArguments ();
123+ if (arguments .length > 0 ) {
124+ // one or more placeholders inside the tagged template text, so consider it templated graphql
125+ return GRAPHQL_TEMPLATE_ENVIRONMENT ;
126+ }
127+ }
113128 return GRAPHQL_ENVIRONMENT ;
114129 }
115130 if (GQL_TEMPLATE_TAG .equals (tagText )) {
0 commit comments