-
Notifications
You must be signed in to change notification settings - Fork 24
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
Refactor RemoteArgumentSource to be a sealed class #487
Merged
+165
−181
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
26 changes: 13 additions & 13 deletions
26
lib/src/main/java/graphql/nadel/dsl/RemoteArgumentSource.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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
package graphql.nadel.dsl | ||
|
||
import graphql.language.Value | ||
import graphql.nadel.util.AnyAstValue | ||
|
||
// todo this should be a union or sealed class thing | ||
data class RemoteArgumentSource( | ||
val argumentName: String?, // for OBJECT_FIELD | ||
val pathToField: List<String>?, | ||
val staticValue: Value<*>?, | ||
val sourceType: SourceType, | ||
) { | ||
enum class SourceType { | ||
ObjectField, | ||
FieldArgument, | ||
StaticArgument | ||
} | ||
sealed class RemoteArgumentSource { | ||
data class ObjectField( | ||
val pathToField: List<String>, | ||
) : RemoteArgumentSource() | ||
|
||
data class FieldArgument( | ||
val argumentName: String, | ||
) : RemoteArgumentSource() | ||
|
||
data class StaticArgument( | ||
val staticValue: AnyAstValue, | ||
) : RemoteArgumentSource() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,11 @@ import graphql.language.InputObjectTypeDefinition | |
import graphql.language.ObjectValue | ||
import graphql.language.SDLDefinition | ||
import graphql.language.StringValue | ||
import graphql.language.TypeDefinition | ||
import graphql.language.Value | ||
import graphql.nadel.dsl.FieldMappingDefinition | ||
import graphql.nadel.dsl.RemoteArgumentDefinition | ||
import graphql.nadel.dsl.RemoteArgumentSource | ||
import graphql.nadel.dsl.RemoteArgumentSource.SourceType | ||
import graphql.nadel.dsl.TypeMappingDefinition | ||
import graphql.nadel.dsl.NadelHydrationDefinition | ||
import graphql.nadel.dsl.NadelHydrationConditionDefinition | ||
|
@@ -328,37 +328,20 @@ object NadelDirectives { | |
} | ||
|
||
private fun createRemoteArgumentSource(value: Value<*>): RemoteArgumentSource { | ||
if (value is StringValue) { | ||
val values = listFromDottedString(value.value) | ||
return when (values.first()) { | ||
"\$source" -> RemoteArgumentSource( | ||
argumentName = null, | ||
return if (value is StringValue) { | ||
val values = value.value.split('.') | ||
|
||
when (values.first()) { | ||
"\$source" -> RemoteArgumentSource.ObjectField( | ||
pathToField = values.subList(1, values.size), | ||
staticValue = null, | ||
sourceType = SourceType.ObjectField, | ||
) | ||
|
||
"\$argument" -> RemoteArgumentSource( | ||
"\$argument" -> RemoteArgumentSource.FieldArgument( | ||
argumentName = values.subList(1, values.size).single(), | ||
pathToField = null, | ||
staticValue = null, | ||
sourceType = SourceType.FieldArgument, | ||
) | ||
|
||
else -> RemoteArgumentSource( | ||
argumentName = null, | ||
pathToField = null, | ||
staticValue = value, | ||
sourceType = SourceType.StaticArgument, | ||
) | ||
else -> RemoteArgumentSource.StaticArgument(staticValue = value) | ||
} | ||
} else { | ||
return RemoteArgumentSource( | ||
argumentName = null, | ||
pathToField = null, | ||
staticValue = value, | ||
sourceType = SourceType.StaticArgument, | ||
) | ||
RemoteArgumentSource.StaticArgument(staticValue = value) | ||
} | ||
} | ||
|
||
|
@@ -380,37 +363,20 @@ object NadelDirectives { | |
|
||
val remoteArgumentSource = if (remoteArgFieldValue != null && remoteArgArgValue != null) { | ||
throw IllegalArgumentException("$inputObjectTypeName can not have both $valueFromFieldKey and $valueFromArgKey set") | ||
} else if (remoteArgFieldValue != null) { | ||
createTemplatedRemoteArgumentSource(remoteArgFieldValue, SourceType.ObjectField) | ||
} else if (remoteArgArgValue != null) { | ||
createTemplatedRemoteArgumentSource(remoteArgArgValue, SourceType.FieldArgument) | ||
} else { | ||
throw IllegalArgumentException("$inputObjectTypeName requires one of $valueFromFieldKey or $valueFromArgKey to be set") | ||
if (remoteArgFieldValue != null) { | ||
RemoteArgumentSource.ObjectField(remoteArgFieldValue.removePrefix("\$source.").split('.')) | ||
} else if (remoteArgArgValue != null) { | ||
RemoteArgumentSource.FieldArgument(remoteArgArgValue.removePrefix("\$argument.")) | ||
} else { | ||
throw IllegalArgumentException("$inputObjectTypeName requires one of $valueFromFieldKey or $valueFromArgKey to be set") | ||
} | ||
} | ||
|
||
RemoteArgumentDefinition(remoteArgName, remoteArgumentSource) | ||
} | ||
} | ||
|
||
private fun createTemplatedRemoteArgumentSource(value: String, argumentType: SourceType): RemoteArgumentSource { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inlined this function, it's much simpler now with the sealed classes. |
||
// for backwards compat reasons - we will allow them to specify "$source.field.name" and treat it as just "field.name" | ||
val values = value | ||
.removePrefix("\$source.") | ||
.removePrefix("\$argument.") | ||
.split('.') | ||
|
||
var argumentName: String? = null | ||
var path: List<String>? = null | ||
var staticValue: Value<*>? = null | ||
when (argumentType) { | ||
SourceType.ObjectField -> path = values | ||
SourceType.FieldArgument -> argumentName = values.single() | ||
SourceType.StaticArgument -> staticValue = StringValue(value) | ||
} | ||
|
||
return RemoteArgumentSource(argumentName, path, staticValue, argumentType) | ||
} | ||
|
||
internal fun createFieldMapping(fieldDefinition: GraphQLFieldDefinition): FieldMappingDefinition? { | ||
val directive = fieldDefinition.getAppliedDirective(renamedDirectiveDefinition.name) | ||
?: return null | ||
|
@@ -427,10 +393,6 @@ object NadelDirectives { | |
return TypeMappingDefinition(underlyingName = from, overallName = directivesContainer.name) | ||
} | ||
|
||
private fun listFromDottedString(from: String): List<String> { | ||
return from.split('.').toList() | ||
} | ||
|
||
private inline fun <reified T : Any> getDirectiveValue( | ||
directive: GraphQLAppliedDirective, | ||
name: String, | ||
|
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to fill in the other values because we create just
RemoteArgumentSource.ObjectField
now.