Skip to content

Commit

Permalink
depend on allocator to refer to other generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
klavs committed Feb 21, 2020
1 parent 1bc11c1 commit 2ec3400
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 112 deletions.
6 changes: 1 addition & 5 deletions gql_build/lib/gql_build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ Builder dataBuilder(
Builder opBuilder(
BuilderOptions options,
) =>
OpBuilder(
AssetId.parse(
options.config["schema"] as String,
),
);
OpBuilder();

/// Builds GraphQL type-safe request builder
Builder reqBuilder(
Expand Down
10 changes: 0 additions & 10 deletions gql_build/lib/src/op_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ import "package:gql_build/src/config.dart";
import "package:gql_code_builder/op.dart";

class OpBuilder implements Builder {
final AssetId schemaId;

OpBuilder(
this.schemaId,
);

@override
Map<String, List<String>> get buildExtensions => {
sourceExtension: [opExtension],
Expand All @@ -22,13 +16,9 @@ class OpBuilder implements Builder {
@override
FutureOr<void> build(BuildStep buildStep) async {
final doc = await readDocument(buildStep);
final schema = await readDocument(buildStep, schemaId);

final library = buildOpLibrary(
doc,
schema,
buildStep.inputId.changeExtension(astExtension).uri.toString(),
schemaId.changeExtension(schemaExtension).uri.toString(),
);

return writeDocument(
Expand Down
2 changes: 0 additions & 2 deletions gql_build/lib/src/req_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class ReqBuilder implements Builder {
final library = buildReqLibrary(
doc,
schema,
buildStep.inputId.changeExtension(opExtension).uri.toString(),
buildStep.inputId.changeExtension(varExtension).uri.toString(),
);

return writeDocument(
Expand Down
37 changes: 30 additions & 7 deletions gql_build/lib/src/utils/writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Future<void> writeDocument(
final genSrc = _dartfmt.format("${library.accept(
DartEmitter(
_GqlAllocator(
buildStep.inputId.uri.toString(),
generatedAsset.uri.toString(),
extension,
),
),
)}");
Expand All @@ -34,14 +34,15 @@ Future<void> writeDocument(
class _GqlAllocator implements Allocator {
static const _doNotPrefix = ["dart:core"];

final _imports = <String, int>{};
final String extension;
final String sourceUrl;
final String currentUrl;

final _imports = <String, int>{};
var _keys = 1;

_GqlAllocator(
this.sourceUrl,
this.currentUrl,
this.extension,
);

@override
Expand All @@ -52,9 +53,31 @@ class _GqlAllocator implements Allocator {
return symbol;
}

if (reference.url.endsWith(sourceExtension)) {
final replacedUrl =
reference.url.replaceAll(RegExp(r".graphql$"), extension);
final uri = Uri.parse(reference.url);

if (uri.path.endsWith(sourceExtension)) {
final replacedUrl = uri
.replace(
path: uri.path.replaceAll(
RegExp(r".graphql$"),
".${uri.fragment}.gql.dart",
),
)
.removeFragment()
.toString();

if (replacedUrl == currentUrl) {
return symbol;
}

return "_i${_imports.putIfAbsent(replacedUrl, _nextKey)}.$symbol";
}

if (uri.path.isEmpty && uri.fragment.isNotEmpty) {
final replacedUrl = sourceUrl.replaceAll(
RegExp(r".graphql$"),
".${uri.fragment}.gql.dart",
);

if (replacedUrl == currentUrl) {
return symbol;
Expand Down
1 change: 0 additions & 1 deletion gql_build/lib/src/var_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class VarBuilder implements Builder {
final library = buildVarLibrary(
doc,
schema,
schemaId.changeExtension(schemaExtension).uri.toString(),
);

return writeDocument(
Expand Down
7 changes: 6 additions & 1 deletion gql_code_builder/lib/ast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ Library buildAstLibrary(
[],
{
"definitions": literalList(
source.getRefs(),
source.getRefs().map(
(ref) => Reference(
ref.symbol,
ref.url + "#ast",
),
),
),
},
)
Expand Down
9 changes: 1 addition & 8 deletions gql_code_builder/lib/op.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@ import "package:gql_code_builder/source.dart";

Library buildOpLibrary(
SourceNode docSource,
SourceNode schemaSource,
String astDocUrl,
String schemaUrl,
) {
final doc = docSource.flatDocument;
final schema = schemaSource.flatDocument;

final operations = doc.definitions.whereType<OperationDefinitionNode>().map(
(def) => refer(
Expand All @@ -19,10 +15,7 @@ Library buildOpLibrary(
.call(
[],
{
"document": refer(
"document",
astDocUrl,
),
"document": refer("document", "#ast"),
"operationName": literalString(
def.name?.value,
),
Expand Down
4 changes: 0 additions & 4 deletions gql_code_builder/lib/req.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@ import "package:gql_code_builder/source.dart";
Library buildReqLibrary(
SourceNode docSource,
SourceNode schemaSource,
String opDocUrl,
String varDocUrl,
) =>
Library(
(b) => b.body
..addAll(
buildOperationReqClasses(
docSource.flatDocument,
schemaSource.flatDocument,
opDocUrl,
varDocUrl,
),
),
);
118 changes: 65 additions & 53 deletions gql_code_builder/lib/src/operation/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ List<Class> _buildSelectionSetDataClasses({
(b) => b
..name = name
..implements = ListBuilder(
superclassSelections.keys.map<Reference>((superName) => refer(
superName,
superclassSelections[superName]
.url
// TODO: remove the hard-coded string
?.replaceAll(RegExp(r".graphql$"), ".data.gql.dart"),
)))
superclassSelections.keys.map<Reference>(
(superName) => refer(
superName,
superclassSelections[superName].url + "#data",
),
),
)
..constructors = _buildConstructors(
name,
selections.whereType<InlineFragmentNode>().toList(),
Expand Down Expand Up @@ -182,25 +182,25 @@ List<Class> _buildSelectionSetDataClasses({
),
),
),
...selections
.whereType<InlineFragmentNode>()
.expand((inlineFragment) => _buildSelectionSetDataClasses(
name: "$name\$as${inlineFragment.typeCondition.on.name.value}",
selections: _mergeSelections(
[
...selections.whereType<FieldNode>(),
...selections.whereType<FragmentSpreadNode>(),
...inlineFragment.selectionSet.selections,
],
fragmentMap,
),
fragmentMap: fragmentMap,
schemaSource: schemaSource,
type: inlineFragment.typeCondition.on.name.value,
superclassSelections: {
name: _SourceSelections(url: null, selections: selections)
},
))
...selections.whereType<InlineFragmentNode>().expand(
(inlineFragment) => _buildSelectionSetDataClasses(
name: "$name\$as${inlineFragment.typeCondition.on.name.value}",
selections: _mergeSelections(
[
...selections.whereType<FieldNode>(),
...selections.whereType<FragmentSpreadNode>(),
...inlineFragment.selectionSet.selections,
],
fragmentMap,
),
fragmentMap: fragmentMap,
schemaSource: schemaSource,
type: inlineFragment.typeCondition.on.name.value,
superclassSelections: {
name: _SourceSelections(url: null, selections: selections)
},
),
),
];
}

Expand Down Expand Up @@ -273,25 +273,30 @@ Map<String, _SourceSelections> _fragmentSelectionsForField(
Map<String, _SourceSelections> fragmentMap,
FieldNode field,
) =>
{
for (var entry in fragmentMap.entries)
for (var selection
in entry.value.selections.whereType<FieldNode>().where(
Map.fromEntries(
fragmentMap.entries.expand(
(entry) => entry.value.selections.whereType<FieldNode>().where(
(selection) {
if (selection.selectionSet == null) return false;

final selectionKey = selection.alias?.value ?? selection.name.value;
final fieldKey = field.alias?.value ?? field.name.value;

return selectionKey == fieldKey;
},
))
if (selection.selectionSet != null)
"${entry.key}\$${field.alias?.value ?? field.name.value}":
_SourceSelections(
url: entry.value.url,
selections: selection.selectionSet.selections
.whereType<FieldNode>()
.toList())
};
).map(
(selection) => MapEntry(
"${entry.key}\$${field.alias?.value ?? field.name.value}",
_SourceSelections(
url: entry.value.url,
selections: selection.selectionSet.selections
.whereType<FieldNode>()
.toList(),
),
),
),
),
);

ListBuilder<Field> _buildFields() => ListBuilder<Field>(
<Field>[
Expand All @@ -315,27 +320,35 @@ ListBuilder<Constructor> _buildConstructors(
Constructor(
(b) => b
..name = inlineFragments.isEmpty ? null : "fromData"
..requiredParameters.add(Parameter(
(b) => b
..name = "data"
..toThis = true,
))
..requiredParameters.add(
Parameter(
(b) => b
..name = "data"
..toThis = true,
),
)
..constant = true,
),
if (inlineFragments.isNotEmpty)
Constructor(
(b) => b
..factory = true
..requiredParameters.add(Parameter((b) => b..name = "data"))
..body = Code([
"switch (data['__typename']) {",
...inlineFragments.map((inlineFragment) => """
..requiredParameters.add(
Parameter(
(b) => b..name = "data",
),
)
..body = Code(
[
"switch (data['__typename']) {",
...inlineFragments.map((inlineFragment) => """
case "${inlineFragment.typeCondition.on.name.value}":
return ${'$name\$as${inlineFragment.typeCondition.on.name.value}'}(data);
"""),
"default: return $name.fromData(data); }"
].join()),
)
"default: return $name.fromData(data); }"
].join(),
),
),
],
);

Expand Down Expand Up @@ -389,8 +402,7 @@ Method _buildGetter(
else if (fieldTypeDef != null)
typeName: refer(
typeName,
// TODO: remove the hard-coded string
schemaSource.url.replaceAll(RegExp(r".graphql$"), ".schema.gql.dart"),
schemaSource.url + "#schema",
)
};

Expand Down
13 changes: 2 additions & 11 deletions gql_code_builder/lib/src/operation/req.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,23 @@ import "package:gql/ast.dart";
List<Class> buildOperationReqClasses(
DocumentNode doc,
DocumentNode schema,
String opDocUrl,
String varDocUrl,
) =>
doc.definitions
.whereType<OperationDefinitionNode>()
.map(
(op) => _buildOperationReqClass(
op,
schema,
opDocUrl,
varDocUrl,
),
)
.toList();

Class _buildOperationReqClass(
OperationDefinitionNode node,
DocumentNode schema,
String opDocUrl,
String varDocUrl,
) {
final name = node.name.value;
final varBuilderRef = refer("${name}VarBuilder", varDocUrl);
final varBuilderRef = refer("${name}VarBuilder", "#var");

return Class(
(b) => b
Expand Down Expand Up @@ -64,10 +58,7 @@ Class _buildOperationReqClass(
).call(
[],
{
"operation": refer(
name,
opDocUrl,
),
"operation": refer(name, "#op"),
"variables": refer("buildVars").call(
<Expression>[
varBuilderRef.call([]),
Expand Down
Loading

0 comments on commit 2ec3400

Please sign in to comment.