Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Add support for customValidateFn and rename formatError to customFormatErrorFn #479

Merged
merged 2 commits into from Apr 6, 2019
Merged

Add support for customValidateFn and rename formatError to customFormatErrorFn #479

merged 2 commits into from Apr 6, 2019

Conversation

jamesmoriarty
Copy link
Contributor

Regarding: #474

@coveralls
Copy link

coveralls commented Mar 20, 2019

Coverage Status

Coverage increased (+0.02%) to 99.396% when pulling 4522dd9 on newscorpaus:feature/validation-cache into 7ab2bc9 on graphql:master.

@jamesmoriarty
Copy link
Contributor Author

@IvanGoncharov aka graphql overlord can you please review?

@jamesmoriarty jamesmoriarty changed the title add optional validationCache to improve performance. add optional validationCache to improve performance. #474 Mar 21, 2019
@jamesmoriarty jamesmoriarty changed the title add optional validationCache to improve performance. #474 add optional validationCache to improve performance. re: #474 Mar 21, 2019
src/index.js Outdated Show resolved Hide resolved
@jamesmoriarty
Copy link
Contributor Author

jamesmoriarty commented Mar 22, 2019

Hey @IvanGoncharov, the set cache property signature was a mistake. I've changed it to set(key, value) and get(key). I've changed the test to use LruMap.

src/index.js Outdated Show resolved Hide resolved
@ivome
Copy link

ivome commented Mar 24, 2019

I also think wrapping / replacing the validate function would be better. I wrote a validation rule to analyze the query complexity and reject complex queries that depends on the input variables: graphql-query-complexity
To use something like this with validation caching we would have to implement some custom logic.

Maybe we could even add a plugin system to express-graphql to have a well defined interface for things like this and even more complex extensions? This could also be used for #391.

Initialization could look something like this:

app.get('/graphql', graphqlHTTP({
  schema: MyGraphQLSchema,
  graphiql: true,
  plugins: [
    new ValidationCachePlugin(options),
    // Other plugins
    new FakeResultPlugin(),

    // We could also add hooks for things like...
    new ProfilingPlugin(),
    // etc.
  ]
}));

Might be overkill for just this one feature and this would definitely need some more detailed conception but I just wanted to throw this out here to see what you think...

src/index.js Outdated Show resolved Hide resolved
@jamesmoriarty
Copy link
Contributor Author

Thanks for the feedback @IvanGoncharov. Using the same pattern as the formatErrorworked great.

@IvanGoncharov
Copy link
Member

@jamesmoriarty Thanks 👍
Can you also cleanup test cases (it doesn't make sense to introduce new dependencies just for tests) and make it more like formatError/#391?

@jamesmoriarty
Copy link
Contributor Author

Removed unnecessary dependencies and cleaned up the tests. Looks consistent with formatError and your execute PR.

Screen Shot 2019-04-03 at 9 58 31 pm

@IvanGoncharov
Copy link
Member

@jamesmoriarty Thanks a lot 👍 I will try to merge it in the next few days.

One issue I still have is with both validate and execute is that they are not very intuitive names for the functionality they enable.
Maybe replaceValidate/replaceExecute?
Would be great to hear your thoughts?

@jamesmoriarty
Copy link
Contributor Author

I agree. The existing implementation made it possible. I've had a look at the graphql-js source to get ideas on consistency. I'm thinking customValidateFn customExecuteFn and possible deprecate formatError and change to customFormatErrorFn. What do you think @IvanGoncharov?

Based on:

Screen Shot 2019-04-04 at 9 21 56 am

@IvanGoncharov
Copy link
Member

@jamesmoriarty Great suggestion 👍 I like it.
I will update #391 to use customExecuteFn.

I'm thinking customValidateFn customExecuteFn and possible deprecate formatError and change to customFormatErrorFn

Make sense 👍
I think we should support deprecated formatError until 1.0.0 release.
Do you want to work on PR for this?

@jamesmoriarty
Copy link
Contributor Author

Sure thing.

@jamesmoriarty jamesmoriarty changed the title add optional validationCache to improve performance. re: #474 refine custom operations. re: #474 Apr 3, 2019
@jamesmoriarty
Copy link
Contributor Author

Made the change to the naming, deprecating formatError and updating the README. I imagine there is a better way to notify the user with the deprecation warning than using console.error. Any feedback appreciated.

src/index.js Outdated
}

formatErrorFn =
optionsData.customFormatErrorFn || optionsData.formatError;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@IvanGoncharov interestingly I couldn't change this to:

optionsData.customFormatErrorFn || optionsData.formatError || formatError
// ...
.map(errorFormatFn) 

Without breaking the tests. It appears the

.map(errorFormatFn || formatError) 

may be called before the options resolve. maybe around schema validation.

Might be worth resolving outside this change.

* formatError is deprecated.
* customFormatErrorFn to replace formatError.
@jamesmoriarty
Copy link
Contributor Author

Added to README. Thanks for spending time on this @IvanGoncharov. Much appreciated.

@IvanGoncharov
Copy link
Member

@jamesmoriarty I made a few small fixes like replacing console.error with console.warn and fixing ESLint warnings.

@IvanGoncharov IvanGoncharov merged commit 898a033 into graphql:master Apr 6, 2019
@IvanGoncharov IvanGoncharov changed the title refine custom operations. re: #474 Add support for customValidateFn and rename formatError to customFormatErrorFn Apr 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants