Skip to content
Open
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
19 changes: 19 additions & 0 deletions packages/graphql_codegen/lib/src/config/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ class GraphQLCodegenConfigEnum {
Map<String, dynamic> toJson() => _$GraphQLCodegenConfigEnumToJson(this);
}

@JsonSerializable()
class GraphQLCodegenConfigImplements {
final String parent;
final String? import;

const GraphQLCodegenConfigImplements({
required this.parent,
this.import,
});

@override
factory GraphQLCodegenConfigImplements.fromJson(Map<String, dynamic> json) =>
_$GraphQLCodegenConfigImplementsFromJson(json);

Map<String, dynamic> toJson() => _$GraphQLCodegenConfigImplementsToJson(this);
}

@JsonSerializable()
class GraphQLCodegenConfig {
final Set<GraphQLCodegenConfigClient> clients;
Expand All @@ -67,6 +84,7 @@ class GraphQLCodegenConfig {
final String outputDirectory;
final bool disableContextReplacement;
final bool disableCopyWithGeneration;
final Map<String, List<GraphQLCodegenConfigImplements>> typeImplements;

GraphQLCodegenConfig({
this.clients = const {},
Expand All @@ -82,6 +100,7 @@ class GraphQLCodegenConfig {
this.extraKeywords = const [],
this.outputDirectory = '.',
this.disableCopyWithGeneration = false,
this.typeImplements = const {},
});

@override
Expand Down
25 changes: 25 additions & 0 deletions packages/graphql_codegen/lib/src/config/config.g.dart

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

13 changes: 13 additions & 0 deletions packages/graphql_codegen/lib/src/printer/base/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ Class printContext(PrintContext c) {
c.addDependency(extendContext.path);
}
final properties = c.context.properties;

final typeImplements =
context.config.typeImplements[c.namePrinter.printClassName(context.path)];
if (typeImplements != null) {
for (final t in typeImplements) {
if (t.import != null) {
c.addPackage(t.import!);
}
}
}

return Class(
(b) => b
..name = c.namePrinter.printClassName(context.path)
Expand All @@ -120,6 +131,8 @@ Class printContext(PrintContext c) {
.map(refer),
if (extendContext != null)
refer(c.namePrinter.printClassName(extendContext.path)),
if (typeImplements != null)
...typeImplements.map((e) => Reference(e.parent, e.import))
])
..constructors = ListBuilder([
_printConstructor(c, properties),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type Query {
t: T
}

type T {
id: String!
name: String
}

fragment F on T {
id
name
}

query Q {
t {
...F
}
}
Loading