LibraryBest Practices for Naming Conventions

Best Practices for Naming Conventions

Learn about Best Practices for Naming Conventions as part of GraphQL API Development and Federation

GraphQL Naming Conventions: Clarity and Consistency

In GraphQL, consistent and descriptive naming conventions are crucial for building maintainable, understandable, and scalable APIs. They act as a shared language between frontend and backend developers, reducing ambiguity and improving developer experience. This module explores best practices for naming fields, types, arguments, and enums.

Core Principles of GraphQL Naming

Adhering to a few core principles will guide your naming decisions:

  • Clarity: Names should clearly indicate the data or operation they represent.
  • Consistency: Apply the same naming patterns across your entire schema.
  • Conciseness: While clarity is key, avoid overly verbose names.
  • Predictability: Developers should be able to anticipate naming patterns.

Naming Conventions for Types and Fields

Use PascalCase for Type names and camelCase for Field names.

GraphQL schemas typically use PascalCase (e.g., UserProfile) for object types, interfaces, and enums. Fields within these types, as well as arguments, are conventionally named using camelCase (e.g., firstName, getUserById).

Object types, interfaces, and enums should follow the PascalCase convention, where the first letter of each word is capitalized. For example, User, Product, Order, Query, Mutation, Subscription, DateTime. Fields within these types, as well as arguments passed to fields, should use camelCase, where the first word is lowercase and subsequent words start with an uppercase letter. For instance, userName, productId, orderDate, getUsers, createProduct. This convention aligns with common practices in many programming languages and promotes a consistent look and feel for your schema.

Naming Conventions for Arguments

Arguments are used to filter, sort, or specify parameters for fields. They should also follow the camelCase convention. For example, when fetching a user, you might use

code
userId
or
code
username
as arguments:
code
user(userId: "123")
. When fetching a list of products, you might use
code
first
,
code
after
,
code
filter
, or
code
sortBy
as arguments:
code
products(first: 10, sortBy: PRICE_ASC)
.

Naming Conventions for Enums

Enum types themselves should be named in PascalCase, similar to other object types (e.g.,

code
OrderStatus
,
code
SortDirection
). The individual enum values, however, are conventionally named using SCREAMING_SNAKE_CASE (all uppercase with underscores). This clearly distinguishes them as distinct, fixed values. For example:

graphql
enum OrderStatus {
PENDING
PROCESSING
SHIPPED
DELIVERED
CANCELLED
}

Naming Conventions for Input Objects

Input objects, used for passing complex arguments to mutations or queries, should also follow the PascalCase convention for the type name (e.g.,

code
CreateUserInput
,
code
UpdateProductInput
). The fields within these input objects should use camelCase, consistent with other fields.

Avoiding Common Pitfalls

Avoid using abbreviations or jargon that might not be universally understood. If a name needs to be longer to be clear, it's usually worth it.

Common pitfalls include:

  • Inconsistency: Mixing PascalCase and camelCase for fields or types.
  • Ambiguity: Using names that don't clearly describe the data (e.g.,
    code
    data
    ,
    code
    info
    ,
    code
    item
    ).
  • Overly long names: While clarity is important, excessively long names can be cumbersome.
  • Using reserved keywords: While GraphQL is flexible, avoid names that might conflict with future language features or common programming keywords.

Naming for Federation

When working with GraphQL Federation, maintaining consistent naming across subgraphs is paramount. The same principles of clarity, consistency, and predictability apply. Ensure that shared types and fields have identical names and definitions across all federated services to avoid conflicts and simplify client-side queries. For example, if a

code
Product
type is shared, its fields like
code
id
,
code
name
, and
code
price
must be named and typed identically in every subgraph that defines or extends it.

Practical Application: A Simple Example

Consider a schema for a blog. We have a Post type with fields like postId, title, content, and author. The author field might be a User type, which also has fields like userId, firstName, and lastName. A query to fetch a post might be post(postId: "abc-123"). A mutation to create a post could be createPost(input: { title: "New Blog Post", content: "...", authorId: "user-456" }). Enum for post status could be PostStatus { DRAFT, PUBLISHED, ARCHIVED }.

📚

Text-based content

Library pages focus on text content

Summary and Best Practices Recap

GraphQL ElementConventionExample
Type Names (Object, Interface, Enum)PascalCaseUserProfile, OrderStatus
Field NamescamelCasefirstName, orderDate
Argument NamescamelCaseuserId, sortBy
Enum ValuesSCREAMING_SNAKE_CASEPENDING, PRICE_ASC
Input Object NamesPascalCaseCreateUserInput
What is the standard naming convention for GraphQL Type names?

PascalCase (e.g., UserProfile)

What is the standard naming convention for GraphQL Field names?

camelCase (e.g., firstName)

How should Enum values be named in GraphQL?

SCREAMING_SNAKE_CASE (e.g., PENDING)

Learning Resources

GraphQL Specification: Naming Conventions(documentation)

The official GraphQL specification detailing naming rules and best practices for identifiers.

Apollo GraphQL Docs: Schema Design Best Practices(documentation)

Apollo's official documentation provides practical advice on schema design, including naming conventions.

Building a GraphQL API: Naming Conventions(blog)

A blog post from Hasura explaining common naming conventions and their importance in GraphQL.

GraphQL Naming Conventions Explained(tutorial)

A tutorial that breaks down the essential naming conventions for building a GraphQL schema.

Best Practices for GraphQL Schema Design(blog)

An article discussing broader schema design principles, with a section dedicated to naming conventions.

GraphQL Federation: Naming Conventions(documentation)

Specific guidance on naming conventions when working with GraphQL Federation to ensure consistency across services.

GraphQL Schema Design Patterns(blog)

A comprehensive guide to various schema design patterns, including a focus on naming for clarity and maintainability.

Understanding GraphQL Naming Conventions(video)

A video explanation of GraphQL naming conventions, often featuring visual examples and practical tips.

GraphQL Best Practices: Naming(blog)

Prisma's perspective on GraphQL best practices, highlighting the significance of naming conventions for developer productivity.

GraphQL Naming Conventions - A Deep Dive(blog)

An in-depth article exploring the nuances of GraphQL naming conventions and their impact on API design.