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

Schema validation: interfaces #229 #243

Merged

Conversation

TobiasPfeifer
Copy link
Contributor

initial schema validation method layout for interfaces for #229

@ghostdogpr can you do an early stage review of private def validateInterface(t: __Type)

@TobiasPfeifer TobiasPfeifer force-pushed the SchemaValidation_Interfaces branch from ef4c199 to 19122af Compare February 25, 2020 19:20
Copy link
Owner

@ghostdogpr ghostdogpr left a comment

Choose a reason for hiding this comment

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

@TobiasPfeifer looks good so far!

core/src/main/scala/caliban/validation/Validator.scala Outdated Show resolved Hide resolved
core/src/main/scala/caliban/validation/Validator.scala Outdated Show resolved Hide resolved
core/src/main/scala/caliban/validation/Validator.scala Outdated Show resolved Hide resolved
@TobiasPfeifer
Copy link
Contributor Author

TobiasPfeifer commented Feb 26, 2020

@ghostdogpr I've implemented fieldname and argumentname validation. I'm now thinking about return type and input type validation. Should I try to lift this validation to the type level?

A __Field could define a `type` of type __TypeReturn that has a kind: __TypeKindReturn that only has instances of the valid return type kinds.
Same for the __InputValue thats `type` can be __TypeInput that has kind: __TypeKindInput

That way __Type conceptually becomes a sealed trait with instances such as Scalar, Object, Union etc.

However it's possible for me to leave the types as they are and validate on them at runtime

@ghostdogpr
Copy link
Owner

ghostdogpr commented Feb 27, 2020

@TobiasPfeifer doing it at the type level would be better in the general case, however __Type and other classes prefixed with __ are modeled after the introspection schema and they are directly used for introspection in Caliban. If you use a sealed trait, __Type would become a union or an interface and wouldn't match with the schema from the spec. For that reason I think we have to stick to that and perform the check at runtime. Makes sense?

Copy link
Owner

@ghostdogpr ghostdogpr left a comment

Choose a reason for hiding this comment

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

One more thing I told to @zach-albia who's working on #228:
Enum and union were very simple and didn't really need unit tests (and I think Caliban by default can only generate valid ones) but here rules are more complex so I think it would be good to have some. The existing tests in ValidationSpec are all about query validation, so yours will be a bit different: you'll need to create a GraphQL object, and check if .interpreter returns the appropriate ValidationError.

core/src/main/scala/caliban/validation/Validator.scala Outdated Show resolved Hide resolved
@zalbia
Copy link
Contributor

zalbia commented Feb 27, 2020

@ghostdogpr Something came up so I haven't gotten around to pushing a WIP PR #228 yet. Looking to get back to it this Saturday.

@ghostdogpr
Copy link
Owner

@zach-albia no problem. There are a few rules in common in your 2 issues (and the one @mriceron is doing too) so keep an eye on the other PRs. Whoever does it first, the others can reuse 😄

@TobiasPfeifer
Copy link
Contributor Author

@ghostdogpr I'll add tests to ensure correct ValidationError when calling .interpreter

@TobiasPfeifer doing it at the type level would be better in the general case, however __Type and other classes prefixed with __ are modeled after the introspection schema and they are directly used for introspection in Caliban. If you use a sealed trait, __Type would become a union or an interface and wouldn't match with the schema from the spec. For that reason I think we have to stick to that and perform the check at runtime. Makes sense?

Sure. I'll implement runtime validation with this PR. If I feel challenged to find a way to do this on the type level without altering the __XYZ types for the introspection schema, I'll tackle this in a separate PR

@ghostdogpr
Copy link
Owner

#244 was merged so you can re-use the same file for tests (and maybe some of the code)

@TobiasPfeifer TobiasPfeifer force-pushed the SchemaValidation_Interfaces branch from fccd277 to be11dc4 Compare February 28, 2020 15:50
@TobiasPfeifer TobiasPfeifer force-pushed the SchemaValidation_Interfaces branch from c5356c4 to 2f25426 Compare February 28, 2020 22:47
@TobiasPfeifer TobiasPfeifer changed the title [WIP] Schema validation: interfaces #229 Schema validation: interfaces #229 Feb 28, 2020
@TobiasPfeifer
Copy link
Contributor Author

@ghostdogpr I restructured my code to match the structure of #244 and reused common validation methods

Regarding the tests: I think it's not possible to construct an Interface that has a field returning an INPUT_OBJECT, so there is no test for that rule. Same for the rule "no two fields may share the same name"

Copy link
Owner

@ghostdogpr ghostdogpr left a comment

Choose a reason for hiding this comment

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

Looks good! Just a few remarks to get better error messages and we can merge 👍

core/src/main/scala/caliban/validation/Validator.scala Outdated Show resolved Hide resolved
core/src/main/scala/caliban/validation/Validator.scala Outdated Show resolved Hide resolved
core/src/main/scala/caliban/validation/Validator.scala Outdated Show resolved Hide resolved
core/src/main/scala/caliban/validation/Validator.scala Outdated Show resolved Hide resolved
Copy link
Owner

@ghostdogpr ghostdogpr left a comment

Choose a reason for hiding this comment

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

All good! Thanks for contributing!

@ghostdogpr ghostdogpr merged commit a916ad6 into ghostdogpr:master Feb 29, 2020
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.

3 participants