Skip to content

feat: build possible types map #343

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

Merged
merged 5 commits into from
Aug 7, 2022
Merged
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions codegen/gql_build/lib/gql_build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Builder schemaBuilder(
SchemaBuilder(
typeOverrideMap(options.config["type_overrides"]),
enumFallbackConfig(options.config),
generatePossibleTypesConfig(options.config),
);

/// Builds an aggregate Serlializers object for [built_value]s
Expand Down
3 changes: 3 additions & 0 deletions codegen/gql_build/lib/src/schema_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import "./utils/writer.dart";
class SchemaBuilder implements Builder {
final Map<String, Reference> typeOverrides;
final EnumFallbackConfig enumFallbackConfig;
final bool generatePossibleTypesMap;

SchemaBuilder(
this.typeOverrides,
this.enumFallbackConfig,
this.generatePossibleTypesMap,
);

@override
Expand All @@ -37,6 +39,7 @@ class SchemaBuilder implements Builder {
basename(generatedPartUrl),
typeOverrides,
enumFallbackConfig,
generatePossibleTypesMap: generatePossibleTypesMap,
);

return writeDocument(
Expand Down
3 changes: 3 additions & 0 deletions codegen/gql_build/lib/src/utils/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ EnumFallbackConfig enumFallbackConfig(Map<String, dynamic> config) =>
fallbackValueMap: enumFallbackMap(config["enum_fallbacks"]),
);

bool generatePossibleTypesConfig(Map<String, dynamic> config) =>
config["generate_possible_types_map"] as bool? ?? true;

Map<String, String> enumFallbackMap(final dynamic enumFallbacks) {
if (enumFallbacks is YamlMap) {
return Map.fromEntries(
Expand Down
1 change: 1 addition & 0 deletions codegen/gql_code_builder/lib/possible_types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export "src/utils/possible_types.dart";
38 changes: 31 additions & 7 deletions codegen/gql_code_builder/lib/schema.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
import "package:code_builder/code_builder.dart";
import "package:gql/ast.dart";
import "package:gql_code_builder/source.dart";
import "package:gql_code_builder/src/schema.dart";
import "package:gql_code_builder/src/utils/possible_types.dart";

Library buildSchemaLibrary(
SourceNode schemaSource,
String partUrl,
Map<String, Reference> typeOverrides,
EnumFallbackConfig enumFallbackConfig) {
SourceNode schemaSource,
String partUrl,
Map<String, Reference> typeOverrides,
EnumFallbackConfig enumFallbackConfig, {
bool generatePossibleTypesMap = false,
}) {
final lib = buildSchema(
schemaSource,
typeOverrides,
enumFallbackConfig,
) as Library;

final Code? possibleTypes;
if (generatePossibleTypesMap && lib.body.isNotEmpty) {
possibleTypes = buildPossibleTypes(schemaSource.document);
} else {
possibleTypes = null;
}
return lib.rebuild(
(b) => b
..directives.add(
(b) {
b.directives.add(
Directive.part(partUrl),
),
);
if (possibleTypes != null) {
b.body.add(possibleTypes);
}
return b;
},
);
}

Code buildPossibleTypes(DocumentNode document) {
// generate the map of possible types
final possibleTypesMap = document.possibleTypesMap();
// wrap the map in a literal for codegen
final possibleTypesLiteral = literalMap(possibleTypesMap);
// assign the literal to a const variable named "possibleTypes"
return possibleTypesLiteral.assignConst("possibleTypesMap").statement;
}

class EnumFallbackConfig {
final bool generateFallbackValuesGlobally;
final String? globalEnumFallbackName;
Expand Down
96 changes: 96 additions & 0 deletions codegen/gql_code_builder/lib/src/utils/possible_types.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import "package:gql/ast.dart";

/// implementation based on graphql_codegen:
/// https://github.com/heftapp/graphql_codegen/blob/main/packages/graphql_codegen/lib/src/context.dart#L770
extension PossibleTypes on DocumentNode {
Map<String, Set<String>> possibleTypesMap() {
// TODO handle interfaces that extend other interfaces?
// https://github.com/graphql/graphql-spec/pull/373
final possibleTypes = <String, Set<String>>{};

for (final definition in definitions) {
if (definition is UnionTypeDefinitionNode) {
final types = possibleTypes[definition.name.value] ?? {};
for (final tpe in definition.types) {
types.add(tpe.name.value);
}
possibleTypes[definition.name.value] = types;
} else if (definition is ObjectTypeDefinitionNode) {
for (final tpe in definition.interfaces) {
final types = possibleTypes[tpe.name.value] ?? {};
types.add(definition.name.value);
possibleTypes[tpe.name.value] = types;
}
}
}
return possibleTypes.map((key, value) => MapEntry<String, Set<String>>(
key,
value
.expand<ObjectTypeDefinitionNode>(_lookupConcreteTypes)
.map((e) => e.name.value)
.toSet()));
}

Iterable<ObjectTypeDefinitionNode> _lookupConcreteTypes(String name) {
final typeDefinition = _lookupType(name);
if (typeDefinition is ObjectTypeDefinitionNode) {
return [typeDefinition];
}
if (typeDefinition is UnionTypeDefinitionNode) {
return typeDefinition.types
.expand((e) => _lookupConcreteTypes(e.name.value));
}

if (typeDefinition is InterfaceTypeDefinitionNode) {
return definitions.whereType<ObjectTypeDefinitionNode>().where(
(element) => element.interfaces
.where((element) => element.name.value == name)
.isNotEmpty,
);
}

return [];
}

TType? _lookupType<TType extends TypeDefinitionNode>(String name) {
final defs = _memoizedTypeDefinitionsOf(this);

final def = defs[name];
if (def == null) {
return null;
}
if (def is! TType) {
return null;
}
return def;
}
}

final _memoizedTypeDefinitionsOf = _memo1(_typeDefinitionsOf);

Map<String, TypeDefinitionNode> _typeDefinitionsOf(DocumentNode schema) =>
Map.fromEntries(
schema.definitions
.whereType<TypeDefinitionNode>()
.map((e) => MapEntry(e.name.value, e)),
);

/// Checks 1 argument for equality with [==] operator and returns the cached
/// result if it was not changed.
R Function(A) _memo1<A, R>(R Function(A) func) {
late A prevArg;
late R prevResult;
bool isInitial = true;

return (A arg) {
if (!isInitial && arg == prevArg) {
return prevResult;
} else {
prevArg = arg;
prevResult = func(arg);
isInitial = false;

return prevResult;
}
};
}
2 changes: 1 addition & 1 deletion codegen/gql_code_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
built_value: ^8.0.6
code_builder: ^4.0.0
collection: ^1.15.0
gql: ^0.13.0
gql: ^0.14.0
gql_exec: ^0.4.0
path: ^1.8.0
dev_dependencies:
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading