Skip to content

Commit 8cab1ee

Browse files
committed
feat: configurable empty relationship type
1 parent fe08cc5 commit 8cab1ee

File tree

9 files changed

+22
-4
lines changed

9 files changed

+22
-4
lines changed

lib/anchor/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ class Config
44
:field_case,
55
:use_active_record_presence,
66
:use_active_record_comment,
7-
:infer_nullable_relationships_as_optional
7+
:infer_nullable_relationships_as_optional,
8+
:empty_relationship_type
89

910
def initialize
1011
@ar_column_to_type = nil
1112
@field_case = nil
1213
@use_active_record_presence = nil
1314
@use_active_record_comment = nil
1415
@infer_nullable_relationships_as_optional = nil
16+
@empty_relationship_type = nil
1517
end
1618
end
1719
end

lib/anchor/type_script/resource.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ def express(context: {}, include_all_fields:, exclude_fields:)
44
included_fields = schema_fetchable_fields(context:, include_all_fields:)
55
included_fields -= exclude_fields if exclude_fields
66

7+
relationships_property = anchor_relationships_property(included_fields:)
8+
if relationships_property.nil? && Anchor.config.empty_relationship_type
9+
relationships_property = Anchor::Types::Property.new(:relationships, Anchor.config.empty_relationship_type.call)
10+
end
11+
712
properties = [id_property, type_property] +
813
Array.wrap(anchor_attributes_properties(included_fields:)) +
9-
Array.wrap(anchor_relationships_property(included_fields:)) +
14+
Array.wrap(relationships_property) +
1015
[anchor_meta_property].compact + [anchor_links_property].compact
1116

1217
expression = Anchor::TypeScript::Serializer.type_string(Anchor::Types::Object.new(properties))

lib/anchor/type_script/serializer.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def serialize_literal(value)
3535
end
3636

3737
def serialize_object(type, depth)
38+
return "{}" if type.properties.empty?
3839
properties = type.properties.flat_map do |p|
3940
[
4041
p.description && "/** #{p.description} */",

lib/anchor/types.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class Object
1818
attr_reader :properties
1919

2020
def initialize(properties)
21-
@properties = properties
21+
@properties = properties || []
2222
end
2323

2424
class << self
25-
attr_reader :properties
25+
def properties
26+
@properties ||= []
27+
end
2628

2729
def property(name, type, optional: nil, description: nil)
2830
@properties ||= []

spec/example/config/initializers/anchor.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Anchor
99
return Types::Literal.new("never") if column.name == "loljk"
1010
Types::Inference::ActiveRecord::SQL.default_ar_column_to_type(column)
1111
}
12+
13+
c.empty_relationship_type = -> { Anchor::Types::Object }
1214
end
1315
end
1416

spec/example/test/files/all_fields_false_schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type Comment = {
1212
id: number;
1313
type: "comments";
1414
createdAt: string;
15+
relationships: {};
1516
};
1617

1718
export type User = {
@@ -87,6 +88,7 @@ export type Exhaustive = {
8788
resourceOverridden: unknown;
8889
/** This is a comment. */
8990
withComment: Maybe<string>;
91+
relationships: {};
9092
meta: {
9193
some_count: number;
9294
extra_stuff: string;

spec/example/test/files/excluded_fields_schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type Comment = {
1212
id: number;
1313
type: "comments";
1414
createdAt: string;
15+
relationships: {};
1516
};
1617

1718
export type User = {
@@ -85,6 +86,7 @@ export type Exhaustive = {
8586
resourceOverridden: unknown;
8687
/** This is a comment. */
8788
withComment: Maybe<string>;
89+
relationships: {};
8890
meta: {
8991
some_count: number;
9092
extra_stuff: string;

spec/example/test/files/schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export type Exhaustive = {
9595
resourceOverridden: unknown;
9696
/** This is a comment. */
9797
withComment: Maybe<string>;
98+
relationships: {};
9899
meta: {
99100
some_count: number;
100101
extra_stuff: string;

spec/example/test/files/test_schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export type Exhaustive = {
9292
resourceOverridden: unknown;
9393
/** This is a comment. */
9494
withComment: Maybe<string>;
95+
relationships: {};
9596
meta: {
9697
some_count: number;
9798
extra_stuff: string;

0 commit comments

Comments
 (0)