Skip to content

Export complexity directive #43

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

Conversation

dimatill
Copy link
Contributor

In such a way complexity directive can be reused in code first approach to include directive definition in generated typeDefs:

new GraphQLSchema({
  directives: [complexityDirective()]
})

Copy link
Collaborator

@ivome ivome left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

I added a few suggestions. Could you add some tests and docs for the changes?

@@ -26,6 +21,10 @@ export default function (options?: {}): ComplexityEstimator {
}
},
});
}

export default function (options: { name?: string } = {}): ComplexityEstimator {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's create a type for this that we can then also export ComplexityDirectiveOptions and reuse externally.


const directive = new GraphQLDirective({
name: mergedOptions.name,
export function complexityDirective(name: string = 'complexity') {
Copy link
Collaborator

@ivome ivome Feb 26, 2021

Choose a reason for hiding this comment

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

Suggested change
export function complexityDirective(name: string = 'complexity') {
export function createComplexityDirective(options?: ComplexityDirectiveOptions = {}) {

This way it's clearer that the function is not the directive itself but rather a creator, and passing the entire options object keeps the interface stable in case we decide to extend it later.

@dimatill
Copy link
Contributor Author

@ivome I've pushed suggested changes.
Had to use a workaround inside tests by rebuilding the code first schema from typeDefs because of graphql-js <= 14 prints descriptions in different ways (printSchema(schema) vs print(astNode)) and directive from code first schema has no astNode. left this comment inside tests. Let me know if you have any idea how to write tests in another way

@ivome ivome merged commit d2fc8b3 into slicknode:master Mar 4, 2021
@ivome
Copy link
Collaborator

ivome commented Mar 4, 2021

Thanks for adding those changes, looks good now. I'm fine with the workaround for graphql-js <= 14 since it's just in the tests. Once we drop support for the older version, we can remove this, but 14 is still pretty widely used.

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