AshGraphql.Resource (ash_graphql v0.16.5) View Source

This Ash resource extension adds configuration for exposing a resource in a graphql.

Table of Contents

  • graphql
    • queries
      • get
      • read_one
      • list
    • mutations
      • create
      • update
      • destroy
    • managed_relationships
      • managed_relationship

graphql

Configuration for a given resource in graphql

Examples:

graphql do
  type :post

  queries do
    get :get_post, :read
    list :list_posts, :read
  end

  mutations do
    create :create_post, :create
    update :update_post, :update
    destroy :destroy_post, :destroy
  end
end

  • :type - Required. The type to use for this entity in the graphql schema

  • :primary_key_delimiter - If a composite primary key exists, this must be set to determine the id field value

queries

Queries (read actions) to expose for the resource.

Examples:

queries do
  get :get_post, :read
  read_one :current_user, :current_user
  list :list_posts, :read
end

get

A query to fetch a record by primary key

Introspection Target:

AshGraphql.Resource.Query

Examples:

get :get_post, :read
  • :name - The name to use for the query. The default value is :get.

  • :action - Required. The action to use for the query.

  • :identity - false The identity to use for looking up the user

  • :allow_nil? - Whether or not the action can return nil. The default value is true.

read_one

A query to fetch a record

Introspection Target:

AshGraphql.Resource.Query

Examples:

read_one :current_user, :current_user
  • :name - The name to use for the query. The default value is :read_one.

  • :action - Required. The action to use for the query.

  • :allow_nil? - Whether or not the action can return nil. The default value is true.

list

A query to fetch a list of records

Introspection Target:

AshGraphql.Resource.Query

Examples:

list :list_posts, :read
  • :name - The name to use for the query. The default value is :list.

  • :action - Required. The action to use for the query.

mutations

Mutations (create/update/destroy actions) to expose for the resource.

Examples:

mutations do
  create :create_post, :create
  update :update_post, :update
  destroy :destroy_post, :destroy
end

create

A mutation to create a record

Introspection Target:

AshGraphql.Resource.Mutation

Examples:

create :create_post, :create
  • :name - The name to use for the mutation. The default value is :get.

  • :action - Required. The action to use for the mutation.

  • :upsert? - Whether or not to use the upsert?: true option when calling YourApi.create/2. The default value is false.

update

A mutation to update a record

Introspection Target:

AshGraphql.Resource.Mutation

Examples:

update :update_post, :update
  • :name - The name to use for the mutation. The default value is :get.

  • :action - Required. The action to use for the mutation.

  • :identity - The identity to use to fetch the record to be updated.

    If no identity is required (e.g for a read action that already knows how to fetch the item to be updated), use false.

  • :read_action - The read action to use to fetch the record to be updated. Defaults to the primary read action.

destroy

A mutation to destroy a record

Introspection Target:

AshGraphql.Resource.Mutation

Examples:

destroy :destroy_post, :destroy
  • :name - The name to use for the mutation. The default value is :get.

  • :action - Required. The action to use for the mutation.

  • :read_action - The read action to use to fetch the record to be destroyed. Defaults to the primary read action.

  • :identity - The identity to use to fetch the record to be destroyed. If no identity is required (e.g for a read action that already knows how to fetch the item to be updated), use false.

managed_relationships

Generates input objects for manage_relationship arguments on reosurce actions.

Examples:

managed_relationships do
  manage_relationship :create_post, :comments
end

managed_relationship

Instructs ash_graphql that a given argument with a manage_relationship change should have its input objects derived automatically from the potential actions to be called.

For example, given an action like:

actions do
  create :create do
    argument :comments, {:array, :map}

    change manage_relationship(:comments, type: :direct_control) # <- we look for this change with a matching argument name
  end
end

You could add the following managed_relationship

graphql do
  ...

  managed_relationships do
    managed_relationship :create_post, :comments
  end
end

By default, the {:array, :map} would simply be a json[] type. If the argument name is placed in this list, all of the potential actions that could be called will be combined into a single input object. If there are type conflicts (for example, if the input could create or update a record, and the create and update actions have an argument of the same name but with a different type), a warning is emitted at compile time and the first one is used. If that is insufficient, you will need to do one of the following:

1.) provide the :types option to the managed_relationship constructor (see that option for more) 2.) define a custom type, with a custom input object (see the custom types guide), and use that custom type instead of :map 3.) change your actions to not have overlapping inputs with different types

Introspection Target:

AshGraphql.Resource.ManagedRelationship

  • :argument - Required. The argument for which an input object should be derived.

  • :action - The action that accepts the argument

  • :type_name - The name of the input object that will be derived. Defaults to <action_type>_<resource>_<argument_name>_input

    Because multiple actions could potentially be managing the same relationship, it isn't suficcient to default to something like <resource>_<relationship>_input. Additionally, Ash doesn't expose resource action names by default, meaning that there is no automatic way to ensure that all of these have a default name that will always be unique. If you have multiple actions of the same type that manage a relationship with an argument of the same name, you will get a compile-time error.

  • :types - A keyword list of field names to their graphql type identifiers.

    Since managed relationships can ultimately call multiple actions, there is the possibility of field type conflicts. Use this to determine the type of fields and remove the conflict warnings.

Link to this section Summary

Link to this section Functions

Link to this function

decode_primary_key(resource, value)

View Source
Link to this function

encode_primary_key(record)

View Source
Link to this function

enum_definitions(resource, schema, only_auto? \\ false)

View Source
Link to this function

managed_relationships(resource)

View Source
Link to this function

no_graphql_types(resource, schema)

View Source
Link to this function

primary_key_delimiter(resource)

View Source
Link to this function

type_definition(resource, api, schema)

View Source