GraphQL
Beginner
1 min read
SDL: Defining Types and Fields
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!]!
}
Related Resources
GraphQL Reference
Complete tag & property list
GraphQL How-To Guides
Step-by-step practical guides
GraphQL Exercises
Practice what you've learned
More in GraphQL