Skip to content

Commit

Permalink
Hack the V5 inflectors for naming consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Nov 24, 2024
1 parent 161f25d commit 82d769c
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"format:check": "yarn format:all --list-different",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"prepack": "tsc -p tsconfig.build.json",
"postinstall": "patch-package",
"test": "tsc -p tsconfig.build.json && scripts/test"
},
"repository": {
Expand All @@ -32,6 +33,7 @@
"eslint-plugin-prettier": "4.2.1",
"eslint_d": "^14.2.2",
"jest": "29.3.1",
"patch-package": "^8.0.0",
"pg": "8.8.0",
"postgraphile": "^5.0.0-beta.33",
"prettier": "2.8.0",
Expand Down
24 changes: 24 additions & 0 deletions patches/graphile-build+5.0.0-beta.28.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
diff --git a/node_modules/graphile-build/dist/newWithHooks/index.js b/node_modules/graphile-build/dist/newWithHooks/index.js
index dc0d978..46e0a9c 100644
--- a/node_modules/graphile-build/dist/newWithHooks/index.js
+++ b/node_modules/graphile-build/dist/newWithHooks/index.js
@@ -5,6 +5,7 @@ const grafast_1 = require("grafast");
const graphql_1 = require("grafast/graphql");
const util_1 = require("util");
const utils_js_1 = require("../utils.js");
+const { assertName } = require("graphql");
const isString = (str) => typeof str === "string";
const knownTypes = [
graphql_1.GraphQLSchema,
@@ -81,6 +82,11 @@ function makeNewWithHooks({ builder }) {
const processedFields = [];
const fieldWithHooks = (fieldScope, fieldSpec) => {
const { fieldName } = fieldScope;
+ try {
+ assertName(fieldName)
+ } catch (e) {
+ throw new Error(`Attempted to add field ${JSON.stringify(fieldName)} to object type ${Self.name}, but that's not a valid name: ${e.message}`)
+ }
build.extend(fieldScope, scope, "Adding the object type scope to the field's scope");
if (!isString(fieldName)) {
throw new Error("It looks like you forgot to pass the fieldName to `fieldWithHooks`, we're sorry this is current necessary.");
105 changes: 105 additions & 0 deletions patches/graphile-build-pg+5.0.0-beta.32.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
diff --git a/node_modules/graphile-build-pg/dist/plugins/PgAttributesPlugin.js b/node_modules/graphile-build-pg/dist/plugins/PgAttributesPlugin.js
index 88c6973..e523ea1 100644
--- a/node_modules/graphile-build-pg/dist/plugins/PgAttributesPlugin.js
+++ b/node_modules/graphile-build-pg/dist/plugins/PgAttributesPlugin.js
@@ -120,21 +120,25 @@ exports.PgAttributesPlugin = {
add: {
_attributeName(options, { attributeName, codec }) {
const attribute = codec.attributes[attributeName];
- return this.coerceToGraphQLName(attribute.extensions?.tags?.name || attributeName);
+ const name = attribute.extensions?.tags?.name || attributeName
+ // Avoid conflict with 'id' field used for Relay.
+ const nonconflictName =
+ name === "id" && !codec.isAnonymous
+ ? "row_id"
+ : name;
+ return this.coerceToGraphQLName(
+ nonconflictName
+ );
},
_joinAttributeNames(options, codec, names) {
return names
.map((attributeName) => {
- return this.attribute({ attributeName, codec });
+ return this._attributeName({ attributeName, codec });
})
.join("-and-");
},
attribute(options, details) {
- const attributeFieldName = this.camelCase(this._attributeName(details));
- // Avoid conflict with 'id' field used for Relay.
- return attributeFieldName === "id" && !details.codec.isAnonymous
- ? "rowId"
- : attributeFieldName;
+ return this.camelCase(this._attributeName(details));
},
},
},
diff --git a/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.d.ts b/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.d.ts
index 04ed8f0..2620bb0 100644
--- a/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.d.ts
+++ b/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.d.ts
@@ -45,6 +45,8 @@ declare global {
}): string;
singleRelation(this: Inflection, details: PgRelationsPluginRelationDetails): string;
singleRelationBackwards(this: Inflection, details: PgRelationsPluginRelationDetails): string;
+ _singleRelationRaw(this: Inflection, details: PgRelationsPluginRelationDetails): string;
+ _singleRelationBackwardsRaw(this: Inflection, details: PgRelationsPluginRelationDetails): string;
_manyRelation(this: Inflection, details: PgRelationsPluginRelationDetails): string;
manyRelationConnection(this: Inflection, details: PgRelationsPluginRelationDetails): string;
manyRelationList(this: Inflection, details: PgRelationsPluginRelationDetails): string;
diff --git a/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.js b/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.js
index 117fc39..85d19fc 100644
--- a/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.js
+++ b/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.js
@@ -42,7 +42,7 @@ exports.PgRelationsPlugin = {
const attributeNames = attributes.map((col) => col.attname);
return this.camelCase(`${isUnique ? remoteName : this.pluralize(remoteName)}-by-${isReferencee ? "their" : "my"}-${attributeNames.join("-and-")}`);
},
- singleRelation(options, details) {
+ _singleRelationRaw(options, details) {
const { registry, codec, relationName } = details;
const relation = registry.pgRelations[codec.name]?.[relationName];
//const codec = relation.remoteResource.codec;
@@ -52,9 +52,12 @@ exports.PgRelationsPlugin = {
// E.g. posts(author_id) references users(id)
const remoteType = this.tableType(relation.remoteResource.codec);
const localAttributes = relation.localAttributes;
- return this.camelCase(`${remoteType}-by-${this._joinAttributeNames(codec, localAttributes)}`);
+ return (`${remoteType}-by-${this._joinAttributeNames(codec, localAttributes)}`);
},
- singleRelationBackwards(options, details) {
+ singleRelation(options, details) {
+ return this.camelCase(this._singleRelationRaw(details))
+ },
+ _singleRelationBackwardsRaw(options, details) {
const { registry, codec, relationName } = details;
const relation = registry.pgRelations[codec.name]?.[relationName];
if (typeof relation.extensions?.tags.foreignSingleFieldName === "string") {
@@ -66,9 +69,12 @@ exports.PgRelationsPlugin = {
// E.g. posts(author_id) references users(id)
const remoteType = this.tableType(relation.remoteResource.codec);
const remoteAttributes = relation.remoteAttributes;
- return this.camelCase(`${remoteType}-by-${this._joinAttributeNames(relation.remoteResource.codec, remoteAttributes)}`);
+ return (`${remoteType}-by-${this._joinAttributeNames(relation.remoteResource.codec, remoteAttributes)}`);
},
- _manyRelation(options, details) {
+ singleRelationBackwards(options, details) {
+ return this.camelCase(this._singleRelationBackwardsRaw(details))
+ },
+ _manyRelationRaw(options, details) {
const { registry, codec, relationName } = details;
const relation = registry.pgRelations[codec.name]?.[relationName];
const baseOverride = relation.extensions?.tags.foreignFieldName;
@@ -78,7 +84,10 @@ exports.PgRelationsPlugin = {
// E.g. users(id) references posts(author_id)
const remoteType = this.tableType(relation.remoteResource.codec);
const remoteAttributes = relation.remoteAttributes;
- return this.camelCase(`${this.pluralize(remoteType)}-by-${this._joinAttributeNames(relation.remoteResource.codec, remoteAttributes)}`);
+ return (`${this.pluralize(remoteType)}-by-${this._joinAttributeNames(relation.remoteResource.codec, remoteAttributes)}`);
+ },
+ _manyRelation(options, details) {
+ return this.camelCase(this._manyRelationRaw(details))
},
manyRelationConnection(options, details) {
const { registry, codec, relationName } = details;
18 changes: 18 additions & 0 deletions patches/postgraphile+5.0.0-beta.33.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
diff --git a/node_modules/postgraphile/dist/presets/v4.js b/node_modules/postgraphile/dist/presets/v4.js
index a8d69be..44ba8cb 100644
--- a/node_modules/postgraphile/dist/presets/v4.js
+++ b/node_modules/postgraphile/dist/presets/v4.js
@@ -58,9 +58,10 @@ const makeV4Plugin = (options) => {
? null
: {
// Don't rename 'id' to 'rowId'
- attribute(previous, options, details) {
- const attributeFieldName = this.camelCase(this._attributeName(details));
- return attributeFieldName;
+ _attributeName(previous, options, {attributeName, codec}) {
+ const attribute = codec.attributes[attributeName];
+ const name = attribute.extensions?.tags?.name || attributeName
+ return this.coerceToGraphQLName(name);
},
}),
},
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ export const PgOrderByRelatedPlugin: GraphileConfig.Plugin = {
];
const prefix = this.constantCase(
relation.isReferencee
? this.singleRelationBackwards(relationDetails)
: this.singleRelation(relationDetails)
? this._singleRelationBackwardsRaw(relationDetails)
: this._singleRelationRaw(relationDetails)
);
if (!relation.isUnique)
throw new Error(
Expand All @@ -96,8 +96,8 @@ export const PgOrderByRelatedPlugin: GraphileConfig.Plugin = {
];
const prefix = this.constantCase(
relation.isReferencee
? this.singleRelationBackwards(relationDetails)
: this.singleRelation(relationDetails)
? this._singleRelationBackwardsRaw(relationDetails)
: this._singleRelationRaw(relationDetails)
);
if (!relation.isUnique)
throw new Error(
Expand Down
Loading

0 comments on commit 82d769c

Please sign in to comment.