GraphQL
Beginner
1 min read
Input Types and Custom Scalars
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;
# },
# });
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