Skip to content
intermediate Phase 61 · GraphQL Basics

GraphQL Schema

Understanding Magento 2 GraphQL schema: type definitions, interfaces, input types, and custom types

45m
0 problems
Topic Progress 0%

Type Definitions

Object Types

type Product {
    id: ID!
    name: String!
    sku: String!
    url_key: String
    price_range: PriceRange!
    image: ProductImage
    description: ComplexTextValue
    stock_status: StockStatus
}

type PriceRange {
    minimum_price: ProductPrice!
    maximum_price: ProductPrice
}

type ProductPrice {
    regular_price: Money!
    final_price: Money!
    discount: ProductDiscount
}

type Money {
    value: Float!
    currency: String!
}

Enum Types

enum StockStatus {
    IN_STOCK
    OUT_OF_STOCK
    BACKORDERED
}

enum SortOrder {
    ASC
    DESC
}

enum ProductVisibility {
    NOT_VISIBLE
    IN_CATALOG
    IN_SEARCH
    BOTH
}

Interface Types

interface ProductInterface {
    id: ID!
    name: String!
    sku: String!
    url_key: String
}

type SimpleProduct implements ProductInterface {
    id: ID!
    name: String!
    sku: String!
    url_key: String
    price: Float
}

type ConfigurableProduct implements ProductInterface {
    id: ID!
    name: String!
    sku: String!
    url_key: String
    configurable_options: [ConfigurableOption]
}

Input Types

Basic Input Types

input ProductFilterInput {
    sku: FilterEqualTypeInput
    name: FilterMatchTypeInput
    price: FilterRangeTypeInput
    category_id: FilterEqualTypeInput
}

input FilterEqualTypeInput {
    eq: String
    in: [String]
    nin: [String]
}

input FilterMatchTypeInput {
    match: String
}

input FilterRangeTypeInput {
    from: String
    to: String
}

Mutation Input Types

input CreateProductInput {
    name: String!
    sku: String!
    price: Float!
    description: String
    status: Int
    visibility: Int
}

input UpdateProductInput {
    id: ID!
    name: String
    price: Float
    description: String
}

input CustomerInput {
    email: String!
    firstname: String!
    lastname: String!
    password: String!
}

Input with Validation

input AddressInput {
    firstname: String!
    lastname: String!
    street: [String]!
    city: String!
    region: String!
    postcode: String!
    country_code: String!
    telephone: String!
}

Input Type Usage

mutation {
    createProduct(
        input: {
            name: "New Product"
            sku: "NEW-SKU"
            price: 29.99
        }
    ) {
        id
        name
    }
}

Custom Types

Custom Scalar Types

scalar Date
scalar DateTime
scalar JSON
scalar URL

Custom Object Types

type CustomEntity {
    id: ID!
    name: String!
    email: String!
    status: CustomStatus!
    metadata: JSON
    created_at: DateTime
    updated_at: DateTime
}

type CustomStatus {
    code: String!
    label: String!
}

Union Types

union SearchResult = Product | Category | CmsPage

type Product {
    id: ID!
    name: String!
    __typename: String
}

type Category {
    id: ID!
    name: String!
    __typename: String
}

Extended Types

# Extend existing types
type Product {
    custom_field: String
    custom_attribute: Int
}

# Extend Query type
type Query {
    customProducts: [CustomProduct]
}

# Extend Mutation type
type Mutation {
    createCustomProduct(input: CreateCustomProductInput!): CustomProduct
}

Schema Extension

Define Schema Extension

# schema.graphqls in your module
type Query {
    customItems(
        filter: CustomFilterInput
        sort: CustomSortInput
        pageSize: Int
        currentPage: Int
    ): CustomItemSearchResult
}

type Mutation {
    createCustomItem(
        input: CreateCustomItemInput!
    ): CustomItem
    
    updateCustomItem(
        input: UpdateCustomItemInput!
    ): CustomItem
    
    deleteCustomItem(
        input: DeleteCustomItemInput!
    ): Boolean
}

Type Definitions

type CustomItem {
    id: ID!
    name: String!
    email: String!
    status: String!
    created_at: DateTime
}

type CustomItemSearchResult {
    items: [CustomItem]!
    total_count: Int
    page_info: PageInfo
}

type PageInfo {
    page_size: Int
    current_page: Int
}

input CreateCustomItemInput {
    name: String!
    email: String!
}

input UpdateCustomItemInput {
    id: ID!
    name: String
    email: String
}

input DeleteCustomItemInput {
    id: ID!
}

input CustomFilterInput {
    name: FilterMatchTypeInput
    status: FilterEqualTypeInput
}

input CustomSortInput {
    name: SortOrder
    created_at: SortOrder
}

Schema Location

app/code/Vendor/Module/etc/schema.graphqls

Merge Process

Magento merges all schema.graphqls files automatically.

Quiz

1. What is the difference between type and input?

Question 1 options

2. What are enum types used for?

Question 2 options

3. Where is the GraphQL schema defined?

Question 3 options

Flashcards

Question

What is a GraphQL type?

Answer

Defines the structure of output data

Question

What is an input type?

Answer

Defines the structure of mutation arguments

Question

What is an enum type?

Answer

A fixed set of allowed values

Question

Where is the schema file?

Answer

etc/schema.graphqls

Question

How do you extend existing types?

Answer

Use extend keyword in schema.graphqls

Revision Notes

Key Takeaways

  • 1. Types define output structure for queries
  • 2. Input types define argument structure for mutations
  • 3. Enum types provide fixed value sets
  • 4. Interfaces enable polymorphic types
  • 5. Schema extensions merge automatically

Interview Tips

  • Know the difference between types and input types
  • Understand interface and union types
  • Be ready to define custom types
  • Discuss schema extension patterns

Cheat Sheet

Type: Output structure
type Product { id: ID!, name: String! }

Input: Mutation arguments
input CreateProduct { name: String!, sku: String! }

Enum: Fixed values
enum Status { ACTIVE, INACTIVE }

Interface: Polymorphic
type Product implements ProductInterface { ... }

Schema file: etc/schema.graphqls