Skip to content

Commit

Permalink
refactor(rbac): tweak error message for missing input type for custom…
Browse files Browse the repository at this point in the history
… mutations (#375)
  • Loading branch information
benjie committed Jan 10, 2019
1 parent f90a61b commit edca926
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion packages/graphile-build-pg/src/plugins/PgBasicsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ function sqlCommentByAddingTags(entity, tagsToAdd) {
.reduce((memo, tag) => {
const tagValue = tags[tag];
const valueArray = Array.isArray(tagValue) ? tagValue : [tagValue];
const highlightOrNot = tag in tagsToAdd ? chalk.bold : identity;
const highlightOrNot = tag in tagsToAdd ? chalk.bold.green : identity;
valueArray.forEach(value => {
memo.push(
highlightOrNot(
Expand Down
18 changes: 12 additions & 6 deletions packages/graphile-build-pg/src/plugins/makeProcField.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Build, FieldWithHooksFunction } from "graphile-build";
import type { PgProc } from "./PgIntrospectionPlugin";
import type { SQL } from "pg-sql2";
import debugSql from "./debugSql";
import chalk from "chalk";

const firstValue = obj => {
let firstKey;
Expand Down Expand Up @@ -129,16 +130,21 @@ export default function makeProcField(
const Type = pgGetGqlInputTypeByTypeIdAndModifier(type.id, variant);
if (!Type) {
const hint = type.class
? `; you might want to use smart comments, e.g. 'COMMENT ON FUNCTION "${
proc.namespace.name
}"."${proc.name}"(${argTypes
.map(t => `"${t.namespaceName}"."${t.name}"`)
.join(", ")}) IS E'@arg${idx}variant base';"`
? `; this might be because no INSERT column privileges are granted on ${describePgEntity(
type.class
)}. You can use smart comments to tell PostGraphile to instead use the "${chalk.bold.green(
"base"
)}" input type which includes all columns:\n\n ${sqlCommentByAddingTags(
proc,
{
[`arg${idx}variant`]: "base",
}
)}\n`
: "";
throw new Error(
`Could not determine type for argument ${idx} ('${
argNames[idx]
}') of function '${proc.name}'${hint}`
}') of function ${describePgEntity(proc)}${hint}`
);
}
if (idx >= notNullArgCount) {
Expand Down
6 changes: 3 additions & 3 deletions packages/graphile-build/src/swallowError.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ export default function swallowError(e: Error): void {
e && typeof e.toString === "function"
? String(e)
.replace(/\n.*/g, "")
.substr(0, 160)
.substr(0, 320)
.trim()
: null;
if (errorSnippet) {
// eslint-disable-next-line no-console
console.warn(
`Recoverable error occurred; use envvar 'DEBUG="graphile-build:warn"' for full error (see: http://graphile.org/postgraphile/debugging )\n> ${errorSnippet}…`
`Recoverable error occurred; use envvar 'DEBUG="graphile-build:warn"' for full error (see: https://graphile.org/postgraphile/debugging )\n> ${errorSnippet}…`
);
} else {
// eslint-disable-next-line no-console
console.warn(
`Recoverable error occurred; use envvar 'DEBUG="graphile-build:warn"' for error (see: http://graphile.org/postgraphile/debugging )`
`Recoverable error occurred; use envvar 'DEBUG="graphile-build:warn"' for error (see: https://graphile.org/postgraphile/debugging )`
);
}
debugWarn(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ exports[`column naming clash - rename 1`] = `
Adding ascending orderBy enum value for column "wont_cast_easy" on table "c"."edge_case". You can rename this field with:

[35m COMMENT ON COLUMN "c"."edge_case"."wont_cast_easy" IS E'[1m@name newNameHere[22m';[39m
[35m COMMENT ON COLUMN "c"."edge_case"."wont_cast_easy" IS E'[1m[32m@name newNameHere[35m[22m';[39m
The second entity was:
Adding ascending orderBy enum value for column "row_id" on table "c"."edge_case" (with smart comments: @name wontCastEasy). You can rename this field with:

[33m COMMENT ON COLUMN "c"."edge_case"."row_id" IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON COLUMN "c"."edge_case"."row_id" IS E'[1m[32m@name newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`function naming clash - computed 1`] = `
Expand All @@ -23,13 +23,13 @@ exports[`function naming clash - computed 1`] = `
Adding field for column "row_id" on table "c"."edge_case". You can rename this field with:

[35m COMMENT ON COLUMN "c"."edge_case"."row_id" IS E'[1m@name newNameHere[22m';[39m
[35m COMMENT ON COLUMN "c"."edge_case"."row_id" IS E'[1m[32m@name newNameHere[35m[22m';[39m
The second entity was:
Adding computed column for function "c"."edge_case_computed"(...args...) (with smart comments: @fieldName rowId). You can rename this field with:

[33m COMMENT ON FUNCTION "c"."edge_case_computed"(...arg types go here...) IS E'[1m@fieldName newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON FUNCTION "c"."edge_case_computed"(...arg types go here...) IS E'[1m[32m@fieldName newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`function naming clash - createPost 1`] = `
Expand All @@ -39,17 +39,17 @@ exports[`function naming clash - createPost 1`] = `
Adding table create payload type for table "a"."post". You can rename the table's GraphQL type via:

[35m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m';[39m
[35m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[35m[22m';[39m

or disable the built-in create mutation via:

[35m COMMENT ON TABLE "a"."post" IS E'[1m@omit create[22m';[39m
[35m COMMENT ON TABLE "a"."post" IS E'[1m[32m@omit create[35m[22m';[39m
The second entity was:
Adding mutation function payload type for function "a"."mutation_text_array"(...args...) (with smart comments: @name createPost). You can rename the function's GraphQL field (and its dependent types) via:

[33m COMMENT ON FUNCTION "a"."mutation_text_array"(...arg types go here...) IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON FUNCTION "a"."mutation_text_array"(...arg types go here...) IS E'[1m[32m@name newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`function naming clash - deletePost 1`] = `
Expand All @@ -59,13 +59,13 @@ exports[`function naming clash - deletePost 1`] = `
Adding table delete mutation payload type for table "a"."post". You can rename the table's GraphQL type via:

[35m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m';[39m
[35m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[35m[22m';[39m
The second entity was:
Adding mutation function payload type for function "a"."mutation_text_array"(...args...) (with smart comments: @name deletePost). You can rename the function's GraphQL field (and its dependent types) via:

[33m COMMENT ON FUNCTION "a"."mutation_text_array"(...arg types go here...) IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON FUNCTION "a"."mutation_text_array"(...arg types go here...) IS E'[1m[32m@name newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`function naming clash - nodeId 1`] = `
Expand All @@ -79,7 +79,7 @@ exports[`function naming clash - nodeId 1`] = `
Adding query field for function "c"."int_set_query"(...args...) (with smart comments: @name nodeId). You can rename this field with:

[33m COMMENT ON FUNCTION "c"."int_set_query"(...arg types go here...) IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON FUNCTION "c"."int_set_query"(...arg types go here...) IS E'[1m[32m@name newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`function naming clash - payload 1`] = `
Expand All @@ -89,13 +89,13 @@ exports[`function naming clash - payload 1`] = `
Adding function result edge type for function "c"."int_set_query"(...args...) (with smart comments: @name q). You can rename the function's GraphQL field (and its dependent types) via:

[35m COMMENT ON FUNCTION "c"."int_set_query"(...arg types go here...) IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
[35m COMMENT ON FUNCTION "c"."int_set_query"(...arg types go here...) IS E'[1m[32m@name newNameHere[35m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
The second entity was:
Adding table type for table "a"."post" (with smart comments: @name q_edge). You can rename the table's GraphQL type via:

[33m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`function naming clash - updatePost 1`] = `
Expand All @@ -105,13 +105,13 @@ exports[`function naming clash - updatePost 1`] = `
Adding table update mutation payload type for table "a"."post". You can rename the table's GraphQL type via:

[35m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m';[39m
[35m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[35m[22m';[39m
The second entity was:
Adding mutation function payload type for function "a"."mutation_text_array"(...args...) (with smart comments: @name updatePost). You can rename the function's GraphQL field (and its dependent types) via:

[33m COMMENT ON FUNCTION "a"."mutation_text_array"(...arg types go here...) IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON FUNCTION "a"."mutation_text_array"(...arg types go here...) IS E'[1m[32m@name newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`simple collections naming clash 1`] = `
Expand All @@ -121,13 +121,13 @@ exports[`simple collections naming clash 1`] = `
Backward relation (connection) for constraint "post_author_id_fkey" on table "a"."post" (with smart comments: @foreignFieldName clash). To rename this relation with smart comments:

[35m COMMENT ON CONSTRAINT "post_author_id_fkey" ON "a"."post" IS E'[1m@foreignFieldName newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
[35m COMMENT ON CONSTRAINT "post_author_id_fkey" ON "a"."post" IS E'[1m[32m@foreignFieldName newNameHere[35m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
The second entity was:
Backward relation (simple collection) for constraint "post_author_id_fkey" on table "a"."post" (with smart comments: @foreignFieldName clash). To rename this relation with smart comments:

[33m COMMENT ON CONSTRAINT "post_author_id_fkey" ON "a"."post" IS E'[1m@foreignSimpleFieldName newNameHere[22m\\n@foreignFieldName clash\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON CONSTRAINT "post_author_id_fkey" ON "a"."post" IS E'[1m[32m@foreignSimpleFieldName newNameHere[33m[22m\\n@foreignFieldName clash\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`table naming clash - condition 1`] = `
Expand All @@ -137,13 +137,13 @@ exports[`table naming clash - condition 1`] = `
Adding condition type for table "c"."person". You can rename the table's GraphQL type via:

[35m COMMENT ON TABLE "c"."person" IS E'[1m@name newNameHere[22m\\nPerson test comment';[39m
[35m COMMENT ON TABLE "c"."person" IS E'[1m[32m@name newNameHere[35m[22m\\nPerson test comment';[39m
The second entity was:
Adding table type for table "a"."post" (with smart comments: @name person_condition). You can rename the table's GraphQL type via:

[33m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`table naming clash - direct 1`] = `
Expand All @@ -153,13 +153,13 @@ exports[`table naming clash - direct 1`] = `
Adding connection "orderBy" argument for table "a"."post" (with smart comments: @name person). You can rename the table's GraphQL type via:

[35m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
[35m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[35m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
The second entity was:
Adding connection "orderBy" argument for table "c"."person". You can rename the table's GraphQL type via:

[33m COMMENT ON TABLE "c"."person" IS E'[1m@name newNameHere[22m\\nPerson test comment';[39m]
[33m COMMENT ON TABLE "c"."person" IS E'[1m[32m@name newNameHere[33m[22m\\nPerson test comment';[39m]
`;

exports[`table naming clash - mutation 1`] = `
Expand All @@ -169,7 +169,7 @@ exports[`table naming clash - mutation 1`] = `
Adding table type for table "a"."post" (with smart comments: @name mutation). You can rename the table's GraphQL type via:

[35m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
[35m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[35m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
The second entity was:
Expand All @@ -183,13 +183,13 @@ exports[`table naming clash - order 1`] = `
Adding connection "orderBy" argument for table "c"."person". You can rename the table's GraphQL type via:

[35m COMMENT ON TABLE "c"."person" IS E'[1m@name newNameHere[22m\\nPerson test comment';[39m
[35m COMMENT ON TABLE "c"."person" IS E'[1m[32m@name newNameHere[35m[22m\\nPerson test comment';[39m
The second entity was:
Adding table type for table "a"."post" (with smart comments: @name people_order_by). You can rename the table's GraphQL type via:

[33m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`table naming clash - query 1`] = `
Expand All @@ -203,7 +203,7 @@ exports[`table naming clash - query 1`] = `
Adding row by globally unique identifier field for table "a"."post" (with smart comments: @name query). You can rename this table via:

[33m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
[33m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[33m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m]
`;

exports[`table naming clash - subscription 1`] = `
Expand All @@ -213,7 +213,7 @@ exports[`table naming clash - subscription 1`] = `
Adding table type for table "a"."post" (with smart comments: @name subscription). You can rename the table's GraphQL type via:

[35m COMMENT ON TABLE "a"."post" IS E'[1m@name newNameHere[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
[35m COMMENT ON TABLE "a"."post" IS E'[1m[32m@name newNameHere[35m[22m\\nRest of existing \\'comment\\' \\nhere.';[39m
The second entity was:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ grant select(id, headline, body, author_id) on a.post to postgraphile_test_visit

-- DO NOT GRANT ANYTHING ON c.compound_key!
grant execute on function c.return_table_without_grants() to postgraphile_test_visitor;


-- SJE bug; do not grant other permissions on POST
grant execute on function a.post_with_suffix(a.post, text) to postgraphile_test_visitor;

0 comments on commit edca926

Please sign in to comment.