-
-
Notifications
You must be signed in to change notification settings - Fork 569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(postgraphql) add extendedErrors option #513
Conversation
Note: this allows the user to expose the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent start, couple minor niggles plus I'd love an integration test for this. To do so:
- add your throw_test_error to https://github.com/postgraphql/postgraphql/blob/master/examples/kitchen-sink/schema.sql (put it in schema
a
unless another one seems more appropriate - add a new gqlSchema with your required options here: https://github.com/postgraphql/postgraphql/blob/e62d7af5c47065a0aba27f0518c02ac795931d89/src/postgraphql/__tests__/postgraphqlIntegrationQueries-test.js#L33-L35
- add a new graphql query triggering the error here: https://github.com/postgraphql/postgraphql/tree/e62d7af5c47065a0aba27f0518c02ac795931d89/src/postgraphql/__tests__/fixtures/queries
- run the test suite
- review the generated snapshots to see if they're what you'd expect for the given options
- commit everything including the snapshots
Let me know if you want any further pointers 👍
hint: string, | ||
detail: string, | ||
code: string, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this into src/postgraphql
as it's done as an operation after graphql resolves rather than being part of the graphql resolving itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, yes, makes sense. done
throw new Error('Received null or undefined error.') | ||
} | ||
const originalError = error.originalError as GraphQLErrorExtended | ||
fields = fields.map(field => field.toLowerCase()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be done when the option is passed to postgraphql so it's only executed once in total rather than once for every error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes that would be more efficient. done
@@ -21,6 +21,7 @@ type PostGraphQLOptions = { | |||
jwtPgTypeIdentifier?: string, | |||
watchPg?: boolean, | |||
showErrorStack?: boolean, | |||
extendedErrors?: Array<string>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Process it to lower case in this file 👍
(Regarding my comment on
fields = fields.map(field => field.toLowerCase())
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done :)
I had indeed tried that same integration test approach before, but encountered errors I did not fully understand, so I backed it out. For example:
I was puzzled because But here's the problem: we're executing all the graphql queries in parallel, using the same pg client! Thus, if any of the queries causes an error, it causes the others to fail during that same transaction on that same connection. I am going to try resolving that by executing the integration test graphql queries in sequence instead. |
Good idea; return await withPgClient(async pgClient => {
// Add data to the client instance we are using.
await pgClient.query(await kitchenSinkData)
for (const fileName of queryFileNames) { // <--
await pgClient.query("savepoint pretest"); // <--
// Read the query from the file system.
const query = await new Promise((resolve, reject) => {
readFile(resolvePath(queriesDir, fileName), 'utf8', (error, data) => {
if (error) reject(error)
else resolve(data)
})
})
await pgClient.query("rollback to savepoint pretest"); // <--
... |
It appears that the savepoint doesn't prevent a raised exception from rendering the transaction unusable. Here's an isolated SQL test case:
yields:
So, it appears the only way to ensure that a query that could raise an exception does not affect other tests is to run it in a separate transaction, which means running it in a separate |
@zopf you didn't rollback to savepoint after the second one before the commit? cat <<HERE | psql postgres
begin;
savepoint foo;
do \$\$ begin raise exception 'Exception thrown from PL/PGSQL'; end; \$\$ language plpgsql;
rollback to savepoint foo;
do \$\$ begin raise exception 'Exception thrown from PL/PGSQL'; end; \$\$ language plpgsql;
rollback to savepoint foo;
select 1;
commit;
HERE
|
Sorry, should have posted my
It turns out that I was executing this on a client that maintained the same connection. After the commit, when I ran the script again, it would give me the If I just run it once on a fresh connection, or if I run it via the command line, it appears to work properly. After some refactoring, it looks like the |
Soooo after figuring out how to get all these tests to run in sequence effectively instead of in parallel, I realized that since this is just the schema passing into I wrote a couple simple tests in |
Oh gosh, I'm sorry I should have realised that we were testing the wrong thing! 😳 |
Just some minor lint issues to fix |
Ok, lint issues fixed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last tweak and then we're good to merge I think? 👍
(Have you tested with the CLI?)
@@ -122,6 +123,7 @@ const server = createServer(postgraphql(pgConfig, schemas, { | |||
pgDefaultRole, | |||
watchPg, | |||
showErrorStack, | |||
extendedErrors, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You've added the options here, but you've not added them to the list on lines 23-45? Also you may need to coerce to array here by splitting on commas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely right. Fixed, tested with CLI, and cleaned up docs
f39b991
to
19d2d58
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay, I missed that you'd updated it!
This looks good to me, the only issue is the toLowerCase
because if we later need to access err.camelCaseThing
for whatever reason we'll need a breaking API change to do it (a major version bump).
For that reason, I'm going to remove the toLowerCase and merge - an update to the documentation to reflect this would be most welcome.
@zopf If you're happy with my changes I'll get this merged and released in 3.4.0 sometime next week 👍 |
* feat(postgraphql) add extendedErrors option * feat(postgraphql) add extendedErrors option tests * Remove `toLowerCase`, filter empty strings
In response to issue #485. Adds the
extendedErrors
option to thepostgraphql
middleware and CLI. Includes documentation update, but no tests since I could not find a good way to fit an error-producing test into the kitchen-sink integration test suite.Example: