This is an automated email from the ASF dual-hosted git repository.
bdelacretaz pushed a commit to branch master
in repository
https://gitbox.apache.org/repos/asf/sling-org-apache-sling-graphql-schema-aggregator.git
The following commit(s) were added to refs/heads/master by this push:
new b9d8322 SLING-10622 - always output a Query section
b9d8322 is described below
commit b9d8322e237012bc75e3b080835a63c4c10db7a3
Author: Bertrand Delacretaz <[email protected]>
AuthorDate: Fri Jul 16 13:04:27 2021 +0200
SLING-10622 - always output a Query section
---
.../aggregator/impl/DefaultSchemaAggregator.java | 26 ++++++++----
.../impl/DefaultSchemaAggregatorTest.java | 44 +++++++++++++++++----
src/test/resources/partials/mutation.only.txt | 6 +++
.../result-syntax-output-mutation-only.txt | 33 ++++++++++++++++
.../resources/partials/result-syntax-output.txt | 46 ++++++++++++++++++++++
.../{ => partials}/several-providers-output.txt | 0
6 files changed, 141 insertions(+), 14 deletions(-)
diff --git
a/src/main/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregator.java
b/src/main/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregator.java
index 0f83f41..6f8a8dd 100644
---
a/src/main/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregator.java
+++
b/src/main/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregator.java
@@ -42,6 +42,15 @@ public class DefaultSchemaAggregator implements
SchemaAggregator {
private static final Logger log =
LoggerFactory.getLogger(DefaultSchemaAggregator.class.getName());
public static final int MAX_REQUIREMENTS_RECURSION_LEVEL = 5;
+ /** Some sections like Query {} are surround by blocks in
+ * the output.
+ */
+ enum OutputMode {
+ NO_BLOCK,
+ WITH_BLOCK_IF_NOT_EMPTY,
+ WITH_BLOCK
+ };
+
@Reference
private ProviderBundleTracker tracker;
@@ -58,8 +67,8 @@ public class DefaultSchemaAggregator implements
SchemaAggregator {
}
}
- private void copySection(Set<Partial> selected, Partial.SectionName
sectionName, boolean inBlock, Writer target) throws IOException {
- String prefixToWrite = inBlock ? String.format("%ntype %s {%n",
capitalize(sectionName)) : null;
+ private void copySection(Set<Partial> selected, Partial.SectionName
sectionName, OutputMode mode, Writer target) throws IOException {
+ String prefixToWrite = (mode == OutputMode.NO_BLOCK) ? null :
String.format("%ntype %s {%n", capitalize(sectionName));
boolean anyOutput = false;
for(Partial p : selected) {
final Optional<Partial.Section> section =
p.getSection(sectionName);
@@ -71,9 +80,12 @@ public class DefaultSchemaAggregator implements
SchemaAggregator {
}
writeSourceInfo(target, p);
IOUtils.copy(section.get().getContent(), target);
+ } else if(mode == OutputMode.WITH_BLOCK && prefixToWrite != null) {
+ target.write(prefixToWrite);
+ prefixToWrite = null;
}
}
- if(anyOutput && inBlock) {
+ if( (anyOutput && mode == OutputMode.WITH_BLOCK_IF_NOT_EMPTY) || mode
== OutputMode.WITH_BLOCK) {
target.write(String.format("%n}%n"));
}
}
@@ -101,10 +113,10 @@ public class DefaultSchemaAggregator implements
SchemaAggregator {
}
// copy sections that belong in the output SDL
- copySection(selected, Partial.SectionName.PROLOGUE, false, target);
- copySection(selected, Partial.SectionName.QUERY, true, target);
- copySection(selected, Partial.SectionName.MUTATION, true, target);
- copySection(selected, Partial.SectionName.TYPES, false, target);
+ copySection(selected, Partial.SectionName.PROLOGUE,
OutputMode.NO_BLOCK, target);
+ copySection(selected, Partial.SectionName.QUERY,
OutputMode.WITH_BLOCK, target);
+ copySection(selected, Partial.SectionName.MUTATION,
OutputMode.WITH_BLOCK_IF_NOT_EMPTY, target);
+ copySection(selected, Partial.SectionName.TYPES, OutputMode.NO_BLOCK,
target);
final StringBuilder partialNames = new StringBuilder();
selected.forEach(p -> {
diff --git
a/src/test/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregatorTest.java
b/src/test/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregatorTest.java
index 2726ccf..cad4510 100644
---
a/src/test/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregatorTest.java
+++
b/src/test/java/org/apache/sling/graphql/schema/aggregator/impl/DefaultSchemaAggregatorTest.java
@@ -49,6 +49,14 @@ public class DefaultSchemaAggregatorTest {
private ProviderBundleTracker tracker;
private BundleContext bundleContext;
+ private void assertOutput(String expectedResourceName, String actual)
throws IOException {
+ try(InputStream is =
getClass().getResourceAsStream(expectedResourceName)) {
+ assertNotNull("Expecting classpath resource to be present:" +
expectedResourceName, is);
+ final String expected = IOUtils.toString(is, "UTF-8");
+ assertEquals(expected, actual);
+ }
+ }
+
@Before
public void setup() throws Exception {
dsa = new DefaultSchemaAggregator();
@@ -83,12 +91,7 @@ public class DefaultSchemaAggregatorTest {
dsa.aggregate(target, "B1a", "B2", "2.z");
final String sdl = target.toString().trim();
assertContainsIgnoreCase("schema aggregated by
DefaultSchemaAggregator", sdl);
-
- try(InputStream is =
getClass().getResourceAsStream("/several-providers-output.txt")) {
- assertNotNull("Expecting test resource to be present", is);
- final String expected = IOUtils.toString(is, "UTF-8");
- assertEquals(expected, sdl);
- }
+ assertOutput("/partials/several-providers-output.txt", sdl);
}
@Test
@@ -102,7 +105,7 @@ public class DefaultSchemaAggregatorTest {
}
@Test
- public void parseResult() throws Exception {
+ public void verifyResultSyntax() throws Exception {
final StringWriter target = new StringWriter();
tracker.addingBundle(U.mockProviderBundle(bundleContext, "SDL", 1,
"a.sdl.txt", "b.sdl.txt", "c.sdl.txt"), null);
@@ -125,6 +128,33 @@ public class DefaultSchemaAggregatorTest {
final Optional<TypeDefinition> mutation = reg.getType("Mutation");
assertTrue("Expecting Mutation", mutation.isPresent());
assertTrue(mutation.get().getChildren().toString().contains("someMutation"));
+
+ assertOutput("/partials/result-syntax-output.txt", sdl);
+ }
+
+ @Test
+ public void verifyResultSyntaxMutationOnly() throws Exception {
+ final StringWriter target = new StringWriter();
+ tracker.addingBundle(U.mockProviderBundle(bundleContext, "SDL", 1,
"a.sdl.txt", "mutation.only.txt"), null);
+
+ dsa.aggregate(target, "mutation.only");
+
+ // Parse the output with a real SDL parser
+ final String sdl = target.toString();
+ final TypeDefinitionRegistry reg = new SchemaParser().parse(sdl);
+
+ // And make sure it contains what we expect
+ assertTrue(reg.getDirectiveDefinition("fetcher").isPresent());
+
+ final Optional<TypeDefinition> mutation = reg.getType("Mutation");
+ assertTrue("Expecting Mutation", mutation.isPresent());
+
assertTrue(mutation.get().getChildren().toString().contains("theOnlyMutation"));
+
+ // A Query is required, even if not provided in any partial
+ final Optional<TypeDefinition> query = reg.getType("Query");
+ assertTrue("Expecting Query", query.isPresent());
+
+ assertOutput("/partials/result-syntax-output-mutation-only.txt", sdl);
}
@Test
diff --git a/src/test/resources/partials/mutation.only.txt
b/src/test/resources/partials/mutation.only.txt
new file mode 100644
index 0000000..7c11d1a
--- /dev/null
+++ b/src/test/resources/partials/mutation.only.txt
@@ -0,0 +1,6 @@
+PARTIAL: Example with a Mutation but no Query
+
+REQUIRES: a.sdl
+
+MUTATION:
+theOnlyMutation : SlingResource
\ No newline at end of file
diff --git a/src/test/resources/partials/result-syntax-output-mutation-only.txt
b/src/test/resources/partials/result-syntax-output-mutation-only.txt
new file mode 100644
index 0000000..1ac133d
--- /dev/null
+++ b/src/test/resources/partials/result-syntax-output-mutation-only.txt
@@ -0,0 +1,33 @@
+# Schema aggregated by DefaultSchemaAggregator
+
+# DefaultSchemaAggregator.source=a.sdl
+directive @fetcher(
+ name : String,
+ options : String = "",
+ source : String = ""
+) on FIELD_DEFINITION
+
+
+type Query {
+
+}
+
+type Mutation {
+
+# DefaultSchemaAggregator.source=mutation.only
+theOnlyMutation : SlingResource
+}
+
+# DefaultSchemaAggregator.source=a.sdl
+type SlingResource {
+ path: String!
+}
+
+type PageInfo {
+ count: Int
+}
+
+type typeFromA {
+ path : String
+}
+# End of Schema aggregated from {mutation.only,a.sdl} by
DefaultSchemaAggregator
\ No newline at end of file
diff --git a/src/test/resources/partials/result-syntax-output.txt
b/src/test/resources/partials/result-syntax-output.txt
new file mode 100644
index 0000000..d9d4923
--- /dev/null
+++ b/src/test/resources/partials/result-syntax-output.txt
@@ -0,0 +1,46 @@
+# Schema aggregated by DefaultSchemaAggregator
+
+# DefaultSchemaAggregator.source=a.sdl
+directive @fetcher(
+ name : String,
+ options : String = "",
+ source : String = ""
+) on FIELD_DEFINITION
+
+
+type Query {
+
+# DefaultSchemaAggregator.source=c.sdl
+oneSchemaResource : SlingResource @fetcher(name:"test/pipe" source:"$")
+oneSchemaQuery : SlingResourceConnection @connection(for: "SlingResource")
@fetcher(name:"test/query")
+
+
+}
+
+type Mutation {
+
+# DefaultSchemaAggregator.source=c.sdl
+someMutation : SlingResource
+}
+
+# DefaultSchemaAggregator.source=a.sdl
+type SlingResource {
+ path: String!
+}
+
+type PageInfo {
+ count: Int
+}
+
+type typeFromA {
+ path : String
+}
+# DefaultSchemaAggregator.source=b.sdl
+type SlingResourceConnection {
+ pageInfo : PageInfo
+}
+
+type typeFromB {
+ path : String
+}
+# End of Schema aggregated from {a.sdl,b.sdl,c.sdl} by DefaultSchemaAggregator
\ No newline at end of file
diff --git a/src/test/resources/several-providers-output.txt
b/src/test/resources/partials/several-providers-output.txt
similarity index 100%
rename from src/test/resources/several-providers-output.txt
rename to src/test/resources/partials/several-providers-output.txt