Skip to content

Commit

Permalink
feat(inflector): better errors, overriding possible with makeAddInfle…
Browse files Browse the repository at this point in the history
…ctorsPlugin (#315)

* Improve clash comment

* Give makeAddInflectorsPlugin a replace option
  • Loading branch information
benjie authored Oct 15, 2018
1 parent 1f3328e commit f437721
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/graphile-build-pg/src/plugins/PgBasicsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,8 @@ export default (function PgBasicsPlugin(
`delete-${this._singularizedTableName(table)}-payload`
);
},
})
}),
"Default inflectors from PgBasicsPlugin. You can override these with `makeAddInflectorsPlugin(..., true)`."
);
});
}: Plugin);
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default (function PgForwardRelationPlugin(builder) {
inflection,
pgQueryFromResolveData: queryFromResolveData,
pgOmit: omit,
sqlCommentByAddingTags,
describePgEntity,
} = build;
const {
Expand Down Expand Up @@ -172,7 +173,14 @@ export default (function PgForwardRelationPlugin(builder) {
}
),
},
`Adding forward relation for ${describePgEntity(constraint)}`
`Forward relation for ${describePgEntity(
constraint
)}. To rename this relation with smart comments:\n\n ${sqlCommentByAddingTags(
constraint,
{
fieldName: "newNameHere",
}
)}`
);
return memo;
}, {}),
Expand Down
23 changes: 19 additions & 4 deletions packages/graphile-utils/src/makeAddInflectorsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { Plugin } from "graphile-build";

export default function makeAddInflectorsPlugin(additionalInflectors: {
[str: string]: ((...args: Array<any>) => any);
}): Plugin {
export default function makeAddInflectorsPlugin(
additionalInflectors: {
[str: string]: ((...args: Array<any>) => any);
},
replace = false
): Plugin {
return builder => {
builder.hook("inflection", (inflection, build) => {
return build.extend(inflection, additionalInflectors);
if (replace) {
// Overwrite directly so that we don't lose the 'extend' hints
Object.assign(inflection, additionalInflectors);
return inflection;
} else {
return build.extend(
inflection,
additionalInflectors,
`Adding inflectors ('${Object.keys(additionalInflectors).join(
"', '"
)}') via makeAddInflectorsPlugin. You can pass \`true\` as the second argument to makeAddInflectorsPlugin to allow overwriting existing inflectors.`
);
}
});
};
}
14 changes: 8 additions & 6 deletions packages/postgraphile-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ export const postGraphileClassicIdsInflection = inflections.newInflector({
export const PostGraphileInflectionPlugin = function(builder: SchemaBuilder) {
builder.hook("inflection", (inflection: Inflection) => {
const previous = inflection.enumName;
return {
...inflection,
// Overwrite directly so that we don't lose the 'extend' hints
Object.assign(inflection, {
enumName(value: string) {
return this.constantCase(previous.call(this, value));
},
};
});
return inflection;
});
} as Plugin;

Expand All @@ -130,15 +131,16 @@ export const PostGraphileClassicIdsInflectionPlugin = function(
) {
builder.hook("inflection", (inflection: Inflection) => {
const previous = inflection._columnName;
return {
...inflection,
// Overwrite directly so that we don't lose the 'extend' hints
Object.assign(inflection, {
_columnName(attr: PgAttribute, options: { skipRowId?: boolean }) {
const previousValue = previous.call(this, attr, options);
return (options && options.skipRowId) || previousValue !== "id"
? previousValue
: this.camelCase("rowId");
},
};
});
return inflection;
});
} as Plugin;

Expand Down

0 comments on commit f437721

Please sign in to comment.