Working with Object Types

1. Creating Object Types

ElementSyntax
Declarationtype TypeName { ... }
FieldfieldName: ReturnType
With argsposts(first: Int): [Post!]!
Implementstype User implements Node { id: ID! }

Example: Object type

type Post {
  id: ID!
  title: String!
  body: String!
  author: User!
  comments(first: Int = 10): [Comment!]!
  createdAt: DateTime!
}

2. Defining Fields with Types

Field TypeExampleNotes
Scalartitle: StringLeaf value
Enumstatus: OrderStatusFixed values
Objectauthor: UserNested selection
Listtags: [String!]!Ordered collection
Interface/Unionnode: NodePolymorphic

3. Making Fields Required

ModifierMeaning
StringNullable string
String!Non-null; resolver must not return null
[String]Nullable list of nullable strings
[String!]Nullable list, items required
[String!]!Required list of required strings

4. Making Fields Nullable

PatternWhen
Default nullableResolver may fail without aborting parent
Use nullField not applicable / unknown
Empty list vs nullPrefer [] for "no items"
Note: Default to nullable; mark ! only when truly guaranteed. Non-null errors propagate up to first nullable ancestor.

5. Defining Field Arguments

Example: Arguments with defaults

type Query {
  posts(
    first: Int = 20
    after: String
    orderBy: PostOrder = { field: CREATED_AT, direction: DESC }
    filter: PostFilter
  ): PostConnection!
}
FormExample
Required argid: ID!
Optional argfirst: Int
Default valuefirst: Int = 20
Input object argfilter: PostFilter

6. Setting Default Argument Values

RuleDetail
Literal onlyDefault must be a constant literal
Applied whenArgument omitted OR explicitly null on nullable arg
Required + defaultfirst: Int! = 10 — caller may still omit

7. Using Type References

FormNote
Forward referenceAllowed; SDL is order-independent
CircularSupported (User.posts ↔ Post.author)
Self-referenceAllowed (Tree.children: [Tree!]!)

8. Implementing Interfaces

Example: Implements Node

interface Node { id: ID! }

type User implements Node {
  id: ID!
  email: String!
}
RuleDetail
Field setMust include all interface fields with compatible types
ArgsMust accept all interface args
Multipleimplements Node & Timestamped

9. Creating Abstract Types

KindWhen to use
InterfaceShared fields across implementing types
UnionHeterogeneous result, no shared fields
PatternExample
Co-locateUser.graphql contains User, UserConnection, UserEdge, UserInput
Group by featurebilling/, auth/, catalog/ folders
Sharedcommon/scalars.graphql, common/interfaces.graphql