Skip to content

Commit 2c1270f

Browse files
committed
improvement: use more descriptions from resources
1 parent e238c87 commit 2c1270f

File tree

3 files changed

+155
-95
lines changed

3 files changed

+155
-95
lines changed

lib/ash_graphql.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ defmodule AshGraphql do
393393
%Absinthe.Blueprint.Schema.EnumTypeDefinition{
394394
module: schema,
395395
name: name,
396+
description: AshGraphql.Type.description(type, []),
396397
values:
397398
Enum.map(type.values(), fn value ->
398399
name =

lib/resource/resource.ex

Lines changed: 130 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,6 +3307,8 @@ defmodule AshGraphql.Resource do
33073307
attribute.type.graphql_input_type(attribute.constraints)
33083308
end
33093309

3310+
description = AshGraphql.Type.description(attribute.type, attribute.constraints)
3311+
33103312
constraints = Ash.Type.NewType.constraints(attribute.type, attribute.constraints)
33113313

33123314
already_checked =
@@ -3319,12 +3321,19 @@ defmodule AshGraphql.Resource do
33193321
[
33203322
type_name
33213323
]
3322-
|> define_map_types(constraints, schema, resource, env, already_checked)
3324+
|> define_map_types(description, constraints, schema, resource, env, already_checked)
33233325
|> Enum.concat(
33243326
[
33253327
input_type_name
33263328
]
3327-
|> define_input_map_types(constraints, schema, resource, env, already_checked)
3329+
|> define_input_map_types(
3330+
description,
3331+
constraints,
3332+
schema,
3333+
resource,
3334+
env,
3335+
already_checked
3336+
)
33283337
)
33293338
end)
33303339
else
@@ -3333,7 +3342,15 @@ defmodule AshGraphql.Resource do
33333342
end
33343343

33353344
# sobelow_skip ["DOS.StringToAtom"]
3336-
defp define_map_types(type_names, constraints, schema, resource, env, already_checked) do
3345+
defp define_map_types(
3346+
type_names,
3347+
description,
3348+
constraints,
3349+
schema,
3350+
resource,
3351+
env,
3352+
already_checked
3353+
) do
33373354
type_names
33383355
|> Enum.filter(& &1)
33393356
|> Enum.flat_map(fn type_name ->
@@ -3370,6 +3387,7 @@ defmodule AshGraphql.Resource do
33703387
{
33713388
define_map_types(
33723389
[nested_type_name],
3390+
AshGraphql.Type.description(map_type, map_constraints),
33733391
map_constraints,
33743392
schema,
33753393
resource,
@@ -3382,6 +3400,7 @@ defmodule AshGraphql.Resource do
33823400
identifier: name,
33833401
__reference__: AshGraphql.Resource.ref(env),
33843402
name: to_string(name),
3403+
description: attribute[:description],
33853404
middleware:
33863405
middleware_for_field(
33873406
resource,
@@ -3420,12 +3439,14 @@ defmodule AshGraphql.Resource do
34203439
identifier: name,
34213440
__reference__: AshGraphql.Resource.ref(env),
34223441
name: to_string(name),
3442+
description: attribute[:description],
34233443
middleware:
34243444
middleware_for_field(
34253445
resource,
34263446
%{
34273447
name: name,
34283448
type: attribute[:type],
3449+
description: attribute[:description],
34293450
constraints: attribute[:constraints] || []
34303451
},
34313452
name,
@@ -3444,6 +3465,7 @@ defmodule AshGraphql.Resource do
34443465
%{
34453466
name: name,
34463467
type: attribute[:type],
3468+
description: attribute[:description],
34473469
constraints: Keyword.get(attribute, :constraints) || []
34483470
},
34493471
resource,
@@ -3457,6 +3479,7 @@ defmodule AshGraphql.Resource do
34573479
%{
34583480
name: name,
34593481
type: attribute[:type],
3482+
description: attribute[:description],
34603483
constraints: Keyword.get(attribute, :constraints) || []
34613484
},
34623485
resource,
@@ -3476,6 +3499,7 @@ defmodule AshGraphql.Resource do
34763499
module: schema,
34773500
name: type_name |> to_string() |> Macro.camelize(),
34783501
fields: fields,
3502+
description: description,
34793503
identifier: type_name,
34803504
__reference__: ref(__ENV__)
34813505
}
@@ -3517,6 +3541,7 @@ defmodule AshGraphql.Resource do
35173541
# sobelow_skip ["DOS.StringToAtom"]
35183542
defp define_input_map_types(
35193543
input_type_names,
3544+
description,
35203545
constraints,
35213546
schema,
35223547
resource,
@@ -3527,112 +3552,117 @@ defmodule AshGraphql.Resource do
35273552
|> Enum.filter(& &1)
35283553
|> Enum.flat_map(fn type_name ->
35293554
{types, fields, _} =
3530-
Enum.reduce(constraints[:fields], {[], [], already_checked}, fn {name, attribute},
3531-
{types, fields,
3532-
already_checked} ->
3533-
new_type? = Ash.Type.NewType.new_type?(attribute[:type])
3534-
{map_type?, map_type, map_constraints} = map_type(attribute, new_type?)
3535-
3536-
map_constraints =
3537-
if new_type? do
3538-
Ash.Type.NewType.constraints(map_type, map_constraints)
3539-
else
3540-
map_constraints
3541-
end
3555+
Enum.reduce(constraints[:fields], {[], [], already_checked}, fn
3556+
{name, attribute}, {types, fields, already_checked} ->
3557+
new_type? = Ash.Type.NewType.new_type?(attribute[:type])
3558+
{map_type?, map_type, map_constraints} = map_type(attribute, new_type?)
35423559

3543-
if new_type? && map_type in already_checked do
3544-
{types, fields, already_checked}
3545-
else
3546-
already_checked =
3560+
map_constraints =
35473561
if new_type? do
3548-
[map_type | already_checked]
3562+
Ash.Type.NewType.constraints(map_type, map_constraints)
35493563
else
3550-
already_checked
3564+
map_constraints
35513565
end
35523566

3553-
if map_type? && map_constraints[:fields] do
3554-
nested_type_name =
3555-
String.to_atom(
3556-
"#{Atom.to_string(type_name) |> String.replace("_input", "")}_#{Atom.to_string(name)}_input"
3557-
)
3567+
if new_type? && map_type in already_checked do
3568+
{types, fields, already_checked}
3569+
else
3570+
already_checked =
3571+
if new_type? do
3572+
[map_type | already_checked]
3573+
else
3574+
already_checked
3575+
end
35583576

3559-
nested_type = array_to_list_of(attribute[:type], nested_type_name)
3577+
if map_type? && map_constraints[:fields] do
3578+
nested_type_name =
3579+
String.to_atom(
3580+
"#{Atom.to_string(type_name) |> String.replace("_input", "")}_#{Atom.to_string(name)}_input"
3581+
)
35603582

3561-
{
3562-
define_input_map_types(
3563-
[nested_type_name],
3564-
map_constraints,
3565-
schema,
3566-
resource,
3567-
env,
3583+
nested_type = array_to_list_of(attribute[:type], nested_type_name)
3584+
3585+
{
3586+
define_input_map_types(
3587+
[nested_type_name],
3588+
AshGraphql.Type.description(map_type, map_constraints),
3589+
map_constraints,
3590+
schema,
3591+
resource,
3592+
env,
3593+
already_checked
3594+
) ++ types,
3595+
[
3596+
%Absinthe.Blueprint.Schema.InputValueDefinition{
3597+
module: schema,
3598+
identifier: name,
3599+
description: attribute[:description],
3600+
__reference__: AshGraphql.Resource.ref(env),
3601+
name: to_string(name),
3602+
type:
3603+
if Keyword.get(attribute, :allow_nil?, true) do
3604+
nested_type
3605+
else
3606+
%Absinthe.Blueprint.TypeReference.NonNull{
3607+
of_type: nested_type
3608+
}
3609+
end
3610+
}
3611+
| fields
3612+
],
35683613
already_checked
3569-
) ++ types,
3570-
[
3571-
%Absinthe.Blueprint.Schema.InputValueDefinition{
3572-
module: schema,
3573-
identifier: name,
3574-
__reference__: AshGraphql.Resource.ref(env),
3575-
name: to_string(name),
3576-
type:
3577-
if Keyword.get(attribute, :allow_nil?, true) do
3578-
nested_type
3579-
else
3580-
%Absinthe.Blueprint.TypeReference.NonNull{
3581-
of_type: nested_type
3582-
}
3583-
end
3584-
}
3585-
| fields
3586-
],
3587-
already_checked
3588-
}
3589-
else
3590-
{types,
3591-
[
3592-
%Absinthe.Blueprint.Schema.InputValueDefinition{
3593-
module: schema,
3594-
identifier: name,
3595-
__reference__: AshGraphql.Resource.ref(env),
3596-
name: to_string(name),
3597-
type:
3598-
if Keyword.get(attribute, :allow_nil?, true) do
3599-
do_field_type(
3600-
attribute[:type],
3601-
%{
3602-
name: name,
3603-
type: attribute[:type],
3604-
constraints: Keyword.get(attribute, :constraints) || []
3605-
},
3606-
resource,
3607-
true
3608-
)
3609-
else
3610-
%Absinthe.Blueprint.TypeReference.NonNull{
3611-
of_type:
3612-
do_field_type(
3613-
attribute[:type],
3614-
%{
3615-
name: name,
3616-
type: attribute[:type],
3617-
constraints: Keyword.get(attribute, :constraints) || []
3618-
},
3619-
resource,
3620-
true
3621-
)
3622-
}
3623-
end
3624-
}
3625-
| fields
3626-
], already_checked}
3614+
}
3615+
else
3616+
{types,
3617+
[
3618+
%Absinthe.Blueprint.Schema.InputValueDefinition{
3619+
module: schema,
3620+
identifier: name,
3621+
__reference__: AshGraphql.Resource.ref(env),
3622+
name: to_string(name),
3623+
description: attribute[:description],
3624+
type:
3625+
if Keyword.get(attribute, :allow_nil?, true) do
3626+
do_field_type(
3627+
attribute[:type],
3628+
%{
3629+
name: name,
3630+
type: attribute[:type],
3631+
description: attribute[:description],
3632+
constraints: Keyword.get(attribute, :constraints) || []
3633+
},
3634+
resource,
3635+
true
3636+
)
3637+
else
3638+
%Absinthe.Blueprint.TypeReference.NonNull{
3639+
of_type:
3640+
do_field_type(
3641+
attribute[:type],
3642+
%{
3643+
name: name,
3644+
type: attribute[:type],
3645+
description: attribute[:description],
3646+
constraints: Keyword.get(attribute, :constraints) || []
3647+
},
3648+
resource,
3649+
true
3650+
)
3651+
}
3652+
end
3653+
}
3654+
| fields
3655+
], already_checked}
3656+
end
36273657
end
3628-
end
36293658
end)
36303659

36313660
[
36323661
%Absinthe.Blueprint.Schema.InputObjectTypeDefinition{
36333662
module: schema,
36343663
name: type_name |> to_string() |> Macro.camelize(),
36353664
fields: fields,
3665+
description: description,
36363666
identifier: type_name,
36373667
__reference__: ref(__ENV__)
36383668
}
@@ -3690,7 +3720,8 @@ defmodule AshGraphql.Resource do
36903720
%{
36913721
attribute
36923722
| name: nested_union_type_name(attribute, name),
3693-
constraints: config[:constraints]
3723+
constraints: config[:constraints],
3724+
description: config[:description]
36943725
},
36953726
resource,
36963727
false
@@ -3718,12 +3749,14 @@ defmodule AshGraphql.Resource do
37183749
name: input_type_name |> to_string() |> Macro.camelize(),
37193750
identifier: String.to_atom(to_string(input_type_name)),
37203751
__reference__: ref(env),
3752+
description: AshGraphql.Type.description(attribute.type, attribute.constraints),
37213753
fields:
37223754
Enum.map(constraints[:types], fn {name, config} ->
37233755
%Absinthe.Blueprint.Schema.InputValueDefinition{
37243756
name: name |> to_string(),
37253757
identifier: name,
37263758
__reference__: ref(env),
3759+
description: config[:description],
37273760
type:
37283761
field_type(
37293762
config[:type],
@@ -3741,12 +3774,13 @@ defmodule AshGraphql.Resource do
37413774
|> Enum.reject(fn {name, _} ->
37423775
name in grapqhl_unnested_unions
37433776
end)
3744-
|> Enum.map(fn {name, _} ->
3777+
|> Enum.map(fn {name, config} ->
37453778
%Absinthe.Blueprint.Schema.ObjectTypeDefinition{
37463779
module: schema,
37473780
name: "#{type_name}_#{name}" |> Macro.camelize(),
37483781
identifier: :"#{type_name}_#{name}",
37493782
__reference__: ref(env),
3783+
description: config[:description],
37503784
fields: [
37513785
%Absinthe.Blueprint.Schema.FieldDefinition{
37523786
identifier: :value,
@@ -3766,6 +3800,7 @@ defmodule AshGraphql.Resource do
37663800
module: schema,
37673801
name: type_name |> to_string() |> Macro.camelize(),
37683802
resolve_type: func,
3803+
description: AshGraphql.Type.description(attribute.type, attribute.constraints),
37693804
types:
37703805
Enum.map(constraints[:types], fn {name, _config} ->
37713806
if name in grapqhl_unnested_unions do

0 commit comments

Comments
 (0)