Skip to content

fix(gqlorm): use Cedar's pluralization function in graphqlGenerator #1617

@lisa-assistant

Description

@lisa-assistant

Problem

In packages/gqlorm/src/generator/graphqlGenerator.ts, the #getGraphQLFieldName method generates the GraphQL field name for findMany operations using a naive ${model}s string append:

case 'findMany':
  // TODO: Use Cedar's pluralization function
  // Simple pluralization
  return `${model}s`

This will produce incorrect results for irregular plurals (e.g. PersonPersons instead of People, DatumDatums instead of Data, CategoryCategorys instead of Categories).

Cedar already has a proper pluralization function in packages/utils/src/cedarPluralize.ts (which wraps the pluralize npm package and adds support for PascalCased multi-word identifiers).

Fix

  1. Add @cedarjs/utils to packages/gqlorm/package.json dependencies
  2. Import pluralize from @cedarjs/utils (or @cedarjs/utils/cedarPluralize) in graphqlGenerator.ts
  3. Replace ${model}s with pluralize(model)
import { pluralize } from '@cedarjs/utils'

// ...

case 'findMany':
  return pluralize(model)

Note: cedarPluralize.ts#pluralize already handles PascalCase correctly (e.g. OrderItemOrderItems, FooBarBazFooBarBazs... actually it pluralizes only the last word), so the behavior should be correct for typical Prisma model names.

Files to change

  • packages/gqlorm/src/generator/graphqlGenerator.ts — replace naive pluralization
  • packages/gqlorm/package.json — add @cedarjs/utils dependency
  • packages/gqlorm/src/generator/__tests__/ — add test cases for irregular plurals

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions