Skip to content

Commit

Permalink
fix(DocGenerator): entities syntax and idempotent mock values (#562)
Browse files Browse the repository at this point in the history
* fix(DocGenerator): entities syntax

* don't randomize values on every build

* fix typo
  • Loading branch information
Cole Turner authored May 23, 2023
1 parent 6093f9e commit d91aac3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import graphql.schema.idl.TypeUtil
import kotlinx.serialization.json.*
import java.math.BigDecimal
import java.math.BigInteger
import kotlin.math.abs
import kotlin.random.Random

@Suppress("FoldInitializerAndIfToElvis")
class DocGenerator(private val config: CodeGenConfig, private val document: Document) {
Expand Down Expand Up @@ -116,13 +114,15 @@ class DocGenerator(private val config: CodeGenConfig, private val document: Docu
private fun getExampleEntitiesQuery(definition: ObjectTypeDefinition): String? {
val gql: String = """
query(${'$'}representations: [_Any!]!) {
... on ${definition.name} {
${definition.fieldDefinitions.map {
entities(representations: ${'$'}representations) {
... on ${definition.name} {
${definition.fieldDefinitions.map {
val selectionSet : List<String> = getSelectionSet(it.type.findTypeDefinition(document))
"""
${it.name}${if (it.inputValueDefinitions.size > 0) "(${it.inputValueDefinitions.map{ "${it.name}: ${getMockGQLValueAsAST(it.type)}"}.joinToString(", ")})" else ""} ${if (selectionSet.size > 0) "{${selectionSet.joinToString("\n")}}" else ""}
"""
}.joinToString("\n")}
}
}
}
""".trimIndent()
Expand Down Expand Up @@ -194,11 +194,11 @@ class DocGenerator(private val config: CodeGenConfig, private val document: Docu

if (type is TypeName) {
return when (type.name) {
"ID" -> StringValue("random${Random.nextInt()}")
"ID" -> StringValue("random12345")
"String" -> StringValue("randomString")
"Boolean" -> BooleanValue(Random.nextBoolean())
"Int" -> IntValue(BigInteger.valueOf(abs(Random.nextLong())))
"Float" -> FloatValue(BigDecimal.valueOf(abs(Random.nextDouble())))
"Boolean" -> BooleanValue(true)
"Int" -> IntValue(BigInteger.valueOf(123456789))
"Float" -> FloatValue(BigDecimal.valueOf(12345.6789))
else -> {
getSchemaTypeMockValue(type)
}
Expand Down
8 changes: 5 additions & 3 deletions graphql-dgs-codegen-gradle/generated-docs/Entities.Result.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
### Query
```graphql
query ($representations: [_Any!]!) {
... on Result {
isSuccessful
result
entities(representations: $representations) {
... on Result {
isSuccessful
result
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion graphql-dgs-codegen-gradle/generated-docs/Query.find.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
## Example
```graphql
{
find(filter: {mandatoryString : "randomString", optionalString : "randomString", mandatoryNumber : 269109734867147797, optionalNumber : 6032492703890891451}) {
find(filter: {mandatoryString : "randomString", optionalString : "randomString", mandatoryNumber : 123456789, optionalNumber : 123456789}) {
isSuccessful
result
}
Expand Down

0 comments on commit d91aac3

Please sign in to comment.