Skip to content

Commit

Permalink
consolidate even more subscriptions and queries in the inference meth…
Browse files Browse the repository at this point in the history
…od. More coherent and simple testing of empty queries. Fix empty subscriptions
  • Loading branch information
echauchot committed Feb 28, 2025
1 parent ab1e2ae commit 3e49640
Showing 1 changed file with 34 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ public Optional<GraphQLSchema> generate(ServerPhysicalPlan serverPlan) {

// process query table functions
final Optional<GraphQLObjectType> queriesObjectType = createQueriesOrSubscriptionsObjectType(serverPlan, AccessModifier.QUERY);
//TODO when there is no query fields createQueriesOrSubscriptionsObjectType return an empty optional, so this test is no more neeeded.
// and if we remove cleanInvalidTypes, the whole queryFields is no more needed.
/*
if (queryFields.isEmpty()) { // there must be at least 1 query
return Optional.empty();
}
queriesObjectType.ifPresent(graphQLSchemaBuilder::query);
*/
queriesObjectType.ifPresentOrElse(
graphQLSchemaBuilder::query,
() -> {throw new IllegalArgumentException("No queryable tables found for server");} // there is no query
);

// process subscriptions table functions
final Optional<GraphQLObjectType> subscriptionsObjectType = createQueriesOrSubscriptionsObjectType(serverPlan, AccessModifier.SUBSCRIPTION);
Expand All @@ -88,7 +95,6 @@ public Optional<GraphQLSchema> generate(ServerPhysicalPlan serverPlan) {

graphQLSchemaBuilder.additionalTypes(new LinkedHashSet<>(objectTypes)); // the cleaned types

Preconditions.checkArgument(!queriesObjectType.get().getFields().isEmpty(),"No queryable tables found for server");
return Optional.of(graphQLSchemaBuilder.build());
}

Expand Down Expand Up @@ -131,15 +137,11 @@ public Optional<GraphQLObjectType> createQueriesOrSubscriptionsObjectType(Server
final List<SqrlTableFunction> rootTableFunctions = tableFunctions.stream()
.filter(tableFunction -> !tableFunction.isRelationship())
.collect(Collectors.toList());
GraphQLObjectType rootObjectType;
if (tableFunctionsType == AccessModifier.QUERY) { //this method was called for queries
rootObjectType = createRootQueryType(rootTableFunctions);
} else { //this method was called for subscriptions
rootObjectType = createRootSubscriptionType(rootTableFunctions);
}
Optional<GraphQLObjectType> rootObjectType = createRootType(rootTableFunctions, tableFunctionsType);
//TODO no more needed?
//TODO fix cleanInvalidTypes: it removes nestedTypes.
// cleanInvalidTypes();
return Optional.of(rootObjectType);
return rootObjectType;
}


Expand Down Expand Up @@ -232,9 +234,10 @@ public Optional<GraphQLObjectType> createMutationsObjectType() {
}

/**
* Create the root Query graphQL object encapsulating the type references to all the root table functions.
* GraphQL queries and subscriptions are generated the same way. So we call this method with
* {@link AccessModifier#QUERY} for generating root Query type and with {@link AccessModifier#SUBSCRIPTION} for generating root subscription type.
*/
private GraphQLObjectType createRootQueryType(List<SqrlTableFunction> rootTableFunctions) {
private Optional<GraphQLObjectType> createRootType(List<SqrlTableFunction> rootTableFunctions, AccessModifier tableFunctionsType) {

List<GraphQLFieldDefinition> fields = new ArrayList<>();

Expand All @@ -244,58 +247,39 @@ private GraphQLObjectType createRootQueryType(List<SqrlTableFunction> rootTableF
continue;
}

final GraphQLOutputType type =
tableFunctionsType == AccessModifier.QUERY
? (GraphQLOutputType) wrapMultiplicity(createTypeReference(tableFunction), tableFunction.getMultiplicity())
: createTypeReference(tableFunction); // type is nullable because there can be no update in the subscription
GraphQLFieldDefinition field = GraphQLFieldDefinition.newFieldDefinition()
.name(tableFunctionName)
.type((GraphQLOutputType) wrapMultiplicity(createTypeReference(tableFunction), tableFunction.getMultiplicity()))
.type(type)
.arguments(createArguments(tableFunction))
.build();
fields.add(field);
}

if (fields.isEmpty()) {
return Optional.empty();
}
String rootTypeName = tableFunctionsType.name().toLowerCase();
rootTypeName = Character.toUpperCase(rootTypeName.charAt(0)) + rootTypeName.substring(1);
// rootTypeName == "Query" or "Subscription"
GraphQLObjectType rootQueryObjectType = GraphQLObjectType.newObject()
.name("Query")
.name(rootTypeName)
.fields(fields)
.build();

definedTypeNames.add("Query");
this.queryFields.addAll(fields);

return rootQueryObjectType;
}


/**
* Create the root Subscription graphQL object encapsulating the type references to all the root table functions.
*/
private GraphQLObjectType createRootSubscriptionType(List<SqrlTableFunction> rootTableFunctions) {

List<GraphQLFieldDefinition> fields = new ArrayList<>();

for (SqrlTableFunction tableFunction : rootTableFunctions) {
String tableFunctionName = tableFunction.getFullPath().getDisplay();
if (!isValidGraphQLName(tableFunctionName)) {
continue;
}

GraphQLFieldDefinition field = GraphQLFieldDefinition.newFieldDefinition()
.name(tableFunctionName)
.type(createTypeReference(tableFunction)) // type is nullable because there can be no update in the subscription
.arguments(createArguments(tableFunction))
.build();
fields.add(field);
definedTypeNames.add(rootTypeName);
// TODO no more needed ?
// for downstream cleaning invalid types
if (tableFunctionsType == AccessModifier.QUERY) {
this.queryFields.addAll(fields);
}

GraphQLObjectType rootQueryObjectType = GraphQLObjectType.newObject()
.name("Subscription")
.fields(fields)
.build();

definedTypeNames.add("Subscription");
this.queryFields.addAll(fields);

return rootQueryObjectType;
return Optional.of(rootQueryObjectType);
}


/**
* Create a non-relationship field :
* - a scalar type
Expand Down Expand Up @@ -384,6 +368,7 @@ private GraphQLOutputType createTypeReference(SqrlTableFunction tableFunction) {
}


// TODO no more needed ?
public void cleanInvalidTypes() {
// Ensure every field points to a valid type
boolean found;
Expand Down

0 comments on commit 3e49640

Please sign in to comment.