Skip to content
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

getTypeMap() does not include input types used in directives #1189

Merged
merged 3 commits into from
Jan 8, 2018

Conversation

Yogu
Copy link
Contributor

@Yogu Yogu commented Jan 2, 2018

The result of getTypeMap() recursively includes all types referenced by root types, so I would expect it to include types referenced by directives, too.

If this is not intended, I think a test asserting the opposite would be a good idea.

@IvanGoncharov
Copy link
Member

The result of getTypeMap() recursively includes all types referenced by root types, so I would expect it to include types referenced by directives, too.

@Yogu You absolutely right 👍

Do you want to fix it yourself?
If so, you can add support for directives to typeMapReducer. And add all directives to initialTypes (make sense to rename it).

@Yogu
Copy link
Contributor Author

Yogu commented Jan 2, 2018

Ok great, I'll push a fix tomorrow.

However, I would have first collected the types of directive arguments, and then used the typeMapReducer as-is. Directives can't be defined indirectly, so we don't need a reducer for that. What do you think?

@Yogu Yogu force-pushed the directive-type-map branch 7 times, most recently from 1a85ba2 to 7321a18 Compare January 3, 2018 09:07
@Yogu
Copy link
Contributor Author

Yogu commented Jan 3, 2018

@IvanGoncharov Can you have a look on the fix?

@@ -269,3 +278,10 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {

return reducedMap;
}

function getDirectiveArgTypes(directive: GraphQLDirective) {
if (!isDirective(directive)) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need this check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a test that validateSchema throws a specific error when directives is invalid (a string instead of a proper instance). new GraphQLSchema() does not perform this validation, so for the mentioned test to pass, the constructor needs to accept the invalid directive.

Copy link
Member

Choose a reason for hiding this comment

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

@Yogu Make sense 👍

if (!isDirective(directive)) {
return [];
}
return directive.args.map(arg => getNamedType(arg.type));
Copy link
Member

Choose a reason for hiding this comment

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

locations: ['OBJECT'],
args: {
arg: {
type: DirectiveInputType,
Copy link
Member

Choose a reason for hiding this comment

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

Would be great to also test wrapped type maybe as one more argument

@@ -13,6 +13,7 @@ import {
isUnionType,
isInputObjectType,
isWrappingType,
getNamedType,
Copy link
Member

Choose a reason for hiding this comment

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

@Yogu Looks great now!

One last issue from me: Can you please remove this line?
It cause Travis build to fail:
image

@leebyron
Copy link
Contributor

leebyron commented Jan 8, 2018

This looks like it also won't work if a directive argument type is a list or non-null type. I would expect flow to also report an error with this PR

@Yogu
Copy link
Contributor Author

Yogu commented Jan 8, 2018

This looks like it also won't work if a directive argument type is a list or non-null type. I would expect flow to also report an error with this PR

@leebyron I initially had code to unwrap the named type, but @IvanGoncharov pointed out this should not be necessary. I'll add a test tomorrow to see if it works.

@leebyron
Copy link
Contributor

leebyron commented Jan 8, 2018

Ah - I think you're right, it's just not clear from the code. initialTypes is expected to be an array of named types, but the type reducer unwraps types since when recursing it encounters wrapped types

@leebyron leebyron merged commit 87c1bc3 into graphql:master Jan 8, 2018
@IvanGoncharov
Copy link
Member

IvanGoncharov commented Jan 8, 2018

@leebyron In Voyager we support selecting any type as a root of a graph:
image

It would be great to reuse typeMapReducer so what do you think about extracting it into utilities under getTypeDependencies name? Would you merge such PR?

Also, it's very useful for schema stitching since you need to find a minimal set of dependencies for the every injected type.

@Yogu Yogu deleted the directive-type-map branch January 8, 2018 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants