Skip to content

Commit 2accf56

Browse files
committed
GED202204 Adding federation base to allow providers without runtime support
1 parent 70504cb commit 2accf56

File tree

5 files changed

+70
-15
lines changed

5 files changed

+70
-15
lines changed

src/main/java/com/intuit/graphql/orchestrator/ServiceProvider.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ default ServiceType getSeviceType() {
3434
return ServiceType.GRAPHQL;
3535
}
3636

37+
default boolean isFederationProvider() { return getSeviceType() == ServiceType.FEDERATION_SUBGRAPH; }
38+
3739
enum ServiceType {
40+
FEDERATION_SUBGRAPH,
3841
GRAPHQL,
3942
REST
4043
}

src/main/java/com/intuit/graphql/orchestrator/stitching/XtextStitcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private Map<String, BatchLoader> getBatchLoaders(Map<String, XtextGraph> xtextGr
142142

143143
HashMap<String, BatchLoader> batchLoaderMap = new HashMap<>();
144144
xtextGraphMap.forEach((namespace, graph) -> {
145-
if (graph.getServiceProvider().getSeviceType() == ServiceType.GRAPHQL) {
145+
if (graph.getServiceProvider().getSeviceType() == ServiceType.GRAPHQL || graph.getServiceProvider().isFederationProvider()) {
146146
batchLoaderMap.put(namespace,
147147
GraphQLServiceBatchLoader
148148
.newQueryExecutorBatchLoader()

src/main/java/com/intuit/graphql/orchestrator/xtext/XtextGraphBuilder.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,34 @@
44
import com.intuit.graphql.orchestrator.ServiceProvider;
55
import com.intuit.graphql.orchestrator.schema.Operation;
66
import com.intuit.graphql.orchestrator.utils.XtextUtils;
7+
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Paths;
711
import java.util.EnumMap;
812
import java.util.Map;
13+
14+
import lombok.extern.slf4j.Slf4j;
15+
import org.apache.commons.io.IOUtils;
916
import org.eclipse.xtext.resource.XtextResourceSet;
1017

1118
public class XtextGraphBuilder {
1219

1320
public static XtextGraph build(ServiceProvider serviceProvider) {
14-
1521
XtextResourceSet xtextResourceSet = XtextResourceSetBuilder.newBuilder()
16-
.files(serviceProvider.sdlFiles())
17-
.build();
22+
.files(serviceProvider.sdlFiles())
23+
.isFederatedResourceSet(serviceProvider.isFederationProvider())
24+
.build();
1825

1926
final Map<Operation, ObjectTypeDefinition> operationMap = new EnumMap<>(Operation.class);
2027
for (Operation operation : Operation.values()) {
2128
XtextUtils.findOperationType(operation, xtextResourceSet)
22-
.ifPresent(operationType -> operationMap.put(operation, operationType));
29+
.ifPresent(operationType -> operationMap.put(operation, operationType));
2330
}
2431

2532
return XtextGraph.newBuilder().xtextResourceSet(xtextResourceSet)
26-
.serviceProvider(serviceProvider)
27-
.operationMap(operationMap).build();
33+
.serviceProvider(serviceProvider)
34+
.operationMap(operationMap).build();
2835
}
2936

3037
}

src/main/java/com/intuit/graphql/orchestrator/xtext/XtextResourceSetBuilder.java

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,27 @@
77
import com.intuit.graphql.orchestrator.schema.SchemaParseException;
88
import java.io.IOException;
99
import java.io.InputStream;
10+
import java.nio.charset.Charset;
1011
import java.util.List;
1112
import java.util.Map;
1213
import java.util.concurrent.ConcurrentHashMap;
1314
import java.util.stream.Collectors;
15+
16+
import lombok.extern.slf4j.Slf4j;
1417
import org.apache.commons.io.IOUtils;
18+
import org.apache.commons.lang3.StringUtils;
1519
import org.eclipse.emf.common.util.URI;
1620
import org.eclipse.xtext.EcoreUtil2;
1721
import org.eclipse.xtext.resource.IResourceFactory;
1822
import org.eclipse.xtext.resource.XtextResource;
1923
import org.eclipse.xtext.resource.XtextResourceSet;
2024
import org.eclipse.xtext.util.CancelIndicator;
25+
import org.eclipse.xtext.util.Files;
2126
import org.eclipse.xtext.validation.CheckMode;
2227
import org.eclipse.xtext.validation.IResourceValidator;
2328
import org.eclipse.xtext.validation.Issue;
2429

30+
@Slf4j
2531
/**
2632
* The Xtext resource set builder.
2733
*/
@@ -30,6 +36,9 @@ public class XtextResourceSetBuilder {
3036
private XtextResourceSet graphqlResourceSet;
3137
private static Injector GRAPHQL_INJECTOR = new GraphQLStandaloneSetupGenerated().createInjectorAndDoEMFRegistration();
3238
private Map<String, String> files = new ConcurrentHashMap<>();
39+
private boolean isFederatedResourceSet = false;
40+
41+
public static final String FEDERATION_DIRECTIVES = getFederationDirectives();
3342

3443
private XtextResourceSetBuilder() {
3544
}
@@ -57,20 +66,36 @@ public XtextResourceSetBuilder file(String fileName, String file) {
5766
return this;
5867
}
5968

69+
public XtextResourceSetBuilder isFederatedResourceSet(boolean isFederatedResourceSet) {
70+
this.isFederatedResourceSet = isFederatedResourceSet;
71+
return this;
72+
}
73+
6074
/**
6175
* Build xtext resource set.
6276
*
6377
* @return the xtext resource set
6478
*/
6579
public XtextResourceSet build() {
6680
graphqlResourceSet = GRAPHQL_INJECTOR.getInstance(XtextResourceSet.class);
67-
files.forEach((fileName, content) -> {
81+
82+
if(isFederatedResourceSet) {
83+
String content = FEDERATION_DIRECTIVES + "\n" + StringUtils.join(files.values(), "\n");
84+
6885
try {
69-
createGraphqlResourceFromString(content, fileName);
86+
createGraphqlResourceFromString(content, "appended_federation");
7087
} catch (IOException e) {
71-
throw new SchemaParseException("Unable to parse file:" + fileName, e);
88+
throw new SchemaParseException("Unable to parse file: appended federation file", e);
7289
}
73-
});
90+
} else {
91+
files.forEach((fileName, content) -> {
92+
try {
93+
createGraphqlResourceFromString(content, fileName);
94+
} catch (IOException e) {
95+
throw new SchemaParseException("Unable to parse file:" + fileName, e);
96+
}
97+
});
98+
}
7499

75100
List<Issue> issues = validate();
76101
if (!issues.isEmpty()) {
@@ -100,8 +125,8 @@ private List<Issue> validate() {
100125
IResourceValidator validator = GRAPHQL_INJECTOR.getInstance(IResourceValidator.class);
101126
// collect issues
102127
return graphqlResourceSet.getResources().stream()
103-
.flatMap(resource -> validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl).stream()
104-
).collect(Collectors.toList());
128+
.flatMap(resource -> validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl).stream()
129+
).collect(Collectors.toList());
105130

106131
}
107132

@@ -116,7 +141,22 @@ public static XtextResourceSetBuilder newBuilder() {
116141

117142
public static XtextResourceSet singletonSet(String fileName, String file) {
118143
return newBuilder()
119-
.file(fileName, file)
120-
.build();
144+
.file(fileName, file)
145+
.build();
146+
}
147+
148+
private static String getFederationDirectives() {
149+
String directives = "";
150+
try {
151+
directives = IOUtils.toString(
152+
XtextResourceSetBuilder.class.getClassLoader()
153+
.getResourceAsStream( "federation_built_in_directives.graphqls"),
154+
Charset.defaultCharset()
155+
);
156+
} catch (IOException ex) {
157+
log.error("Failed to read resource");
158+
}
159+
160+
return directives;
121161
}
122162
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
directive @key(fields: String) on OBJECT | INTERFACE
2+
directive @extends on OBJECT | INTERFACE
3+
directive @external on FIELD_DEFINITION
4+
directive @provides(fields: String!) on FIELD_DEFINITION
5+
directive @requires(fields: String!) on FIELD_DEFINITION

0 commit comments

Comments
 (0)