The State of the Soul
Altering an entity's schema based on its internal condition.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: State
ARCANUM SCHOOL: Evocation // Transformation
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
# The common interface for all states
interface QuestState {
id: ID!
title: String!
}
# Concrete State 1
type PendingQuest implements QuestState {
id: ID!
title: String!
requiredLevel: Int!
}
# Concrete State 2
type ActiveQuest implements QuestState {
id: ID!
title: String!
currentObjective: String!
timeRemaining: Int!
}
# Concrete State 3
type CompletedQuest implements QuestState {
id: ID!
title: String!
rewardsClaimed: Boolean!
}
union Quest = PendingQuest | ActiveQuest | CompletedQuest
type Query {
# The entity returned has different fields based on its internal state
getQuest(id: ID!): Quest!
}
The State pattern in GraphQL is powerfully represented by returning Unions or Interfaces where the concrete type changes depending on the entity’s lifecycle. A quest that is Pending exposes different fields than one that is Active, naturally forcing the client to handle the entity’s current state.
Runic Tags:
#graphql
#behavioral
#state
#unions
#interfaces