Skip to content

Commit 3cb504b

Browse files
committed
Support for strongly typed variable placeholders in GraphQL tagged templates (#130)
1 parent e1bbc72 commit 3cb504b

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/main/com/intellij/lang/jsgraphql/ide/injection/JSGraphQLLanguageInjectionUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import com.google.common.collect.Sets;
1111
import com.intellij.ide.util.PropertiesComponent;
12+
import com.intellij.lang.javascript.psi.JSExpression;
1213
import com.intellij.lang.javascript.psi.JSFile;
1314
import com.intellij.lang.javascript.psi.JSReferenceExpression;
1415
import 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";
@@ -100,6 +102,14 @@ public static boolean isJSGraphQLLanguageInjectionTarget(PsiElement host, @Nulla
100102
}
101103
return true;
102104
}
105+
final String builderTailName = tagExpression.getReferenceName();
106+
if(builderTailName != null && SUPPORTED_TAG_NAMES.contains(builderTailName)) {
107+
// a builder pattern that ends in a tagged template, e.g. someQueryAPI.graphql``
108+
if(envRef != null) {
109+
envRef.set(getEnvironmentFromTemplateTag(builderTailName, host));
110+
}
111+
return true;
112+
}
103113
}
104114
}
105115
return false;
@@ -110,6 +120,13 @@ public static String getEnvironmentFromTemplateTag(String tagText, PsiElement ho
110120
return RELAY_ENVIRONMENT;
111121
}
112122
if (GRAPHQL_TEMPLATE_TAG.equals(tagText) || GRAPHQL_EXPERIMENTAL_TEMPLATE_TAG.equals(tagText)) {
123+
if(host instanceof JSStringTemplateExpression) {
124+
final JSExpression[] arguments = ((JSStringTemplateExpression) host).getArguments();
125+
if(arguments.length > 0) {
126+
// one or more placeholders inside the tagged template text, so consider it templated graphql
127+
return GRAPHQL_TEMPLATE_ENVIRONMENT;
128+
}
129+
}
113130
return GRAPHQL_ENVIRONMENT;
114131
}
115132
if (GQL_TEMPLATE_TAG.equals(tagText)) {

0 commit comments

Comments
 (0)