### Install Elixir Project Dependencies Source: https://github.com/divvypayhq/absinthe_federation/blob/main/CONTRIBUTING.md To begin development, run this command in the project's root directory. It uses Mix to fetch and install all necessary Elixir dependencies, preparing the environment for further development. ```Shell $ mix deps.get ``` -------------------------------- ### Elixir: Use Dataloader for _resolve_reference Queries in Absinthe Federation Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This example shows how to leverage Dataloader within Absinthe Federation's `_resolve_reference` queries for efficient data fetching. It demonstrates two approaches: using the `dataloader/2` helper and the `on_load/2` helper, both requiring manual setup of batch and item keys due to the absence of a parent object in these specific resolutions. ```Elixir defmodule Example.Schema do use Absinthe.Schema use Absinthe.Federation.Schema import Absinthe.Resolution.Helpers, only: [on_load: 2, dataloader: 2] def context(ctx) do loader = Dataloader.new() |> Dataloader.add_source(Example.Loader, Dataloader.Ecto.new(Example.Repo)) Map.put(ctx, :loader, loader) end def plugins do [Absinthe.Middleware.Dataloader] ++ Absinthe.Plugin.defaults() end object :item do key_fields("item_id") # Using the dataloader/2 resolution helper field :_resolve_reference, :item do resolve dataloader(Example.Loader, fn _parent, args, _res -> %{batch: {{:one, Example.Item}, %{}}, item: [item_id: args.item_id]} end) end end object :verbose_item do key_fields("item_id") # Using the on_load/2 resolution helper field :_resolve_reference, :verbose_item do resolve fn %{item_id: id}, %{context: %{loader: loader}} -> batch_key = {:one, Example.Item, %{}} item_key = [item_id: id] loader |> Dataloader.load(Example.Loader, batch_key, item_key) |> on_load(fn loader -> result = Dataloader.get(loader, Example.Loader, batch_key, item_key) {:ok, result} end) end end end end ``` -------------------------------- ### Install Absinthe.Federation from GitHub Branch Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This snippet demonstrates how to include Absinthe.Federation as a dependency directly from a specific branch on GitHub. This is useful for testing unreleased features or contributing to the project, pointing to the "main" branch. ```Elixir def deps do [ {:absinthe_federation, github: "DivvyPayHQ/absinthe_federation", branch: "main"} ] end ``` -------------------------------- ### Install Absinthe.Federation from Hex Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This snippet shows how to add Absinthe.Federation as a dependency in your Elixir project's mix.exs file, fetching it from Hex. It specifies a version constraint of ~> 0.7 to ensure compatibility with your project. ```Elixir def deps do [ {:absinthe_federation, "~> 0.7"} ] end ``` -------------------------------- ### Elixir: Integrate Absinthe Federation with Existing Schema Prototype Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This example illustrates how to integrate Absinthe Federation into an existing Absinthe schema that uses a prototype. It shows the addition of `use Absinthe.Federation.Schema` to the main schema and `use Absinthe.Federation.Schema.Prototype.FederatedDirectives` to the prototype, enabling federation features while maintaining the prototype structure. ```Elixir defmodule Example.Schema do use Absinthe.Schema + use Absinthe.Federation.Schema, prototype_schema: Example.SchemaPrototype query do ... end end ``` ```Elixir defmodule Example.SchemaPrototype do use Absinthe.Schema.Prototype + use Absinthe.Federation.Schema.Prototype.FederatedDirectives directive :my_directive do on [:schema] end end ``` -------------------------------- ### Absinthe.Federation _resolve_reference Return Types Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This section outlines the acceptable return types for the :_resolve_reference field in Absinthe.Federation. It shows examples of returning a struct, a map with atom keys, a map with string keys, or nil, all wrapped in an {:ok, ...} tuple. ```Elixir {:ok, %Product{id: id, ...}} {:ok, %{__typename: "Product", id: id, ...}} {:ok, %{"__typename" => "Product", "id" => id, ...}} {:ok, nil} ``` -------------------------------- ### Integrate Absinthe.Federation into Absinthe Schema Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This snippet illustrates how to enable Absinthe.Federation support in your Absinthe schema by adding `use Absinthe.Federation.Schema`. This line should be placed within your root schema module to activate federation features and directives. ```Elixir defmodule Example.Schema do use Absinthe.Schema use Absinthe.Federation.Schema query do ... end end ``` -------------------------------- ### Elixir: Define Absinthe Federation Schema with SDL (Experimental) Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This snippet demonstrates how to define an Absinthe Federation schema using GraphQL Schema Definition Language (SDL) within an Elixir application. It shows how to `import_sdl` to extend existing types and define new ones, allowing for a more declarative schema definition. This approach is marked as experimental. ```Elixir defmodule Example.Schema do use Absinthe.Schema + use Absinthe.Federation.Schema import_sdl """ extend type Query { review(id: ID!): Review } extend type Product @key(fields: "upc") { upc: String! @external reviews: [Review] } """ def hydrate(_, _) do ... end end ``` -------------------------------- ### Configure Absinthe Federation @link for Namespacing and Renaming (Elixir) Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This Elixir code snippet demonstrates how to use the `extend schema` block within an Absinthe schema to configure the `@link` directive. It shows how to import and rename a directive (e.g., `@key` to `@primaryKey`) and how to apply namespacing (e.g., `as: "federation"`). This functionality requires Absinthe version 1.7.2 or higher. ```Elixir defmodule Example.Schema do use Absinthe.Schema use Absinthe.Federation.Schema extend schema do directive :link, url: "https://specs.apollo.dev/federation/v2.3", import: [%{"name" => "@key", "as" => "@primaryKey"}], # directive renaming as: "federation" # namespacing end query do ... end end ``` -------------------------------- ### Define an Apollo Federation Entity in Elixir Absinthe Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This Elixir snippet demonstrates how to define an entity (:product) using Absinthe.Federation, including the @key directive and the required :_resolve_reference field. It shows how to link to the Federation v2.3 specification and define standard fields like id, name, and price. ```Elixir defmodule Products.Schema do use Absinthe.Schema use Absinthe.Federation.Schema extend schema do directive(:link, url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", ...] ) end object :product do directive :key, fields: "id" # Any subgraph contributing fields MUST define a _resolve_reference field. field :_resolve_reference, :product do resolve &Products.find_by_id/2 end field :id, non_null(:id) field :name, non_null(:string) field :price, :int end query do ... end end ``` -------------------------------- ### Elixir: Import Apollo Federation v2 Directives in Absinthe Schema Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This snippet illustrates how to enable Apollo Federation v2 directives in an Absinthe schema. It shows the use of the `@link` directive within the `extend schema` block to import a list of standard Federation v2 directives from the official specification URL, allowing the schema to utilize advanced federation features. ```Elixir defmodule Example.Schema do use Absinthe.Schema use Absinthe.Federation.Schema + extend schema do + directive :link, + url: "https://specs.apollo.dev/federation/v2.7", + import: [ "@authenticated", "@extends", "@external", "@inaccessible", "@key", "@override", "@policy", "@provides", "@requires", "@requiresScopes", "@shareable", "@tag", "@composeDirective", "@interfaceObject" + ] + end query do ... end end ``` -------------------------------- ### Validate Absinthe.Federation Schema Configuration Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This command-line snippet validates that Absinthe.Federation is correctly wired up in your schema. It outputs the Apollo Federation Subgraph Specification fields along with your defined fields, helping to confirm proper integration and identify any misconfigurations. ```Bash mix absinthe.federation.schema.sdl --schema Example.Schema ``` -------------------------------- ### Contribute Entity Fields in Absinthe.Federation Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This Elixir snippet illustrates how a subgraph can contribute additional fields to an existing entity, such as in_stock to a Product. It demonstrates how to implement the :_resolve_reference field to merge new data onto an incoming entity reference, ensuring each subgraph manages its specific fields. ```Elixir defmodule Inventory.Schema do use Absinthe.Schema use Absinthe.Federation.Schema extend schema do directive(:link, url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", ...] ) end object :product do directive :key, fields: "id" # In this case, only the `Inventory.Schema` should resolve the `inStock` field. field :_resolve_reference, :product do resolve(fn %{__typename: "Product", id: id} = entity, _info -> {:ok, Map.merge(entity, %{in_stock: true})} end) end field :id, non_null(:string) field :in_stock, non_null(:boolean) end query do ... end end ``` -------------------------------- ### Elixir: Resolve Structs to Schema Types in Absinthe Federation _entities Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This snippet explains how to resolve Elixir structs to specific schema types within Absinthe Federation's `_entities` queries. It demonstrates implementing the `Absinthe.Federation.Schema.EntityUnion.Resolver` protocol for a struct, allowing the schema to correctly identify and resolve the type of the returned entity. ```Elixir defmodule MySchema do @type t :: %__MODULE___{ id: String.t() } defstruct id: "" defimpl Absinthe.Federation.Schema.EntityUnion.Resolver do def resolve_type(_, _), do: :my_schema_object_name end end ``` -------------------------------- ### Elixir: Reference Federated Entity Without Contributing Fields Source: https://github.com/divvypayhq/absinthe_federation/blob/main/README.md This snippet demonstrates how to define a stubbed entity in an Absinthe Federation schema that is marked as unresolvable by the current subgraph. It shows how to reference an entity (e.g., Product) by its key fields (id) without requiring the subgraph to resolve its full details, allowing other subgraphs to own the entity's resolution. ```Elixir defmodule Reviews.Schema do use Absinthe.Schema use Absinthe.Federation.Schema extend schema do directive(:link, url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", ...] ) end # Stubbed entity, marked as unresolvable in this subgraph. object :product do directive :key, fields: "id", resolvable: false field :id, non_null(:string) end object :review do field :id, non_null(:id) field :score, non_null(:int) field :description, non_null(:string) # This subgraph only needs to resolve the key fields used to reference the entity. field :product, non_null(:product) do resolve(fn %{product_id: id} = _parent, _args, _info -> {:ok, %{id: id}} end) end end query do field :latest_reviews, non_null(list(:review)) do resolve(&ReviewsResolver.find_many/2) end end end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.