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

add support for variables #23

Merged
merged 2 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions core/src/main/kotlin/me/lazmaid/kraph/Kraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ typealias FieldBlock = Kraph.FieldBuilder.() -> Unit
typealias CursorBlock = Kraph.CursorSelectionBuilder.() -> Unit
typealias NodeBlock = Kraph.NodeBuilder.() -> Unit

data class KraphVariableType(val value: String)
data class KraphVariable(val name: String, val type: KraphVariableType, val jsonValue: String) {
Copy link
Owner

Choose a reason for hiding this comment

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

I think these 2 classes deserve their own package. Maybe we can have the package for custom GraphQL type. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean a package that has a class with only those 2 lines of code?
Or any other class as well?

Copy link
Owner

Choose a reason for hiding this comment

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

For now, It would be another package with a file contain these 2 classes

val dollarName: String
get() = "\$$name"
}

class Kraph(f: Kraph.() -> Unit) {

internal lateinit var document: Document
internal val variables: Variables = Variables()

init {
f.invoke(this)
}

fun query(name: String? = null, builder: FieldBlock) {
val set = createSelectionSet("query", builder)
document = Document(Operation(OperationType.QUERY, selectionSet = set, name = name))
document = Document(Operation(OperationType.QUERY, selectionSet = set, name = name, arguments = variables.asArgument()), variables)
}

fun mutation(name: String? = null, builder: FieldBlock) {
val set = createSelectionSet("mutation", builder)
document = Document(Operation(OperationType.MUTATION, selectionSet = set, name = name))
document = Document(Operation(OperationType.MUTATION, selectionSet = set, name = name, arguments = variables.asArgument()), variables)
}

private fun createSelectionSet(name: String, f: FieldBlock): SelectionSet {
Expand Down Expand Up @@ -60,6 +67,11 @@ class Kraph(f: Kraph.() -> Unit) {
fragments[name]?.invoke(this) ?: throw NoSuchFragmentException("No fragment named \"$name\" has been defined.")
}

fun variable(name: String, type: String, jsonValue: String): KraphVariable =
KraphVariable(name, KraphVariableType(type), jsonValue).also {
variables.variables[name] = it
}

fun cursorConnection(name: String, first: Int = -1, last: Int = -1,
before: String? = null, after: String? = null,
builder: CursorBlock) {
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/kotlin/me/lazmaid/kraph/lang/DataEntry.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.lazmaid.kraph.lang

import me.lazmaid.kraph.KraphVariable
import me.lazmaid.kraph.KraphVariableType

/**
* Created by vwongsawangt on 8/6/2017 AD.
*/
Expand All @@ -21,6 +24,14 @@ internal sealed class DataEntry {
override fun print(format: PrintFormat) = value.toString()
}

class VariableData(private val value: KraphVariable) : DataEntry() {
override fun print(format: PrintFormat) = value.dollarName
}

class VariableType(private val value: KraphVariableType) : DataEntry() {
override fun print(format: PrintFormat) = value.value
}

class StringData(private val value: String) : DataEntry() {
override fun print(format: PrintFormat) =
if (format == PrintFormat.JSON) {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/kotlin/me/lazmaid/kraph/lang/Document.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ package me.lazmaid.kraph.lang
* Created by VerachadW on 10/2/2016 AD.
*/

internal class Document(internal val operation: Operation) : GraphQLNode() {
internal val variables: Variables = Variables()
internal class Document(internal val operation: Operation, internal val variables: Variables) : GraphQLNode() {
override fun print(
format: PrintFormat,
previousLevel: Int
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/kotlin/me/lazmaid/kraph/lang/GraphQLNode.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package me.lazmaid.kraph.lang

import me.lazmaid.kraph.KraphVariable
import me.lazmaid.kraph.KraphVariableType

abstract internal class GraphQLNode {
var level = 0

Expand Down Expand Up @@ -30,9 +33,11 @@ abstract internal class GraphQLNode {
is Float -> DataEntry.DecimalNumberData(value.toDouble())
is Boolean -> DataEntry.BooleanData(value)
is Double -> DataEntry.DecimalNumberData(value)
is KraphVariable -> DataEntry.VariableData(value)
is KraphVariableType -> DataEntry.VariableType(value)
is List<*> -> convertToArrayData(value)
is Map<*,*> -> convertToObjectData(value as Map<String, *>)
else -> throw RuntimeException("Unsupported Type")
else -> throw RuntimeException("Unsupported Type: $value")
}
}

Expand Down
18 changes: 15 additions & 3 deletions core/src/main/kotlin/me/lazmaid/kraph/lang/Variables.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
package me.lazmaid.kraph.lang

import me.lazmaid.kraph.KraphVariable

/**
* Created by OinkIguana on 10/25/2017 AD.
*/

// TODO: implement variables
internal class Variables() {
fun print(): String? = null
internal class Variables {
Copy link
Owner

Choose a reason for hiding this comment

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

All the node should implement GraphQLNode. So, it would be easier to to handle formatting logic

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it would be confusing if Variables implements GraphQLNode, since the parameters of print (format: PrintFormat, previousLevel: Int) don't make any sense.

Copy link
Owner

Choose a reason for hiding this comment

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

At this moment, It may not make sense much to make it implement GraphQLNode, but it will make it more easier if we need to implement some features that apply to all the node in the Kraph


val variables = mutableMapOf<String, KraphVariable>()

fun print(): String? = variables
.takeIf { it.isNotEmpty() }
?.let {
it.entries.joinToString(separator = ",", prefix = "{", postfix = "}") { "\"${it.key}\": ${it.value.jsonValue}" }
}

fun asArgument(): Argument? =
Copy link
Owner

Choose a reason for hiding this comment

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

IMO, this should be an extension of this class more than a member function itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't really care.
It would make some classes (such as Variables) not internal - do you mind?

Copy link
Owner

Choose a reason for hiding this comment

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

I think there is no different in this case. You may leave it like this.

variables.takeIf { it.isNotEmpty() }
?.let { Argument(it.values.map { it.dollarName to it.type }.toMap()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package me.lazmaid.kraph.test

import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import me.lazmaid.kraph.lang.*
import me.lazmaid.kraph.lang.relay.InputArgument
import me.lazmaid.kraph.lang.relay.Mutation
import me.lazmaid.kraph.lang.*
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on

class GraphQLPrintSpek : Spek({
data class Expectation(val normal: String, val pretty: String, val json: String)
Expand Down Expand Up @@ -224,7 +223,8 @@ class GraphQLPrintSpek : Spek({
for((operation, title, expectation) in tests) {
given(title) {
it("should print always JSON format in the JSON wrapper") {
assertThat(Document(operation).print(PrintFormat.NORMAL, 0), equalTo("{\"query\": \"${expectation.json}\", \"variables\": null, \"operationName\": ${operation.name?.let {"\"$it\""} ?: "null"}}"))
assertThat(Document(operation, Variables()).print(PrintFormat.NORMAL, 0),
equalTo("{\"query\": \"${expectation.json}\", \"variables\": null, \"operationName\": ${operation.name?.let {"\"$it\""} ?: "null"}}"))
}
}
}
Expand Down
15 changes: 5 additions & 10 deletions core/src/test/kotlin/me/lazmaid/kraph/test/RequestSpek.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package me.lazmaid.kraph.test

import com.natpryce.hamkrest.*
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import me.lazmaid.kraph.Kraph
import me.lazmaid.kraph.NoFieldsInSelectionSetException
import me.lazmaid.kraph.NoSuchFragmentException
import me.lazmaid.kraph.lang.OperationType
import me.lazmaid.kraph.lang.relay.CursorConnection
import me.lazmaid.kraph.lang.relay.PageInfo
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.given
Expand All @@ -17,25 +12,25 @@ class RequestSpek : Spek({
describe("Kraph Query Request format printers") {
val query = Kraph {
query("GetUserId") {
field("user", mapOf("name" to "UserName")) {
field("user", mapOf("name" to variable("name", "User", "{\"name\": \"UserName\"}"))) {
field("id")
}
}
}
describe("#toRequestString") {
given("document with simple query") {
it("should print the entire document") {
assertThat(query.toRequestString(), equalTo("{\"query\": \"query GetUserId { user (name: \\\"UserName\\\") { id } }\", \"variables\": null, \"operationName\": \"GetUserId\"}"))
assertThat(query.toRequestString(), equalTo("{\"query\": \"query GetUserId (\$name: User) { user (name: \$name) { id } }\", \"variables\": {\"name\": {\"name\": \"UserName\"}}, \"operationName\": \"GetUserId\"}"))
}
}
}
describe("#requestQueryString") {
it("should print just the query portion of the document") {
assertThat(query.requestQueryString(), equalTo("query GetUserId { user (name: \"UserName\") { id } }"))
assertThat(query.requestQueryString(), equalTo("query GetUserId (\$name: User) { user (name: \$name) { id } }"))
}
}
describe("#requestVariableString") {
// TODO: variables not yet implemented
assertThat(query.requestVariableString(), equalTo("{\"name\": {\"name\": \"UserName\"}}"))
}
describe("#requestOperationName") {
given("document with simple query") {
Expand Down