Types Overview

View Source

GreenFairy provides a clean DSL for defining all GraphQL type kinds. Each type kind has its own module and follows the "one module = one type" principle.

Type Kinds

KindModuleGuide
Object TypesGreenFairy.TypeObject Types
InterfacesGreenFairy.InterfaceInterfaces
Input TypesGreenFairy.InputInput Types
EnumsGreenFairy.EnumEnums
UnionsGreenFairy.UnionUnions
ScalarsGreenFairy.ScalarScalars

Directory Structure

Organize types by kind:

lib/my_app/graphql/
 schema.ex           # Main schema
 types/              # Object types
 interfaces/         # Interfaces
 inputs/             # Input types
 enums/              # Enums
 unions/             # Unions
 scalars/            # Custom scalars
 queries/            # Query operations
 mutations/          # Mutation operations
 resolvers/          # Resolver logic

Type References

Use module references for non-builtin types to enable auto-discovery:

alias MyApp.GraphQL.Types
alias MyApp.GraphQL.Enums

field :posts, list_of(Types.Post)
field :status, Enums.UserStatus

Use atoms only for built-in scalars: :id, :string, :integer, :float, :boolean, :datetime.

Common Module Functions

All GreenFairy type modules export:

FunctionDescription
__green_fairy_kind__/0Returns the type kind (:object, :interface, etc.)
__green_fairy_identifier__/0Returns the snake_case identifier
__green_fairy_definition__/0Returns the full definition map

Naming Conventions

GraphQL NameElixir Identifier
User:user
CreateUserInput:create_user_input
UserRole:user_role

The identifier is automatically derived from the GraphQL name using snake_case.

Detailed Guides

  • Object Types - Fields, resolvers, batch loading, associations
  • Interfaces - Shared fields, automatic type resolution
  • Input Types - Mutation arguments, validation
  • Enums - Value definitions, mappings
  • Unions - Polymorphic returns
  • Scalars - Custom parsing/serialization