GraphApi.View (GraphApi v1.0.0-rc.1)

Copy Markdown View Source

Macro for defining view structs that project a subset of fields from a schema module.

Views provide IDE autocomplete, automatic $select injection, and compile-time field validation against the parent schema.

Usage

defmodule MyApp.UserSummary do
  use GraphApi.View,
    schema: GraphApi.Schema.User,
    fields: [:id, :display_name, :mail, :job_title]
end

This generates:

  • defstruct with only the listed fields
  • from_map/1 — casts via parent schema's from_map/1, then takes only view fields
  • to_map/1 — converts back to camelCase via parent schema
  • __select__/0 — returns camelCase field names for auto $select injection
  • __schema__/0 — returns the parent schema module