Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvement: make mutation arguments non-null #111

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ config :ash, :validate_api_resource_inclusion?, false
config :ash, :validate_api_config_inclusion?, false

config :ash_graphql, :default_managed_relationship_type_name_template, :action_name
config :ash_graphql, :allow_non_null_mutation_arguments?, true

if Mix.env() == :dev do
config :git_ops,
Expand Down
1 change: 1 addition & 0 deletions documentation/tutorials/getting-started-with-graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ in `config/config.exs`

```elixir
config :ash_graphql, :default_managed_relationship_type_name_template, :action_name
config :ash_graphql, :allow_non_null_mutation_arguments?, true
```

This won't be necessary after the next major release, where this new configuration will be the default.
Expand Down
38 changes: 31 additions & 7 deletions lib/resource/resource.ex
Original file line number Diff line number Diff line change
Expand Up @@ -552,14 +552,14 @@ defmodule AshGraphql.Resource do
[] ->
[]

_ ->
fields ->
[
%Absinthe.Blueprint.Schema.InputValueDefinition{
identifier: :input,
module: schema,
name: "input",
placement: :argument_definition,
type: String.to_atom("#{name}_input")
type: mutation_input_type(name, fields)
}
]
end
Expand Down Expand Up @@ -621,14 +621,14 @@ defmodule AshGraphql.Resource do
[] ->
[]

_ ->
fields ->
[
%Absinthe.Blueprint.Schema.InputValueDefinition{
identifier: :input,
module: schema,
name: "input",
placement: :argument_definition,
type: String.to_atom("#{mutation.name}_input")
type: mutation_input_type(mutation.name, fields)
}
]
end
Expand Down Expand Up @@ -672,15 +672,15 @@ defmodule AshGraphql.Resource do
[] ->
mutation_args(mutation, resource, schema)

_ ->
fields ->
mutation_args(mutation, resource, schema) ++
[
%Absinthe.Blueprint.Schema.InputValueDefinition{
identifier: :input,
module: schema,
name: "input",
placement: :argument_definition,
type: String.to_atom("#{mutation.name}_input"),
type: mutation_input_type(mutation.name, fields),
__reference__: ref(__ENV__)
}
]
Expand Down Expand Up @@ -730,20 +730,44 @@ defmodule AshGraphql.Resource do
|> Enum.concat(mutation_read_args(mutation, resource, schema))
end

@allow_non_null_mutation_arguments? Application.compile_env(
:ash_graphql,
:allow_non_null_mutation_arguments?,
false
)

defp mutation_args(mutation, resource, schema) do
[
%Absinthe.Blueprint.Schema.InputValueDefinition{
identifier: :id,
module: schema,
name: "id",
placement: :argument_definition,
type: :id,
type: maybe_wrap_non_null(:id, @allow_non_null_mutation_arguments?),
__reference__: ref(__ENV__)
}
| mutation_read_args(mutation, resource, schema)
]
end

# sobelow_skip ["DOS.StringToAtom"]
defp mutation_input_type(mutation_name, mutation_fields) do
any_non_null_field? =
mutation_fields
|> Enum.any?(fn
%Absinthe.Blueprint.Schema.FieldDefinition{
type: %Absinthe.Blueprint.TypeReference.NonNull{}
} ->
true

_ ->
false
end)

String.to_atom("#{mutation_name}_input")
|> maybe_wrap_non_null(any_non_null_field? and @allow_non_null_mutation_arguments?)
end

defp mutation_read_args(%{read_action: read_action}, resource, schema) do
read_action =
cond do
Expand Down
2 changes: 1 addition & 1 deletion test/create_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ defmodule AshGraphql.CreateTest do
test "a create with a managed relationship works with many_to_many and [on_lookup: :relate, on_match: :relate]" do
resp =
"""
mutation CreatePostWithCommentsAndTags($input: CreatePostWithCommentsAndTagsInput) {
mutation CreatePostWithCommentsAndTags($input: CreatePostWithCommentsAndTagsInput!) {
createPostWithCommentsAndTags(input: $input) {
result{
text
Expand Down
10 changes: 5 additions & 5 deletions test/destroy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule AshGraphql.DestroyTest do

resp =
"""
mutation DeletePost($id: ID) {
mutation DeletePost($id: ID!) {
deletePost(id: $id) {
result{
text
Expand Down Expand Up @@ -42,7 +42,7 @@ defmodule AshGraphql.DestroyTest do

resp =
"""
mutation ArchivePost($id: ID) {
mutation ArchivePost($id: ID!) {
deletePost(id: $id) {
result{
text
Expand Down Expand Up @@ -96,7 +96,7 @@ defmodule AshGraphql.DestroyTest do

resp =
"""
mutation DeleteWithError($id: ID) {
mutation DeleteWithError($id: ID!) {
deletePostWithError(id: $id) {
result{
text
Expand Down Expand Up @@ -128,7 +128,7 @@ defmodule AshGraphql.DestroyTest do
test "destroying a non-existent record returns a not found error" do
resp =
"""
mutation DeletePost($id: ID) {
mutation DeletePost($id: ID!) {
deletePost(id: $id) {
result{
text
Expand Down Expand Up @@ -162,7 +162,7 @@ defmodule AshGraphql.DestroyTest do

resp =
"""
mutation DeletePost($id: ID) {
mutation DeletePost($id: ID!) {
deletePostWithError(id: $id) {
result{
text
Expand Down
6 changes: 3 additions & 3 deletions test/update_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule AshGraphql.UpdateTest do

resp =
"""
mutation UpdatePost($id: ID, $input: UpdatePostInput) {
mutation UpdatePost($id: ID!, $input: UpdatePostInput) {
updatePost(id: $id, input: $input) {
result{
text
Expand Down Expand Up @@ -209,7 +209,7 @@ defmodule AshGraphql.UpdateTest do

resp =
"""
mutation UpdatePostConfirm($input: UpdatePostConfirmInput, $id: ID) {
mutation UpdatePostConfirm($input: UpdatePostConfirmInput, $id: ID!) {
updatePostConfirm(input: $input, id: $id) {
result{
text
Expand Down Expand Up @@ -253,7 +253,7 @@ defmodule AshGraphql.UpdateTest do

resp =
"""
mutation UpdatePostConfirm($input: UpdatePostConfirmInput, $id: ID) {
mutation UpdatePostConfirm($input: UpdatePostConfirmInput, $id: ID!) {
updatePostConfirm(input: $input, id: $id) {
result{
text
Expand Down
Loading