2020-08-14 09:39:59 +12:00
defmodule AshGraphql.Resource.Query do
2020-08-15 02:20:47 +12:00
@moduledoc " Represents a configured query on a resource "
2021-06-30 04:17:33 +12:00
defstruct [
:name ,
:action ,
:type ,
:identity ,
:allow_nil? ,
:modify_resolution ,
2024-02-07 03:46:09 +13:00
:relay_id_translations ,
2022-09-21 15:07:30 +12:00
as_mutation? : false ,
2024-03-12 03:46:42 +13:00
hide_inputs : [ ] ,
2023-01-29 06:32:21 +13:00
metadata_names : [ ] ,
metadata_types : [ ] ,
2024-03-14 05:10:30 +13:00
paginate_with : :keyset ,
2023-01-29 06:32:21 +13:00
show_metadata : nil ,
type_name : nil ,
2022-09-21 15:07:30 +12:00
relay? : false
2021-06-30 04:17:33 +12:00
]
2020-08-14 09:39:59 +12:00
2023-01-29 06:32:21 +13:00
@query_schema [
2020-08-14 09:39:59 +12:00
name : [
type : :atom ,
doc : " The name to use for the query. " ,
default : :get
] ,
action : [
type : :atom ,
doc : " The action to use for the query. " ,
required : true
2020-11-18 20:14:33 +13:00
] ,
2023-01-29 06:32:21 +13:00
type_name : [
2020-11-18 20:14:33 +13:00
type : :atom ,
2021-06-30 04:17:33 +12:00
doc : """
2023-09-16 05:45:12 +12:00
Override the type name returned by this query . Must be set if the read action has ` metadata ` that is not hidden via the ` show_metadata ` key .
2021-06-30 04:17:33 +12:00
"""
] ,
2023-01-29 06:32:21 +13:00
metadata_names : [
type : :keyword_list ,
default : [ ] ,
doc : " Name overrides for metadata fields on the read action. "
] ,
metadata_types : [
type : :keyword_list ,
default : [ ] ,
doc : " Type overrides for metadata fields on the read action. "
] ,
show_metadata : [
type : { :list , :atom } ,
doc : " The metadata attributes to show. Defaults to all. "
] ,
2021-06-30 04:17:33 +12:00
as_mutation? : [
type : :boolean ,
default : false ,
doc : """
2023-09-16 05:45:12 +12:00
Places the query in the ` mutations ` key instead . Not typically necessary , but is often paired with ` as_mutation? ` . See the [ the guide ] ( / documentation / topics / modifying - the - resolution . html ) for more .
2021-06-30 04:17:33 +12:00
"""
2024-02-07 03:46:09 +13:00
] ,
relay_id_translations : [
type : :keyword_list ,
doc : """
A keyword list indicating arguments or attributes that have to be translated from global Relay IDs to internal IDs . See the [ Relay guide ] ( / documentation / topics / relay . md #translating-relay-global-ids-passed-as-arguments) for more.
""" ,
default : [ ]
2024-03-12 03:46:42 +13:00
] ,
hide_inputs : [
type : { :list , :atom } ,
doc : " A list of inputs to hide from the mutation. " ,
default : [ ]
2020-08-14 09:39:59 +12:00
]
]
2023-01-29 06:32:21 +13:00
@get_schema [
identity : [
type : :atom ,
doc :
" The identity to use for looking up the record. Pass `false` to not use an identity. " ,
required : false
] ,
allow_nil? : [
type : :boolean ,
default : true ,
doc : " Whether or not the action can return nil. "
] ,
modify_resolution : [
type : :mfa ,
doc : """
2023-09-16 05:45:12 +12:00
An MFA that will be called with the resolution , the query , and the result of the action as the first three arguments . See the [ the guide ] ( / documentation / topics / modifying - the - resolution . html ) for more .
2023-01-29 06:32:21 +13:00
"""
]
]
2024-04-02 07:03:06 +13:00
|> Spark.Options . merge ( @query_schema , " Shared Query Options " )
2023-01-29 06:32:21 +13:00
2021-04-04 19:10:50 +12:00
@read_one_schema [
2023-01-29 06:32:21 +13:00
allow_nil? : [
type : :boolean ,
default : true ,
doc : " Whether or not the action can return nil. "
]
]
2024-04-02 07:03:06 +13:00
|> Spark.Options . merge ( @query_schema , " Shared Query Options " )
2021-04-04 19:10:50 +12:00
2020-08-14 09:39:59 +12:00
@list_schema [
2023-01-29 06:32:21 +13:00
relay? : [
type : :boolean ,
default : false ,
doc : """
2023-09-16 05:45:12 +12:00
If true , the graphql queries / resolvers for this resource will be built to honor the relay specification . See [ the relay guide ] ( / documentation / topics / relay . html ) for more .
2023-01-29 06:32:21 +13:00
"""
2024-03-14 05:10:30 +13:00
] ,
paginate_with : [
type : { :one_of , [ :keyset , :offset , nil ] } ,
default : :keyset ,
doc : """
Determine the pagination strategy to use , if multiple are available . If ` nil ` , no pagination is applied , otherwise the given strategy is used .
"""
2023-01-29 06:32:21 +13:00
]
]
2024-04-02 07:03:06 +13:00
|> Spark.Options . merge ( @query_schema , " Shared Query Options " )
2020-08-14 09:39:59 +12:00
def get_schema , do : @get_schema
2021-04-04 19:10:50 +12:00
def read_one_schema , do : @read_one_schema
2020-08-14 09:39:59 +12:00
def list_schema , do : @list_schema
end