Get ready for your next interview with our comprehensive question library
# Query
query GetUser {
user(id: "123") { name }
}
# Mutation
mutation CreateUser {
createUser(input: { name: "John" }) {
id
name
}
}
# Subscription
subscription UserUpdates {
userUpdated(id: "123") {
name
status
}
}
A GraphQL schema is a contract that defines the structure of the API, including all available operations, types, and relationships. It serves as the single source of truth for what data can be fetched and how.
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
}
type Query {
user(id: ID!): User
users: [User!]!
}
Scalars are primitive data types that represent leaf values in GraphQL. Built-in scalars include:
String
: UTF-8 character sequenceInt
: 32-bit signed integerFloat
: Double-precision floating-pointBoolean
: true or falseID
: Unique identifierYou can also define custom scalars:
scalar Date
scalar Email
scalar URL
type User {
id: ID!
email: Email!
birthDate: Date
}
In GraphQL, fields are nullable by default. The exclamation mark (!
) makes a field non-nullable.
type User {
id: ID! # Required field
name: String! # Required field
email: String # Optional field (can be null)
age: Int # Optional field
posts: [Post!]! # Required array of required Posts
tags: [String] # Optional array of optional strings
}
Object types represent entities in your domain with named fields. They're the most common type in GraphQL schemas.
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
createdAt: String!
}
type Post {
id: ID!
title: String!
content: String!
author: User!
published: Boolean!
}
Enums define a set of possible values for a field, providing type safety and validation.
enum UserRole {
ADMIN
MODERATOR
USER
GUEST
}
enum PostStatus {
DRAFT
PUBLISHED
ARCHIVED
}
type User {
id: ID!
name: String!
role: UserRole!
}
Input types are used for arguments in mutations and queries. They can only contain scalars, enums, lists, and other input types.
input CreateUserInput {
name: String!
email: String!
age: Int
}
input UpdateUserInput {
name: String
email: String
age: Int
}
type Mutation {
createUser(input: CreateUserInput!): User!
updateUser(id: ID!, input: UpdateUserInput!): User!
}
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime