-
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
5 changed files
with
135 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# KraphQL [![Build Status](https://travis-ci.org/taskworld/kraph.svg?branch=master)](https://travis-ci.org/taskworld/kraph) [ ![Download](https://api.bintray.com/packages/tw/maven/kraph/images/download.svg) ](https://bintray.com/tw/maven/kraph/_latestVersion) | ||
GraphQL query builder written in Kotlin | ||
|
||
Note that this library is still in alpha stage. The syntax usage may subject to change. Please see the example from unit tests file for now. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ import org.junit.runner.RunWith | |
* Created by VerachadW on 9/20/2016 AD. | ||
*/ | ||
@RunWith(JUnitPlatform::class) | ||
class LangSpek : Spek({ | ||
class BuilderSpek : Spek({ | ||
describe("KraphQL Query DSL Builder") { | ||
given("smaple query") { | ||
val query = KraphQL { | ||
|
@@ -59,7 +59,6 @@ class LangSpek : Spek({ | |
assertThat(query.document.operation.fields[0].selectionSet!!.fields[3].name, equalTo("avatarUrl")) | ||
} | ||
it("should have size argument with value 100 for avatarUrl field") { | ||
assertThat(query.document.operation.fields[0].selectionSet!!.fields[3].arguments!!.args.keys, hasElement("size")) | ||
assertThat(query.document.operation.fields[0].selectionSet!!.fields[3].arguments!!.args["size"] as Int, equalTo(100)) | ||
} | ||
it("should have two fields inside author object") { | ||
|
@@ -72,5 +71,45 @@ class LangSpek : Spek({ | |
assertThat(query.document.operation.fields[0].selectionSet!!.fields[2].selectionSet!!.fields[1].name, equalTo("email")) | ||
} | ||
} | ||
given("sample mutation") { | ||
val query = KraphQL { | ||
mutation { | ||
func("registerUser", mapOf("email" to "[email protected]", "password" to "abcd1234", "age" to 30)) { | ||
field("id") | ||
field("token") | ||
} | ||
} | ||
} | ||
it("should have mutation operation type") { | ||
assertThat(query.document.operation.type, isA(equalTo(OperationType.MUTATION))) | ||
} | ||
it("should have only 1 mutation") { | ||
assertThat(query.document.operation.fields, hasSize(equalTo(1))) | ||
} | ||
it("should have mutation named registerUser") { | ||
assertThat(query.document.operation.fields[0].name, equalTo("registerUser")) | ||
} | ||
it("should have 3 arguments in registerUser mutation") { | ||
assertThat(query.document.operation.fields[0].arguments!!.args.entries, hasSize(equalTo(3))) | ||
} | ||
it("should have argument in registerUser mutation with named email and value as [email protected]") { | ||
assertThat(query.document.operation.fields[0].arguments!!.args["email"] as String, equalTo("[email protected]")) | ||
} | ||
it("should have argument in registerUser mutation with named password and value as abcd1234") { | ||
assertThat(query.document.operation.fields[0].arguments!!.args["password"] as String, equalTo("abcd1234")) | ||
} | ||
it("should have argument in registerUser mutation with named age and value as 30") { | ||
assertThat(query.document.operation.fields[0].arguments!!.args["age"] as Int, equalTo(30)) | ||
} | ||
it("should contains 2 field in registerUser payload") { | ||
assertThat(query.document.operation.fields[0].selectionSet!!.fields , hasSize(equalTo(2))) | ||
} | ||
it("should have id field in registerUser payload") { | ||
assertThat(query.document.operation.fields[0].selectionSet!!.fields[0].name , equalTo("id")) | ||
} | ||
it("should have token field in mutation payload") { | ||
assertThat(query.document.operation.fields[0].selectionSet!!.fields[1].name , equalTo("token")) | ||
} | ||
} | ||
} | ||
}) |
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 |
---|---|---|
|
@@ -12,21 +12,29 @@ import org.junit.runner.RunWith | |
|
||
@RunWith(JUnitPlatform::class) | ||
class NodePrintSpek : Spek({ | ||
describe("ArgumentNode print()") { | ||
given("\"id\" as argument and value as 1") { | ||
val node = ArgumentsNode(mapOf("id" to 1)) | ||
describe("ArgumentNode print function") { | ||
given("id as argument and value as 1") { | ||
val node = ArgumentNode(mapOf("id" to 1)) | ||
it("should print (id: 1)") { | ||
assertThat(node.print(), equalTo("(id: 1)")) | ||
} | ||
} | ||
given("\"id\" and \"title\" as arguments and value as 1 and \"KraphQL\"") { | ||
val node = ArgumentsNode(mapOf("id" to 1, "title" to "KraphQL")) | ||
given("id and title as arguments and value as 1 and \"KraphQL\"") { | ||
val node = ArgumentNode(mapOf("id" to 1, "title" to "KraphQL")) | ||
it("should print (id: 1, title: \"KraphQL\")") { | ||
assertThat(node.print(), equalTo("(id: 1, title: \"KraphQL\")")) | ||
} | ||
} | ||
} | ||
describe("SelectionSetNode print()") { | ||
describe("MutationArgumentNode print function") { | ||
given("id as argument and value as 1") { | ||
val node = MutationArgumentNode(mapOf("id" to 1)) | ||
it("should print (input: { id: 1 })") { | ||
assertThat(node.print(), equalTo("(input: { id: 1 })")) | ||
} | ||
} | ||
} | ||
describe("SelectionSetNode print function") { | ||
given("two fields; id and title") { | ||
val fields = listOf(FieldNode("id"), FieldNode("title")) | ||
val node = SelectionSetNode(fields) | ||
|
@@ -44,15 +52,25 @@ class NodePrintSpek : Spek({ | |
} | ||
} | ||
} | ||
describe("FieldNode print()") { | ||
describe("MutationNode print function") { | ||
given("name registerUser with email and password as argument and payload contains id and token") { | ||
val argNode = MutationArgumentNode(mapOf("email" to "[email protected]", "password" to "abcd1234")) | ||
val setNode = SelectionSetNode(listOf(FieldNode("id"), FieldNode("token"))) | ||
val node = MutationNode("registerUser", argNode, setNode) | ||
it("should print registerUser(input: {email: \"[email protected]\", password: \"abcd1234\"}){ id token }") { | ||
assertThat(node.print(), equalTo("registerUser(input: { email: \"[email protected]\", password: \"abcd1234\" }) {\r\nid\r\ntoken\r\n}")) | ||
} | ||
} | ||
} | ||
describe("FieldNode print function") { | ||
given("name id") { | ||
val node = FieldNode("id") | ||
it("should print id") { | ||
assertThat(node.print(), equalTo("id")) | ||
} | ||
} | ||
given("name avatarSize and size argument with value as 20") { | ||
val argNode = ArgumentsNode(mapOf("size" to 20)) | ||
val argNode = ArgumentNode(mapOf("size" to 20)) | ||
val node = FieldNode("avatarSize", arguments = argNode) | ||
it("should print avatarSize(size: 20)") { | ||
assertThat(node.print(), equalTo("avatarSize(size: 20)")) | ||
|
@@ -65,8 +83,16 @@ class NodePrintSpek : Spek({ | |
assertThat(node.print(), equalTo("assignee {\r\nname\r\nemail\r\n}")) | ||
} | ||
} | ||
given("name user and id argument with value as 10 and contains name and email") { | ||
val argNode = ArgumentNode(mapOf("id" to 10)) | ||
val setNode = SelectionSetNode(listOf(FieldNode("name"), FieldNode("email"))) | ||
val node = FieldNode("user", argNode, setNode) | ||
it("should print user(id: 10){ name email }") { | ||
assertThat(node.print(), equalTo("user(id: 10) {\r\nname\r\nemail\r\n}")) | ||
} | ||
} | ||
} | ||
describe("OperationNode print()") { | ||
describe("OperationNode print function") { | ||
given("query type and field named id") { | ||
val node = OperationNode(OperationType.QUERY, listOf(FieldNode("id"))) | ||
it("should print query { id }") { | ||
|
@@ -80,14 +106,14 @@ class NodePrintSpek : Spek({ | |
} | ||
} | ||
given("query type with name \"getTask\" and id(1234) as argument and field title") { | ||
val argNode = ArgumentsNode(mapOf("id" to 1234)) | ||
val argNode = ArgumentNode(mapOf("id" to 1234)) | ||
val node = OperationNode(OperationType.QUERY, name = "getTask", arguments = argNode, fields = listOf(FieldNode("title"))) | ||
it("should print query getTask(id: 1234) { title }") { | ||
assertThat(node.print(), equalTo("query getTask(id: 1234) {\r\ntitle\r\n}")) | ||
} | ||
} | ||
} | ||
describe("DocumentNode print()") { | ||
describe("DocumentNode print function") { | ||
given("document with simple query") { | ||
val queryNode = OperationNode(OperationType.QUERY, fields = listOf(FieldNode("id"))) | ||
val node = DocumentNode(queryNode) | ||
|