2020-05-02 10:35:12 +12:00
|
|
|
defmodule AshGraphql.Api do
|
2020-08-14 09:39:59 +12:00
|
|
|
@graphql %Ash.Dsl.Section{
|
|
|
|
name: :graphql,
|
|
|
|
describe: """
|
|
|
|
Global configuration for graphql
|
|
|
|
""",
|
2020-12-27 19:49:35 +13:00
|
|
|
examples: [
|
|
|
|
"""
|
|
|
|
graphql do
|
|
|
|
authorize? false # To skip authorization for this API
|
|
|
|
end
|
|
|
|
"""
|
|
|
|
],
|
2020-08-14 09:39:59 +12:00
|
|
|
schema: [
|
|
|
|
authorize?: [
|
|
|
|
type: :boolean,
|
|
|
|
doc: "Whether or not to perform authorization for this API",
|
|
|
|
default: true
|
2021-02-24 06:28:19 +13:00
|
|
|
],
|
2021-09-10 03:49:36 +12:00
|
|
|
root_level_errors?: [
|
|
|
|
type: :boolean,
|
|
|
|
default: false,
|
|
|
|
doc:
|
|
|
|
"By default, mutation errors are shown in their result object's errors key, but this setting places those errors in the top level errors list"
|
|
|
|
],
|
|
|
|
show_raised_errors?: [
|
|
|
|
type: :boolean,
|
|
|
|
default: false,
|
|
|
|
doc:
|
|
|
|
"For security purposes, if an error is *raised* then Ash simply shows a generic error. If you want to show those errors, set this to true."
|
|
|
|
],
|
2021-05-15 04:25:43 +12:00
|
|
|
stacktraces?: [
|
|
|
|
type: :boolean,
|
|
|
|
doc: "Whether or not to include stacktraces in generated errors",
|
2021-06-05 07:07:12 +12:00
|
|
|
default: true
|
2021-05-15 04:25:43 +12:00
|
|
|
],
|
2021-02-24 06:28:19 +13:00
|
|
|
debug?: [
|
|
|
|
type: :boolean,
|
|
|
|
doc: "Whether or not to log (extremely verbose) debug information",
|
|
|
|
default: false
|
2020-08-14 09:39:59 +12:00
|
|
|
]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2020-12-27 19:49:35 +13:00
|
|
|
@sections [@graphql]
|
|
|
|
|
|
|
|
@moduledoc """
|
|
|
|
The entrypoint for adding graphql behavior to an Ash API
|
|
|
|
|
|
|
|
# Table of Contents
|
|
|
|
#{Ash.Dsl.Extension.doc_index(@sections)}
|
|
|
|
|
|
|
|
#{Ash.Dsl.Extension.doc(@sections)}
|
|
|
|
"""
|
|
|
|
|
|
|
|
use Ash.Dsl.Extension, sections: @sections
|
2020-08-14 09:39:59 +12:00
|
|
|
|
|
|
|
def authorize?(api) do
|
2020-09-24 12:54:57 +12:00
|
|
|
Extension.get_opt(api, [:graphql], :authorize?, true)
|
2020-08-14 09:39:59 +12:00
|
|
|
end
|
|
|
|
|
2021-09-10 03:49:36 +12:00
|
|
|
def root_level_errors?(api) do
|
|
|
|
Extension.get_opt(api, [:graphql], :root_level_errors?, false, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_raised_errors?(api) do
|
|
|
|
Extension.get_opt(api, [:graphql], :show_raised_errors?, false, true)
|
|
|
|
end
|
|
|
|
|
2021-02-24 06:28:19 +13:00
|
|
|
def debug?(api) do
|
|
|
|
Extension.get_opt(api, [:graphql], :debug?, false)
|
|
|
|
end
|
|
|
|
|
2021-05-15 04:25:43 +12:00
|
|
|
def stacktraces?(api) do
|
|
|
|
Extension.get_opt(api, [:graphql], :stacktraces?, false)
|
|
|
|
end
|
|
|
|
|
2020-08-14 09:39:59 +12:00
|
|
|
@doc false
|
|
|
|
def queries(api, schema) do
|
|
|
|
api
|
|
|
|
|> Ash.Api.resources()
|
|
|
|
|> Enum.flat_map(&AshGraphql.Resource.queries(api, &1, schema))
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def mutations(api, schema) do
|
|
|
|
api
|
|
|
|
|> Ash.Api.resources()
|
|
|
|
|> Enum.filter(fn resource ->
|
2021-02-19 04:16:00 +13:00
|
|
|
AshGraphql.Resource in Ash.Resource.Info.extensions(resource)
|
2020-08-14 09:39:59 +12:00
|
|
|
end)
|
2021-03-13 03:39:59 +13:00
|
|
|
|> Enum.flat_map(&AshGraphql.Resource.mutations(api, &1, schema))
|
2020-08-14 09:39:59 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2020-12-01 13:04:59 +13:00
|
|
|
def type_definitions(api, schema) do
|
2020-08-14 09:39:59 +12:00
|
|
|
resource_types =
|
|
|
|
api
|
|
|
|
|> Ash.Api.resources()
|
|
|
|
|> Enum.flat_map(fn resource ->
|
2021-04-24 10:44:56 +12:00
|
|
|
if AshGraphql.Resource in Ash.Resource.Info.extensions(resource) do
|
|
|
|
AshGraphql.Resource.type_definitions(resource, api, schema) ++
|
|
|
|
AshGraphql.Resource.mutation_types(resource, schema)
|
|
|
|
else
|
|
|
|
AshGraphql.Resource.no_graphql_types(resource, schema)
|
|
|
|
end
|
2020-08-14 09:39:59 +12:00
|
|
|
end)
|
|
|
|
|
2021-08-04 10:56:08 +12:00
|
|
|
if Enum.any?(Ash.Api.resources(api), &AshGraphql.Resource.relay?/1) do
|
|
|
|
%Absinthe.Blueprint.Schema.InterfaceTypeDefinition{
|
|
|
|
description: "A relay node",
|
|
|
|
name: "Node",
|
|
|
|
fields: [
|
|
|
|
%Absinthe.Blueprint.Schema.FieldDefinition{
|
|
|
|
description: "A unique identifier",
|
|
|
|
identifier: :id,
|
|
|
|
module: schema,
|
|
|
|
name: "id",
|
2021-09-04 05:30:47 +12:00
|
|
|
type: %Absinthe.Blueprint.TypeReference.NonNull{of_type: :id}
|
2021-08-04 10:56:08 +12:00
|
|
|
}
|
|
|
|
],
|
|
|
|
identifier: :node,
|
|
|
|
module: schema
|
|
|
|
}
|
|
|
|
else
|
|
|
|
resource_types
|
|
|
|
end
|
2020-12-01 13:04:59 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
def global_type_definitions(schema) do
|
2021-04-06 05:08:39 +12:00
|
|
|
[mutation_error(schema), sort_order(schema)]
|
2020-08-14 09:39:59 +12:00
|
|
|
end
|
|
|
|
|
2020-11-06 14:59:06 +13:00
|
|
|
defp sort_order(schema) do
|
|
|
|
%Absinthe.Blueprint.Schema.EnumTypeDefinition{
|
|
|
|
module: schema,
|
|
|
|
name: "SortOrder",
|
|
|
|
values: [
|
|
|
|
%Absinthe.Blueprint.Schema.EnumValueDefinition{
|
|
|
|
module: schema,
|
|
|
|
identifier: :desc,
|
|
|
|
name: "DESC",
|
|
|
|
value: :desc
|
|
|
|
},
|
|
|
|
%Absinthe.Blueprint.Schema.EnumValueDefinition{
|
|
|
|
module: schema,
|
|
|
|
identifier: :asc,
|
|
|
|
name: "ASC",
|
|
|
|
value: :asc
|
|
|
|
}
|
|
|
|
],
|
|
|
|
identifier: :sort_order
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-08-14 09:39:59 +12:00
|
|
|
defp mutation_error(schema) do
|
|
|
|
%Absinthe.Blueprint.Schema.ObjectTypeDefinition{
|
|
|
|
description: "An error generated by a failed mutation",
|
|
|
|
fields: error_fields(schema),
|
|
|
|
identifier: :mutation_error,
|
|
|
|
module: schema,
|
|
|
|
name: "MutationError"
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp error_fields(schema) do
|
|
|
|
[
|
|
|
|
%Absinthe.Blueprint.Schema.FieldDefinition{
|
|
|
|
description: "The human readable error message",
|
|
|
|
identifier: :message,
|
|
|
|
module: schema,
|
|
|
|
name: "message",
|
|
|
|
type: :string
|
|
|
|
},
|
2021-06-29 05:33:01 +12:00
|
|
|
%Absinthe.Blueprint.Schema.FieldDefinition{
|
|
|
|
description: "A shorter error message, with vars not replaced",
|
|
|
|
identifier: :short_message,
|
|
|
|
module: schema,
|
|
|
|
name: "short_message",
|
|
|
|
type: :string
|
|
|
|
},
|
|
|
|
%Absinthe.Blueprint.Schema.FieldDefinition{
|
|
|
|
description: "Replacements for the short message",
|
|
|
|
identifier: :vars,
|
|
|
|
module: schema,
|
|
|
|
name: "vars",
|
|
|
|
type: :json
|
|
|
|
},
|
2020-08-14 09:39:59 +12:00
|
|
|
%Absinthe.Blueprint.Schema.FieldDefinition{
|
|
|
|
description: "An error code for the given error",
|
|
|
|
identifier: :code,
|
|
|
|
module: schema,
|
|
|
|
name: "code",
|
|
|
|
type: :string
|
|
|
|
},
|
|
|
|
%Absinthe.Blueprint.Schema.FieldDefinition{
|
|
|
|
description: "The field or fields that produced the error",
|
|
|
|
identifier: :fields,
|
|
|
|
module: schema,
|
|
|
|
name: "fields",
|
|
|
|
type: %Absinthe.Blueprint.TypeReference.List{
|
|
|
|
of_type: :string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2020-05-02 10:35:12 +12:00
|
|
|
end
|
|
|
|
end
|