Skip to content

Commit

Permalink
feat: argument_input_types (#176)
Browse files Browse the repository at this point in the history
* feat: argument_input_types

* use mix task to update formatter.exs

* update CheatSheet
  • Loading branch information
barnabasJ authored Jun 10, 2024
1 parent b399d06 commit 8089164
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions documentation/dsls/DSL:-AshGraphql.Resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
7 changes: 6 additions & 1 deletion lib/resource/info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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, "-")
Expand Down
17 changes: 14 additions & 3 deletions lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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: "~",
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions test/resource_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions test/support/resources/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8089164

Please sign in to comment.