Skip to content

Commit 724e67d

Browse files
committed
feat: infer default as non-null config
1 parent a422680 commit 724e67d

File tree

9 files changed

+25
-4
lines changed

9 files changed

+25
-4
lines changed

lib/anchor/config.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class Config
88
:empty_relationship_type,
99
:use_type_as_schema_name,
1010
:maybe_as_union,
11-
:array_bracket_notation
11+
:array_bracket_notation,
12+
:infer_default_as_non_null
1213

1314
def initialize
1415
@ar_column_to_type = nil
@@ -20,6 +21,7 @@ def initialize
2021
@use_type_as_schema_name = nil
2122
@maybe_as_union = nil
2223
@array_bracket_notation = nil
24+
@infer_default_as_non_null = nil
2325
end
2426
end
2527
end

lib/anchor/resource.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ def anchor_attributes_properties(included_fields:)
9191
end
9292
end
9393
type.type
94+
elsif type.is_a?(Anchor::Types::Maybe) && Anchor.config.infer_default_as_non_null
95+
column.default.present? || column.default_function.present? && column.instance_variable_get(:@generated).blank? ? type.type : type
9496
else
9597
type
9698
end

spec/example/app/resources/exhaustive_resource.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class AssertedObject < Types::Object
4242
attribute :model_overridden
4343
attribute :resource_overridden
4444
attribute :with_comment
45+
attribute :defaulted_boolean
46+
attribute :defaulted_at
4547

4648
class LinkSchema < Anchor::Types::Object
4749
property :self, Anchor::Types::String

spec/example/config/initializers/anchor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Anchor
33
c.field_case = :camel_without_inflection
44
c.use_active_record_comment = true
55
c.use_active_record_validations = true
6+
c.infer_default_as_non_null = true
67
c.infer_nullable_relationships_as_optional = true
78

89
c.ar_column_to_type = lambda { |column|
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddWithDefaultToExhaustives < ActiveRecord::Migration[8.0]
2+
def change
3+
add_column :exhaustives, :defaulted_boolean, :boolean, default: false
4+
end
5+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddWithDefaultFunctionToExhaustives < ActiveRecord::Migration[8.0]
2+
def change
3+
add_column :exhaustives, :defaulted_at, :datetime, default: -> { "NOW()" }
4+
end
5+
end

spec/example/db/schema.rb

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/example/test/files/multifile/Exhaustive.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ type Model = {
5353
resourceOverridden: unknown;
5454
/** This is a comment. */
5555
withComment: Maybe<string>;
56+
defaultedBoolean: boolean;
57+
defaultedAt: string;
5658
relationships: {};
5759
meta: {
5860
some_count: number;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"shared.ts":"e42275c1453e00f368131821b3e4de915d6c5c789a8fc5c72967ddf8c31ad77b","Comment.ts":"fec6ae5197b65afe3fed510a393b7819e288f33366bc34aca1622833c5030ba6","User.ts":"ce80524eb520622e37af4aa9a2b35e49a50cb80bf4ea9921e818631e8a541738","Post.ts":"74d50a09806ff7496c168f2b9b4d6cea64111f42f38effa12464e5ad933c1303","Exhaustive.ts":"ac90628b54e7278d63839fdd4db3d3cbfae9d9e996fb065e5a5d2b756052fe66"}
1+
{"shared.ts":"e42275c1453e00f368131821b3e4de915d6c5c789a8fc5c72967ddf8c31ad77b","Comment.ts":"fec6ae5197b65afe3fed510a393b7819e288f33366bc34aca1622833c5030ba6","User.ts":"ce80524eb520622e37af4aa9a2b35e49a50cb80bf4ea9921e818631e8a541738","Post.ts":"74d50a09806ff7496c168f2b9b4d6cea64111f42f38effa12464e5ad933c1303","Exhaustive.ts":"ff0ed2ee4d354256bce9fef2cba6ae6f82a024fe1755daba545ec8f68f87bad3"}

0 commit comments

Comments
 (0)