-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
396 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...main/java/graphql/nadel/validation/hydration/NadelDefaultHydrationDefinitionValidation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package graphql.nadel.validation.hydration | ||
|
||
import graphql.nadel.definition.hydration.NadelDefaultHydrationDefinition | ||
import graphql.nadel.engine.util.getFieldAt | ||
import graphql.nadel.engine.util.unwrapAll | ||
import graphql.nadel.engine.util.whenType | ||
import graphql.nadel.validation.NadelDefaultHydrationFieldNotFoundError | ||
import graphql.nadel.validation.NadelDefaultHydrationIdArgumentNotFoundError | ||
import graphql.nadel.validation.NadelDefaultHydrationIncompatibleBackingFieldTypeError | ||
import graphql.nadel.validation.NadelSchemaValidationResult | ||
import graphql.nadel.validation.NadelServiceSchemaElement | ||
import graphql.nadel.validation.NadelValidationContext | ||
import graphql.nadel.validation.ok | ||
|
||
/** | ||
* Validates a `@defaultHydration` before it's actually put to use. | ||
*/ | ||
class NadelDefaultHydrationDefinitionValidation { | ||
context(NadelValidationContext) | ||
fun validate(type: NadelServiceSchemaElement.Type): NadelSchemaValidationResult { | ||
val defaultHydration = instructionDefinitions.getDefaultHydrationOrNull(type) | ||
?: return ok() | ||
|
||
return validate(type, defaultHydration) | ||
} | ||
|
||
context(NadelValidationContext) | ||
private fun validate( | ||
type: NadelServiceSchemaElement.Type, | ||
defaultHydration: NadelDefaultHydrationDefinition, | ||
): NadelSchemaValidationResult { | ||
val backingField = engineSchema.queryType.getFieldAt(defaultHydration.backingField) | ||
?: return NadelDefaultHydrationFieldNotFoundError(type, defaultHydration) | ||
|
||
val backingFieldType = backingField.type.unwrapAll() | ||
|
||
// Backing field should return the type declaring the @defaultHydration | ||
// It's ok if the backing field returns an abstract type and there are other possible options | ||
// We have validation elsewhere that dictates that the hydrated field type must be valid | ||
val backingFieldOutputTypeValid = backingFieldType.whenType( | ||
enumType = { enumType -> enumType.name == type.overall.name }, | ||
inputObjectType = { inputObjectType -> inputObjectType.name == type.overall.name }, | ||
interfaceType = { interfaceType -> | ||
interfaceType.name == type.overall.name | ||
|| engineSchema.getImplementations(interfaceType).contains(type.overall) | ||
}, | ||
objectType = { objectType -> objectType.name == type.overall.name }, | ||
scalarType = { scalarType -> scalarType.name == type.overall.name }, | ||
unionType = { unionType -> | ||
unionType.name == type.overall.name | ||
|| unionType.types.contains(type.overall) | ||
}, | ||
) | ||
|
||
if (!backingFieldOutputTypeValid) { | ||
return NadelDefaultHydrationIncompatibleBackingFieldTypeError(type, defaultHydration, backingField) | ||
} | ||
|
||
if (backingField.getArgument(defaultHydration.idArgument) == null) { | ||
return NadelDefaultHydrationIdArgumentNotFoundError(type, defaultHydration, backingField) | ||
} | ||
|
||
return ok() | ||
} | ||
} |
Oops, something went wrong.