SyntaxStudy
Sign Up
GraphQL SDL: Defining Types and Fields
GraphQL Beginner 1 min read

SDL: Defining Types and Fields

The GraphQL Schema Definition Language (SDL) is a human-readable syntax for describing a GraphQL API's type system. Every API starts with scalar types (Int, Float, String, Boolean, ID) and builds up to object types, enums, unions, and interfaces. Fields on a type can be marked non-null with an exclamation mark (!). A non-null field guarantees the server will never return null for that field, enabling safer client-side code. Lists use square brackets: [String] is a nullable list of nullable strings, while [String!]! is a non-null list of non-null strings.
Example
# SDL type definitions

# Scalar types (built-in)
# Int | Float | String | Boolean | ID

# Object type
type Product {
  id: ID!               # non-null ID
  name: String!         # non-null String
  price: Float!
  description: String   # nullable String
  tags: [String!]!      # non-null list of non-null strings
  category: Category!
  createdAt: String!
}

type Category {
  id: ID!
  name: String!
  products: [Product!]!
}

# Enum type
enum OrderStatus {
  PENDING
  PROCESSING
  SHIPPED
  DELIVERED
  CANCELLED
}

# Interface
interface Node {
  id: ID!
}

type User implements Node {
  id: ID!
  email: String!
  role: Role!
}

# Enum for roles
enum Role {
  ADMIN
  EDITOR
  VIEWER
}

# Union type — returned value can be one of several types
union SearchResult = Product | Category | User

# The root Query type
type Query {
  product(id: ID!): Product
  products(category: ID): [Product!]!
  search(term: String!): [SearchResult!]!
}