feat: argument_input_types (#176)

* feat: argument_input_types

* use mix task to update formatter.exs

* update CheatSheet
This commit is contained in:
Barnabas Jovanovics 2024-06-10 19:32:37 +02:00 committed by GitHub
parent b399d06a86
commit 8089164d74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 53 additions and 4 deletions

View file

@ -3,6 +3,7 @@ spark_locals_without_parens = [
action: 3,
action: 4,
allow_nil?: 1,
argument_input_types: 1,
argument_names: 1,
as_mutation?: 1,
attribute_input_types: 1,

View file

@ -64,6 +64,7 @@ end
| [`keyset_field`](#graphql-keyset_field){: #graphql-keyset_field } | `atom` | | If set, the keyset will be displayed on all read actions in this field. It will be `nil` unless at least one of the read actions on a resource uses keyset pagination or it is the result of a mutation |
| [`attribute_types`](#graphql-attribute_types){: #graphql-attribute_types } | `keyword` | | A keyword list of type overrides for attributes. The type overrides should refer to types available in the graphql (absinthe) schema. `list_of/1` and `non_null/1` helpers can be used. |
| [`attribute_input_types`](#graphql-attribute_input_types){: #graphql-attribute_input_types } | `keyword` | | A keyword list of input type overrides for attributes. The type overrides should refer to types available in the graphql (absinthe) schema. `list_of/1` and `non_null/1` helpers can be used. |
| [`argument_input_types`](#graphql-argument_input_types){: #graphql-argument_input_types } | `keyword` | | A keyword list of actions and their input type overrides for arguments. The type overrides should refer to types available in the graphql (absinthe) schema. `list_of/1` and `non_null/1` helpers can be used. |
| [`primary_key_delimiter`](#graphql-primary_key_delimiter){: #graphql-primary_key_delimiter } | `String.t` | `"~"` | If a composite primary key exists, this can be set to determine delimiter used in the `id` field value. |
| [`depth_limit`](#graphql-depth_limit){: #graphql-depth_limit } | `integer` | | A simple way to prevent massive queries. |
| [`generate_object?`](#graphql-generate_object?){: #graphql-generate_object? } | `boolean` | `true` | Whether or not to create the GraphQL object, this allows you to manually create the GraphQL object. |

View file

@ -159,11 +159,16 @@ defmodule AshGraphql.Resource.Info do
Extension.get_opt(resource, [:graphql], :argument_names, [])
end
@doc "Graphql type overrides for the resource"
@doc "Graphql attribute input type overrides for the resource"
def attribute_input_types(resource) do
Extension.get_opt(resource, [:graphql], :attribute_input_types, [])
end
@doc "Graphql argument type overrides for the resource"
def argument_input_types(resource) do
Extension.get_opt(resource, [:graphql], :argument_input_types, [])
end
@doc "The delimiter for a resource with a composite primary key"
def primary_key_delimiter(resource) do
Extension.get_opt(resource, [:graphql], :primary_key_delimiter, "-")

View file

@ -360,6 +360,11 @@ defmodule AshGraphql.Resource do
doc:
"A keyword list of input type overrides for attributes. The type overrides should refer to types available in the graphql (absinthe) schema. `list_of/1` and `non_null/1` helpers can be used."
],
argument_input_types: [
type: :keyword_list,
doc:
"A keyword list of actions and their input type overrides for arguments. The type overrides should refer to types available in the graphql (absinthe) schema. `list_of/1` and `non_null/1` helpers can be used."
],
primary_key_delimiter: [
type: :string,
default: "~",
@ -1185,9 +1190,15 @@ defmodule AshGraphql.Resource do
case find_manage_change(argument, action, resource) do
nil ->
type =
argument.type
|> field_type(argument, resource, true)
|> maybe_wrap_non_null(argument_required?(argument))
case AshGraphql.Resource.Info.argument_input_types(resource)[action.name][name] do
nil ->
argument.type
|> field_type(argument, resource, true)
|> maybe_wrap_non_null(argument_required?(argument))
override ->
unwrap_literal_type(override)
end
%Absinthe.Blueprint.Schema.FieldDefinition{
identifier: name,

View file

@ -118,4 +118,29 @@ defmodule AshGraphql.ResourceTest do
assert bar_with_foo["type"] == %{"name" => "BarWithFoo"}
assert bar_with_baz["type"] == %{"name" => "BarWithBaz"}
end
test "arguments can have their types overriden" do
{:ok, %{data: with_foo}} =
"""
query {
__type(name: "CreatePostBarWithFooWithMapInput") {
name
inputFields {
name
type {
name
kind
}
}
}
}
"""
|> Absinthe.run(AshGraphql.Test.Schema)
bar_with_foo =
with_foo["__type"]["inputFields"]
|> Enum.find(&(&1["name"] == "bar"))
assert bar_with_foo["type"] == %{"name" => "BarWithFoo", "kind" => "INPUT_OBJECT"}
end
end

View file

@ -164,6 +164,7 @@ defmodule AshGraphql.Test.Post do
attribute_types integer_as_string_in_domain: :string
attribute_input_types integer_as_string_in_domain: :string
argument_input_types create_bar_with_foo_with_map: [bar: :bar_with_foo]
field_names text_1_and_2: :text1_and2
keyset_field :keyset
@ -201,6 +202,7 @@ defmodule AshGraphql.Test.Post do
create :create_post_with_common_map, :create_with_common_map
create :create_post_bar_with_foo, :create_bar_with_foo
create :create_post_bar_with_foo_with_map, :create_bar_with_foo_with_map
create :create_post_bar_with_baz, :create_bar_with_baz
create :create_post_with_comments, :with_comments
@ -245,6 +247,10 @@ defmodule AshGraphql.Test.Post do
argument(:bar, BarWithFoo)
end
create :create_bar_with_foo_with_map do
argument(:bar, :map)
end
create :create_bar_with_baz do
argument(:bar, BarWithBaz)
end