SyntaxStudy
Sign Up
GraphQL Input Types and Custom Scalars
GraphQL Beginner 1 min read

Input Types and Custom Scalars

Input types are special object types used exclusively as mutation and query arguments. Unlike regular object types, input types cannot have resolvers or reference output types—they are plain data containers, making them safe to use as argument shapes. Custom scalars extend the five built-in scalars. Common examples are Date, DateTime, URL, and EmailAddress. You define a custom scalar in SDL and then provide a serialize/parseValue/parseLiteral implementation in your resolvers to control how values are coerced at runtime.
Example
# ----- Input Types -----
input CreateProductInput {
  name: String!
  price: Float!
  description: String
  categoryId: ID!
  tags: [String!]
}

input UpdateProductInput {
  name: String
  price: Float
  description: String
  tags: [String!]
}

type Mutation {
  createProduct(input: CreateProductInput!): Product!
  updateProduct(id: ID!, input: UpdateProductInput!): Product!
  deleteProduct(id: ID!): Boolean!
}

# ----- Custom Scalars -----
scalar Date
scalar DateTime
scalar EmailAddress
scalar URL

type User {
  id: ID!
  email: EmailAddress!
  avatar: URL
  createdAt: DateTime!
  birthDate: Date
}

# Resolver implementation (JavaScript):
# import { GraphQLScalarType, Kind } from 'graphql';
#
# const DateScalar = new GraphQLScalarType({
#   name: 'Date',
#   serialize(value) { return value.toISOString().split('T')[0]; },
#   parseValue(value) { return new Date(value); },
#   parseLiteral(ast) {
#     if (ast.kind === Kind.STRING) return new Date(ast.value);
#     return null;
#   },
# });