Objects (type)

Meta: lead with "a type declares both a type and its prototype constructor." That's the unusual thing. Cross-link to Mutation and copy-on-write — don't try to explain CoW here.

Declaration

type Person {
  pub name: String!
  pub age: Int! = 0

  pub greet: String! {
    "hi, I'm " + name
  }
}

Public vs. private members

Implicit constructor

Person("Alice", age: 30)
Person(name: "Alice")

Zero-arg auto-construction

Explicit constructor: new

type Greeter {
  pub greeting: String!

  new(name: String!) {
    self.greeting = "hello, " + name
    self
  }
}

self

Computed fields

pub fullName: String! { firstName + " " + lastName }

Implements

type Person implements Named & Identifiable { ... }

Forward references

Meta: user-defined types and imported GraphQL input types share the same Type(args) construction syntax. Schema input types are constructed exactly like local types: CreateUserInput(name: "Alice", email: "...") passed as Mutation.createUser(input: ...). See GraphQL interop.