Skip to content

Commit

Permalink
Loop to remove all invalid schema objects
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Henneberger <git@danielhenneberger.com>
  • Loading branch information
henneberger committed Oct 30, 2023
1 parent eb2dcc3 commit 3ecddff
Showing 1 changed file with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,41 @@ public GraphQLSchema generate(SqrlSchemaForInference schema, boolean allowAdditi

void postProcess() {
// Ensure every field points to a valid type
Iterator<GraphQLObjectType> iterator = objectTypes.iterator();
while (iterator.hasNext()) {
GraphQLObjectType objectType = iterator.next();
List<GraphQLFieldDefinition> invalidFields = new ArrayList<>();

for (GraphQLFieldDefinition field : objectType.getFields()) {
if (!isValidType(field.getType())) {
invalidFields.add(field);
boolean found;
int attempts = 10;
do {
found = false;
Iterator<GraphQLObjectType> iterator = objectTypes.iterator();
while (iterator.hasNext()) {
GraphQLObjectType objectType = iterator.next();
List<GraphQLFieldDefinition> invalidFields = new ArrayList<>();

for (GraphQLFieldDefinition field : objectType.getFields()) {
if (!isValidType(field.getType())) {
invalidFields.add(field);
}
}
}

// Refactor to remove invalid fields
List<GraphQLFieldDefinition> fields = new ArrayList<>(objectType.getFields());
fields.removeAll(invalidFields);
// Refactor to remove invalid fields
List<GraphQLFieldDefinition> fields = new ArrayList<>(objectType.getFields());
fields.removeAll(invalidFields);

// After removing invalid fields, if an object has no fields, it should be removed
if (fields.isEmpty()) {
iterator.remove();
// After removing invalid fields, if an object has no fields, it should be removed
if (fields.isEmpty()) {
iterator.remove();
found = true;
}
}
}

queryFields.removeIf(field -> !isValidType(field.getType()));
found |= queryFields.removeIf(field -> !isValidType(field.getType()));

// Ensure each object has at least one field
objectTypes.removeIf(objectType -> objectType.getFields().isEmpty());
// Ensure each object has at least one field
found |= objectTypes.removeIf(objectType -> objectType.getFields().isEmpty());
} while(found && --attempts != 0);

if (found) {
throw new RuntimeException("Schema too complexity high, could not be reduced");
}
}

boolean isValidType(GraphQLType type) {
Expand Down

0 comments on commit 3ecddff

Please sign in to comment.