crystal icon indicating copy to clipboard operation
crystal copied to clipboard

@ref smart tag applies inflector to query name

Open danzho opened this issue 10 months ago • 0 comments

Context

(v5)

Consider the case where I have tables:

  • app.users{id}
  • app.invoices{id,user_id}
  • app.transactions{id,invoice_id}

I want a graphql query that gets all transactions for a user:

userById(id: "") {
    transactionsByUserId { ... }
}

I could denormalize user_id into the transactions table. But if I don't want to do that, I can use the ref smart tag to tell postgraphile how to join transactions through invoices.

export const SmartTagsPlugin = makeJSONPgSmartTagsPlugin({
  version: 1,
  config: {
    class: {
      "app.users": {
        tags: {
          ref: "transactions via:(id)->invoices(user_id);(id)->transactions(invoice_id) plural",
        },
      },
    },
  },
});

That works, and it generates:

userById(id: "") {
    transactions { ... }
}

(Which is probably good for everyone using the simplify-inflections plugin)

But in my case, I want the query to be transactionsByUserId since

  1. I'm using vanilla v5 and that's what postgraphile would generate if I added user_id column to the transactions table.
  2. That's already the naming convention of the rest of my API.

Problem

If I try to use another name for the ref tag: transactionByUserId via:(id)->invoices(user_id);(id)->transactions(invoice_id) plural, then it generates this:

userById(id: "") {
    transactionsByUserIds { ... } # <-- notice the trailing 's'
}

This is my concrete use case, but of course this limitation applies to anyone who has any reason to name the query with a suffix that shouldn't be pluralized.

This might be a good place to ask if we want inflectors applied at all to the ref.

danzho avatar Mar 11 '25 22:03 danzho