Skip to content

generate possibleTypes map #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions codegen/gql_code_builder/lib/src/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import "package:gql/ast.dart";

import "./schema/enum.dart";
import "./schema/input.dart";
import "./schema/possible_types.dart";
import "./schema/scalar.dart";
import "../schema.dart";
import "../source.dart";
Expand Down Expand Up @@ -49,9 +50,10 @@ class _SchemaBuilderVisitor extends SimpleVisitor<Spec?> {
DocumentNode node,
) =>
Library(
(b) => b.body.addAll(
_acceptMany(node.definitions).whereType<Spec>(),
),
(b) => b.body.addAll([
buildPossibleTypes(node.definitions),
..._acceptMany(node.definitions).whereType<Spec>(),
]),
);

@override
Expand Down
85 changes: 85 additions & 0 deletions codegen/gql_code_builder/lib/src/schema/possible_types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import "package:code_builder/code_builder.dart";
import "package:gql/ast.dart";

Expression buildPossibleTypes(
List<DefinitionNode> definitions,
) {
final typeDefs = definitions.whereType<TypeDefinitionNode>();

final typesByName = typeDefs.fold<Map<NameNode, TypeDefinitionNode>>(
{}, (map, typeDef) => map..[typeDef.name] = typeDef);

final interfaceImplementors = _interfaceImplementors(typeDefs);

final Map<String, Set<String>> possibleTypesMap =
typeDefs.fold({}, (map, typeDef) {
if (typeDef is UnionTypeDefinitionNode ||
typeDef is InterfaceTypeDefinitionNode) {
final concreteTypes = _concreteTypes(
typeDef.name,
typesByName,
interfaceImplementors,
);
return map
..[typeDef.name.value] =
concreteTypes.map((type) => type.name.value).toSet();
} else {
return map;
}
});

return literalMap(possibleTypesMap).assignConst("possibleTypes");
}

Set<ObjectTypeDefinitionNode> _concreteTypes(
NameNode name,
Map<NameNode, TypeDefinitionNode> typesByName,
Map<NameNode, Set<TypeDefinitionNode>> interfaceImplementors,
) {
final type = typesByName[name]!;

if (type is UnionTypeDefinitionNode) {
return type.types
.expand(
(subtype) =>
_concreteTypes(subtype.name, typesByName, interfaceImplementors),
)
.toSet();
} else if (type is InterfaceTypeDefinitionNode) {
return interfaceImplementors[type.name]!
.expand(
(subtype) =>
_concreteTypes(subtype.name, typesByName, interfaceImplementors),
)
.toSet();
} else if (type is ObjectTypeDefinitionNode) {
return {type};
} else {
return {};
}
}

Map<NameNode, Set<TypeDefinitionNode>> _interfaceImplementors(
Iterable<TypeDefinitionNode> typeDefs,
) {
final interfaces = typeDefs
.whereType<InterfaceTypeDefinitionNode>()
.map((interface) => interface.name);
final interfaceMap = Map<NameNode, Set<TypeDefinitionNode>>.fromIterable(
interfaces,
value: (dynamic _) => {});

for (final typeDef in typeDefs) {
if (typeDef is InterfaceTypeDefinitionNode) {
/// TODO: handle interfaces that implement other interfaces once this is implemented in gql ast
/// https://github.com/graphql/graphql-spec/pull/373
} else if (typeDef is ObjectTypeDefinitionNode) {
typeDef.interfaces
.forEach((interface) => interfaceMap[interface.name]!.add(typeDef));
} else if (typeDef is UnionTypeDefinitionNode) {
/// TODO: handle unions
}
}

return interfaceMap;
}