Skip to content

allow formatErrorMessage callback #3

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

Merged
merged 9 commits into from
Jun 27, 2017
Merged

allow formatErrorMessage callback #3

merged 9 commits into from
Jun 27, 2017

Conversation

chirag04
Copy link
Contributor

@chirag04 chirag04 commented Jun 21, 2017

something like this can be useful for logging the cost. wdyt?

src/index.js Outdated
@@ -149,7 +149,9 @@ export function createComplexityLimitRule(maxCost, options = {}) {
}

if (node.kind === 'Document') {
if (visitor.getCost() > maxCost) {
const cost = visitor.getCost();
if (options.onCost) options.onCost(cost);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also pass the query as arg to onCost?

Copy link
Contributor Author

@chirag04 chirag04 Jun 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@taion I still think passing the document node might be useful to both these functions. we maybe want to log queries that fail in prod as well?

@chirag04 chirag04 requested a review from taion June 21, 2017 15:17
@taion
Copy link
Contributor

taion commented Jun 21, 2017

I'd rather have some callback for formatting the error message to send up to the client.

@taion taion closed this Jun 21, 2017
@taion taion reopened this Jun 21, 2017
@chirag04
Copy link
Contributor Author

ok. can you elaborate on the api? getErrorMessage(cost, documentNode) maybe?

@taion
Copy link
Contributor

taion commented Jun 21, 2017

just formatErrorMessage(cost) perhaps

@chirag04 chirag04 changed the title allow onCost callback for logging allow formatErrorMessage callback Jun 21, 2017
@taion
Copy link
Contributor

taion commented Jun 21, 2017

Wait, is the idea here that you want to log all costs on queries? Or what's the goal here? Maybe it'd be better to re-use the visitor to create a different rule that only does logging?

@chirag04
Copy link
Contributor Author

initially i thought about logging all costs but figured it makes sense to do it only when validation fails. no? so logging when validation fails is the goal.

@taion
Copy link
Contributor

taion commented Jun 21, 2017

I think there are two separate concerns – what to do right now in dev to figure out how to set cost limits, then what we'll want to do in a production deploy.

@taion
Copy link
Contributor

taion commented Jun 22, 2017

Thinking about this some more, probably 4226b58 is the right way to go.

@chirag04
Copy link
Contributor Author

it'd be better to re-use the visitor to create a different rule that only does logging. idk how that would work.

@taion maybe do both. onCost and formatErrorMessage. onCost solves all our usecases but i can imagine somebody needing formatErrorMessage. wdyt?

@taion
Copy link
Contributor

taion commented Jun 22, 2017

Sure – still could be useful to tell without e.g. looking at server logs more details on queries that exceed the cost limit. You'd only want to turn it on in dev, though – would probably be a security issue to enable it in prod.

@chirag04
Copy link
Contributor Author

@taion anything left here?

src/index.js Outdated
const cost = visitor.getCost();
if (options.onCost) options.onCost(cost);
if (cost > maxCost) {
const errorMessage = options.formatErrorMessage ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just default formatErrorMessage to soemthing

src/index.js Outdated
@@ -149,9 +149,14 @@ export function createComplexityLimitRule(maxCost, options = {}) {
}

if (node.kind === 'Document') {
if (visitor.getCost() > maxCost) {
const cost = visitor.getCost();
if (options.onCost) options.onCost(cost);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split out these properties before passing down to visitor

src/index.js Outdated
if (cost > maxCost) {
const errorMessage = options.formatErrorMessage ?
options.formatErrorMessage(cost) :
`query exceeds complexity limit. Cost: ${cost}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should absolutely not always return the cost here; the calculation may be a private detail

README.md Outdated
@@ -20,6 +20,8 @@ const ComplexityLimitRule = createComplexityLimitRule(1000, {
scalarCost: 1,
objectCost: 10, // Default is 0.
listFactor: 20, // Default is 10.
onCost: (cost) => console.log('total'),
formatErrorMessage: (cost) => 'Bad Query',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these examples are sort of weak (plus don't follow eslint guidelines). also the graf above needs to explain these options.

@chirag04
Copy link
Contributor Author

@taion updated.

@taion taion merged commit 7da8dae into master Jun 27, 2017
@taion taion deleted the on-cost branch June 27, 2017 02:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants