I'm submitting a ...
Hello,
I'm building an application which uses Postgraphile. The application allows users to create database tables and Postgraphile will automatically expose their corresponding queries and mutations. I would like to generate the queries and mutation payloads automatically based on the table name chosen by the user but this becomes quite tricky due to the syntax applied by Postgraphile. Example:
A table named list_of_values will give the following queries and mutations
allMyListOfValuesmyListOfValueByIdmyListOfValuecreateMyListOfValueupdateMyListOfValueupdateMyListOfValueByIddeleteMyListOfValuedeleteMyListOfValueByIdIt becomes even more complex with other table names when plural form orthographic changes. A table named babies gives the following:
allBabiesbabyByIdbabycreateBabyupdateBabyupdateBabyByIddeleteBabydeleteBabyByIdI would like to know how the queries and mutation names are generated so that I can replicate the logic on my side to dynamically create the payload? If you have an PGSQL code snippet which allows to generate these names based on the table_name, it would be even better...
Many thanks
Alexis
Our names are generated through the inflection system, you can read about it here:
https://www.graphile.org/postgraphile/inflection/
You can totally replace the inflection system if you prefer, or change how names are made plural/singular, or whatever you need to do. A good place to start is here:
https://www.graphile.org/postgraphile/make-add-inflectors-plugin/
I've written a plugin that massively simplifies the names generated, you can view that here:
https://github.com/graphile-contrib/pg-simplify-inflector
You may also want to turn off the Node interface if you want to reduce the number of mutations/etc. If you want to do that, there's instructions here:
https://www.graphile.org/postgraphile/node-id/#disabling-the-relay-global-object-identifier
I hope this helps 👍
[semi-automated message] Thanks for your question; hopefully we're well on the way to helping you solve your issue. This doesn't currently seem to be a bug in the library so I'm going to close the issue, but please feel free to keep requesting help below and if it does turn out to be a bug we can definitely re-open it 👍
Thanks @benjie it helps a little bit but I'm still not sure what could be the best solution for me to get GraphQL queries / mutations names:
Overwrite the inflection with a custom plugin (seems dangerous and complex)
It's neither dangerous nor complex. Inflectors are simple functions - they take as input relevant data (typically introspection objects representing the table / column / constraint / etc) and return a string. We have an official API for overriding them (makeAddInflectorsPlugin) so it's definitely a supported and stable interface, we've had the pluggable inflector system since 4.0.0-beta.6 (though the makeAddInflectorsPlugin helper is more recent than that).
I think overwriting the inflectors is your best bet because then you state exactly what you want the names to be and control everything.
Recompute them myself based on the table / column names to try to reproduce the logic in makeNewBuild.js? (seems complex to me)
You can do that should you wish; you don't need to actually reproduce the logic, you can use a simple plugin to get access to both the final inflectors and the introspection results and then just feed one into the other as necessary. I can help you write that plugin should you choose to go that way.
Maybe access the GraphQL schema somewhere to find the actual queries / mutation names? Is this actually possible?
You can get a reference to the final schema, but I'm not sure how it'll help since you won't know which table each thing relates to without already knowing how the names map.
Thanks @benjie, I’ll try on my side first but I might poke you again if I need :)
Here is the project I’m working on by the way: https://github.com/alexisrolland/listof
Thanks again for proposing your help.
Hello @benjie,
Here is what I came up with. The solution is still incomplete but I have a question below:
First I installed the inflection npm package: npm install inflection
Considering the following snippet, here is what I did:
var tableName = 'my_new_list_2';
var inflection = require('inflection');
var graphQlName = inflection.transform(tableName, ['pluralize', 'camelize']);
graphQlName = 'all' + graphQlName
This returns graphQlName = allMyNewList2s, but in Postgraphile, the type name would be allMyNewList2S with a capital S at the end. Do you know which inflection rule am I missing to have the capital S?
Thank you
Alexis
I think it’s just a difference in the capitalisation logic, we use the pluralize module internally.
(We don’t use the inflection module. See: https://www.graphile.org/postgraphile/make-add-inflectors-plugin/#where-are-the-default-inflectors-defined)
Sorry, I mean we use lodash for capitalisation.