Skip to content

Commit 82d769c

Browse files
committed
Hack the V5 inflectors for naming consistency
1 parent 161f25d commit 82d769c

File tree

6 files changed

+404
-6
lines changed

6 files changed

+404
-6
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"format:check": "yarn format:all --list-different",
1212
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
1313
"prepack": "tsc -p tsconfig.build.json",
14+
"postinstall": "patch-package",
1415
"test": "tsc -p tsconfig.build.json && scripts/test"
1516
},
1617
"repository": {
@@ -32,6 +33,7 @@
3233
"eslint-plugin-prettier": "4.2.1",
3334
"eslint_d": "^14.2.2",
3435
"jest": "29.3.1",
36+
"patch-package": "^8.0.0",
3537
"pg": "8.8.0",
3638
"postgraphile": "^5.0.0-beta.33",
3739
"prettier": "2.8.0",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
diff --git a/node_modules/graphile-build/dist/newWithHooks/index.js b/node_modules/graphile-build/dist/newWithHooks/index.js
2+
index dc0d978..46e0a9c 100644
3+
--- a/node_modules/graphile-build/dist/newWithHooks/index.js
4+
+++ b/node_modules/graphile-build/dist/newWithHooks/index.js
5+
@@ -5,6 +5,7 @@ const grafast_1 = require("grafast");
6+
const graphql_1 = require("grafast/graphql");
7+
const util_1 = require("util");
8+
const utils_js_1 = require("../utils.js");
9+
+const { assertName } = require("graphql");
10+
const isString = (str) => typeof str === "string";
11+
const knownTypes = [
12+
graphql_1.GraphQLSchema,
13+
@@ -81,6 +82,11 @@ function makeNewWithHooks({ builder }) {
14+
const processedFields = [];
15+
const fieldWithHooks = (fieldScope, fieldSpec) => {
16+
const { fieldName } = fieldScope;
17+
+ try {
18+
+ assertName(fieldName)
19+
+ } catch (e) {
20+
+ throw new Error(`Attempted to add field ${JSON.stringify(fieldName)} to object type ${Self.name}, but that's not a valid name: ${e.message}`)
21+
+ }
22+
build.extend(fieldScope, scope, "Adding the object type scope to the field's scope");
23+
if (!isString(fieldName)) {
24+
throw new Error("It looks like you forgot to pass the fieldName to `fieldWithHooks`, we're sorry this is current necessary.");
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
diff --git a/node_modules/graphile-build-pg/dist/plugins/PgAttributesPlugin.js b/node_modules/graphile-build-pg/dist/plugins/PgAttributesPlugin.js
2+
index 88c6973..e523ea1 100644
3+
--- a/node_modules/graphile-build-pg/dist/plugins/PgAttributesPlugin.js
4+
+++ b/node_modules/graphile-build-pg/dist/plugins/PgAttributesPlugin.js
5+
@@ -120,21 +120,25 @@ exports.PgAttributesPlugin = {
6+
add: {
7+
_attributeName(options, { attributeName, codec }) {
8+
const attribute = codec.attributes[attributeName];
9+
- return this.coerceToGraphQLName(attribute.extensions?.tags?.name || attributeName);
10+
+ const name = attribute.extensions?.tags?.name || attributeName
11+
+ // Avoid conflict with 'id' field used for Relay.
12+
+ const nonconflictName =
13+
+ name === "id" && !codec.isAnonymous
14+
+ ? "row_id"
15+
+ : name;
16+
+ return this.coerceToGraphQLName(
17+
+ nonconflictName
18+
+ );
19+
},
20+
_joinAttributeNames(options, codec, names) {
21+
return names
22+
.map((attributeName) => {
23+
- return this.attribute({ attributeName, codec });
24+
+ return this._attributeName({ attributeName, codec });
25+
})
26+
.join("-and-");
27+
},
28+
attribute(options, details) {
29+
- const attributeFieldName = this.camelCase(this._attributeName(details));
30+
- // Avoid conflict with 'id' field used for Relay.
31+
- return attributeFieldName === "id" && !details.codec.isAnonymous
32+
- ? "rowId"
33+
- : attributeFieldName;
34+
+ return this.camelCase(this._attributeName(details));
35+
},
36+
},
37+
},
38+
diff --git a/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.d.ts b/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.d.ts
39+
index 04ed8f0..2620bb0 100644
40+
--- a/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.d.ts
41+
+++ b/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.d.ts
42+
@@ -45,6 +45,8 @@ declare global {
43+
}): string;
44+
singleRelation(this: Inflection, details: PgRelationsPluginRelationDetails): string;
45+
singleRelationBackwards(this: Inflection, details: PgRelationsPluginRelationDetails): string;
46+
+ _singleRelationRaw(this: Inflection, details: PgRelationsPluginRelationDetails): string;
47+
+ _singleRelationBackwardsRaw(this: Inflection, details: PgRelationsPluginRelationDetails): string;
48+
_manyRelation(this: Inflection, details: PgRelationsPluginRelationDetails): string;
49+
manyRelationConnection(this: Inflection, details: PgRelationsPluginRelationDetails): string;
50+
manyRelationList(this: Inflection, details: PgRelationsPluginRelationDetails): string;
51+
diff --git a/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.js b/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.js
52+
index 117fc39..85d19fc 100644
53+
--- a/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.js
54+
+++ b/node_modules/graphile-build-pg/dist/plugins/PgRelationsPlugin.js
55+
@@ -42,7 +42,7 @@ exports.PgRelationsPlugin = {
56+
const attributeNames = attributes.map((col) => col.attname);
57+
return this.camelCase(`${isUnique ? remoteName : this.pluralize(remoteName)}-by-${isReferencee ? "their" : "my"}-${attributeNames.join("-and-")}`);
58+
},
59+
- singleRelation(options, details) {
60+
+ _singleRelationRaw(options, details) {
61+
const { registry, codec, relationName } = details;
62+
const relation = registry.pgRelations[codec.name]?.[relationName];
63+
//const codec = relation.remoteResource.codec;
64+
@@ -52,9 +52,12 @@ exports.PgRelationsPlugin = {
65+
// E.g. posts(author_id) references users(id)
66+
const remoteType = this.tableType(relation.remoteResource.codec);
67+
const localAttributes = relation.localAttributes;
68+
- return this.camelCase(`${remoteType}-by-${this._joinAttributeNames(codec, localAttributes)}`);
69+
+ return (`${remoteType}-by-${this._joinAttributeNames(codec, localAttributes)}`);
70+
},
71+
- singleRelationBackwards(options, details) {
72+
+ singleRelation(options, details) {
73+
+ return this.camelCase(this._singleRelationRaw(details))
74+
+ },
75+
+ _singleRelationBackwardsRaw(options, details) {
76+
const { registry, codec, relationName } = details;
77+
const relation = registry.pgRelations[codec.name]?.[relationName];
78+
if (typeof relation.extensions?.tags.foreignSingleFieldName === "string") {
79+
@@ -66,9 +69,12 @@ exports.PgRelationsPlugin = {
80+
// E.g. posts(author_id) references users(id)
81+
const remoteType = this.tableType(relation.remoteResource.codec);
82+
const remoteAttributes = relation.remoteAttributes;
83+
- return this.camelCase(`${remoteType}-by-${this._joinAttributeNames(relation.remoteResource.codec, remoteAttributes)}`);
84+
+ return (`${remoteType}-by-${this._joinAttributeNames(relation.remoteResource.codec, remoteAttributes)}`);
85+
},
86+
- _manyRelation(options, details) {
87+
+ singleRelationBackwards(options, details) {
88+
+ return this.camelCase(this._singleRelationBackwardsRaw(details))
89+
+ },
90+
+ _manyRelationRaw(options, details) {
91+
const { registry, codec, relationName } = details;
92+
const relation = registry.pgRelations[codec.name]?.[relationName];
93+
const baseOverride = relation.extensions?.tags.foreignFieldName;
94+
@@ -78,7 +84,10 @@ exports.PgRelationsPlugin = {
95+
// E.g. users(id) references posts(author_id)
96+
const remoteType = this.tableType(relation.remoteResource.codec);
97+
const remoteAttributes = relation.remoteAttributes;
98+
- return this.camelCase(`${this.pluralize(remoteType)}-by-${this._joinAttributeNames(relation.remoteResource.codec, remoteAttributes)}`);
99+
+ return (`${this.pluralize(remoteType)}-by-${this._joinAttributeNames(relation.remoteResource.codec, remoteAttributes)}`);
100+
+ },
101+
+ _manyRelation(options, details) {
102+
+ return this.camelCase(this._manyRelationRaw(details))
103+
},
104+
manyRelationConnection(options, details) {
105+
const { registry, codec, relationName } = details;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
diff --git a/node_modules/postgraphile/dist/presets/v4.js b/node_modules/postgraphile/dist/presets/v4.js
2+
index a8d69be..44ba8cb 100644
3+
--- a/node_modules/postgraphile/dist/presets/v4.js
4+
+++ b/node_modules/postgraphile/dist/presets/v4.js
5+
@@ -58,9 +58,10 @@ const makeV4Plugin = (options) => {
6+
? null
7+
: {
8+
// Don't rename 'id' to 'rowId'
9+
- attribute(previous, options, details) {
10+
- const attributeFieldName = this.camelCase(this._attributeName(details));
11+
- return attributeFieldName;
12+
+ _attributeName(previous, options, {attributeName, codec}) {
13+
+ const attribute = codec.attributes[attributeName];
14+
+ const name = attribute.extensions?.tags?.name || attributeName
15+
+ return this.coerceToGraphQLName(name);
16+
},
17+
}),
18+
},

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ export const PgOrderByRelatedPlugin: GraphileConfig.Plugin = {
7070
];
7171
const prefix = this.constantCase(
7272
relation.isReferencee
73-
? this.singleRelationBackwards(relationDetails)
74-
: this.singleRelation(relationDetails)
73+
? this._singleRelationBackwardsRaw(relationDetails)
74+
: this._singleRelationRaw(relationDetails)
7575
);
7676
if (!relation.isUnique)
7777
throw new Error(
@@ -96,8 +96,8 @@ export const PgOrderByRelatedPlugin: GraphileConfig.Plugin = {
9696
];
9797
const prefix = this.constantCase(
9898
relation.isReferencee
99-
? this.singleRelationBackwards(relationDetails)
100-
: this.singleRelation(relationDetails)
99+
? this._singleRelationBackwardsRaw(relationDetails)
100+
: this._singleRelationRaw(relationDetails)
101101
);
102102
if (!relation.isUnique)
103103
throw new Error(

0 commit comments

Comments
 (0)