Skip to content

Commit

Permalink
Cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
echauchot committed Feb 25, 2025
1 parent 8668b97 commit b0e5836
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.datasqrl.canonicalizer.Name;
import com.datasqrl.canonicalizer.NamePath;
import com.datasqrl.config.PackageJson.CompilerConfig;
import com.datasqrl.engine.log.LogManager;
import com.datasqrl.engine.server.ServerPhysicalPlan;
import com.datasqrl.schema.Multiplicity;
import com.datasqrl.v2.tables.SqrlFunctionParameter;
Expand Down Expand Up @@ -59,21 +58,17 @@ public class GraphqlSchemaFactory2 {

private final List<GraphQLFieldDefinition> queryFields = new ArrayList<>();
private final List<GraphQLObjectType> objectTypes = new ArrayList<>();
private final Set<String> usedNames = new HashSet<>();
private final boolean addArguments;
private final Set<String> definedTypeNames = new HashSet<>();
private final boolean extendedScalarTypes;
private final LogManager logManager;
private final ExecutionGoal goal;
public final Set<String> seen = new HashSet<>();

@Inject
public GraphqlSchemaFactory2(CompilerConfig config, LogManager logManager, ExecutionGoal goal) {
this(config.isAddArguments(), config.isExtendedScalarTypes(), logManager, goal);
public GraphqlSchemaFactory2(CompilerConfig config, ExecutionGoal goal) {
this(config.isExtendedScalarTypes(), goal);
}

public GraphqlSchemaFactory2(boolean addArguments, boolean extendedScalarTypes, LogManager logManager, ExecutionGoal goal) {
this.addArguments = addArguments;
this.logManager = logManager;
public GraphqlSchemaFactory2(boolean extendedScalarTypes, ExecutionGoal goal) {
this.extendedScalarTypes = extendedScalarTypes;
this.goal = goal;
}
Expand Down Expand Up @@ -180,7 +175,7 @@ private Optional<GraphQLObjectType> createRootTableFunctionResultType(SqrlTableF
String typeName;
if (tableFunction.getBaseTable().isPresent()) {
final String baseTableName = tableFunction.getBaseTable().get().getName();
if (usedNames.contains(baseTableName)) {// result type was already defined
if (definedTypeNames.contains(baseTableName)) {// result type was already defined
return Optional.empty();
}
else { // new result type using base name
Expand Down Expand Up @@ -224,7 +219,7 @@ private Optional<GraphQLObjectType> createRootTableFunctionResultType(SqrlTableF
.name(typeName)
.fields(fields)
.build();
usedNames.add(typeName);
definedTypeNames.add(typeName);
queryFields.addAll(fields);
return Optional.of(objectType);
}
Expand Down Expand Up @@ -258,7 +253,7 @@ private Optional<GraphQLObjectType> createRelationshipTableFunctionResultType(Sq
.name(typeName)
.fields(fields)
.build();
usedNames.add(typeName);
definedTypeNames.add(typeName);
queryFields.addAll(fields);
return Optional.of(objectType);
}
Expand Down Expand Up @@ -348,8 +343,7 @@ private GraphQLObjectType createRootQueryType(List<SqrlTableFunction> rootTableF
.fields(fields)
.build();

usedNames.add("Query");

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

return rootQueryObjectType;
Expand Down Expand Up @@ -396,17 +390,17 @@ private Optional<GraphQLFieldDefinition> createRelDataTypeField(RelDataTypeField
* For a relationship table function sur as this: <pre>{@code Customer.orders := SELECT * FROM Orders o WHERE
* this.customerid = o.customerid; }</pre> Create a type reference for the orders field in the Customer table.
*/
private Optional<GraphQLFieldDefinition> createRelationshipField(SqrlTableFunction tableFunction) {
String fieldName = tableFunction.getFullPath().getLast().getDisplay();
private Optional<GraphQLFieldDefinition> createRelationshipField(SqrlTableFunction relationship) {
String fieldName = relationship.getFullPath().getLast().getDisplay();
if (!isValidGraphQLName(fieldName) || isHiddenString(fieldName)) {
return Optional.empty();
}

// reference the type that will be defined when the table function relationship is processed
GraphQLFieldDefinition field = GraphQLFieldDefinition.newFieldDefinition()
.name(fieldName)
.type(wrapMultiplicity(createTypeReference(tableFunction), tableFunction.getMultiplicity()))
.arguments(createArguments(tableFunction))
.type(wrapMultiplicity(createTypeReference(relationship), relationship.getMultiplicity()))
.arguments(createArguments(relationship))
.build();

return Optional.of(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.datasqrl.schema.Multiplicity;
import com.datasqrl.v2.tables.SqrlTableFunction;
import graphql.Scalars;
import graphql.language.FieldDefinition;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
Expand All @@ -25,7 +24,6 @@
import graphql.schema.GraphQLType;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -164,9 +162,6 @@ public static Optional<GraphQLType> getInOutType(RelDataType type, NamePath name
}
}

// Todo move to dialect?
public static final AtomicInteger i = new AtomicInteger(0);

private static String generateUniqueNameForType(NamePath namePath, Set<String> seen, String postfix) {
String name = toName(namePath, postfix);
String uniqueName = uniquify(name, seen);
Expand All @@ -188,7 +183,7 @@ private static String toName(NamePath namePath, String postfix) {
}

public static Optional<GraphQLOutputType> createOutputTypeForRelDataType(RelDataType type,
NamePath namePath, Set<String> seen, boolean extendedScalarTypes) {
NamePath namePath, Set<String> seen, boolean extendedScalarTypes) {
if (!type.isNullable()) {
return getOutputType(type, namePath, seen, extendedScalarTypes).map(GraphQLNonNull::nonNull);
}
Expand All @@ -203,7 +198,6 @@ public static Optional<GraphQLInputType> createInputTypeForRelDataType(RelDataTy
return getGraphQLInputType(type, namePath, seen, extendedScalarTypes);
}

// TODO should not we use getInOutTypeHelper instead, the code seems duplicated ?
private static Optional<GraphQLInputType> getGraphQLInputType(RelDataType type, NamePath namePath, Set<String> seen, boolean extendedScalarTypes) {
switch (type.getSqlTypeName()) {
case BOOLEAN:
Expand Down Expand Up @@ -234,7 +228,7 @@ private static Optional<GraphQLInputType> getGraphQLInputType(RelDataType type,
case VARBINARY:
return Optional.of(Scalars.GraphQLString); // Typically handled as Base64 encoded strings
case ARRAY:
return type.getComponentType() != null
return type.getComponentType() != null // traverse innner type
? getGraphQLInputType(type.getComponentType(), namePath, seen, extendedScalarTypes).map(GraphQLList::list)
: Optional.empty();
case ROW:
Expand Down Expand Up @@ -264,31 +258,6 @@ private static Optional<GraphQLInputType> createGraphQLInputObjectType(RelDataTy
return Optional.of(builder.build());
}

/**
* Helper to handle map types, transforming them into a list of key-value pairs.
*/
private static Optional<GraphQLInputType> createGraphQLInputMapType(RelDataType keyType, RelDataType valueType, NamePath namePath, Set<String> seen, boolean extendedScalarTypes) {
GraphQLInputObjectType mapEntryType = GraphQLInputObjectType.newInputObject()
.name("MapEntry")
.field(GraphQLInputObjectField.newInputObjectField()
.name("key")
.type(getGraphQLInputType(keyType, namePath, seen, extendedScalarTypes).orElse(Scalars.GraphQLString))
.build())
.field(GraphQLInputObjectField.newInputObjectField()
.name("value")
.type(getGraphQLInputType(valueType, namePath, seen, extendedScalarTypes).orElse(Scalars.GraphQLString))
.build())
.build();
return Optional.of(GraphQLList.list(mapEntryType));
}

/**
* Checks if the graphql schema has a different case than the graphql schema
*/
public static boolean hasVaryingCase(FieldDefinition field, RelDataTypeField relDataTypeField) {
return !field.getName().equals(relDataTypeField.getName());
}

public static String uniquifyTableFunctionName(SqrlTableFunction tableFunction) {
return tableFunction.getFullPath().toString("_");
}
Expand Down

0 comments on commit b0e5836

Please sign in to comment.