Schema & Type System — GraphQL

Schema & Type System Schema adalah jantung GraphQL. Setiap field punya type yang jelas — seperti TypeScript untuk API. Schema menjadi kontrak antara client dan

Schema & Type System

Schema adalah jantung GraphQL. Setiap field punya type yang jelas — seperti TypeScript untuk API. Schema menjadi kontrak antara client dan server.

Scalar Types

# Built-in scalars
String   # "hello"
Int      # 42
Float    # 3.14
Boolean  # true/false
ID       # unique identifier (string)

Object Types

type User {
  id: ID!           # ! = non-nullable
  name: String!
  email: String!
  age: Int          # nullable (bisa null)
  posts: [Post!]!   # array of non-null Post, array itself non-null
}

type Post {
  id: ID!
  title: String!
  content: String
  author: User!
  comments: [Comment!]!
  createdAt: String!
}

Input Types

# Untuk mutation arguments
input CreateUserInput {
  name: String!
  email: String!
  age: Int
}

input UpdateUserInput {
  name: String
  email: String
  age: Int
}

Enum

enum Role {
  ADMIN
  EDITOR
  VIEWER
}

type User {
  id: ID!
  role: Role!
}

Query & Mutation Types

type Query {
  user(id: ID!): User
  users(limit: Int, offset: Int): [User!]!
  post(slug: String!): Post
}

type Mutation {
  createUser(input: CreateUserInput!): User!
  updateUser(id: ID!, input: UpdateUserInput!): User!
  deleteUser(id: ID!): Boolean!
}

Schema Definition

schema {
  query: Query
  mutation: Mutation
  subscription: Subscription  # optional
}

Yang akan kamu pelajari