Skip to content

Commit

Permalink
Rename connection to cursorConnection and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
VerachadW committed Oct 6, 2016
1 parent 703aba2 commit 96d28c6
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 30 deletions.
65 changes: 57 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ Adding Kraph to `build.gradle`

### Guide
If you are not familiar with GraphQL syntax, We recommended to read on this [specification](https://facebook.github.io/graphql/) to have an overview of how to write Graphql. Usually, you should be able to use the query from other tools(e.g. [GraphiQL](https://github.com/graphql/graphiql)) with a few tweaks. First, let's see what Kraph provided for you.

- `query` represents QUERY operation in GraphQL. It can be named by passing String as a parameter. The `query` block can only contains `field` or `fieldObject`.
#### Simple GraphQL
- `query`/`mutation` represents QUERY and MUTATION operation in GraphQL. It can be named by passing String as a parameter.
```
/*
* query GetUsers {
Expand All @@ -64,6 +64,17 @@ If you are not familiar with GraphQL syntax, We recommended to read on this [spe
...
}
}
/*
* mutation updateUserProfile {
* ...
* }
*/
Kraph {
mutation("UpdateUserProfile") {
...
}
}
```
- `field` and `fieldObject` represents FIELD in SELECTION SET. The different is that `fieldObject` allow you to have it owns SELECTION SET, which represent data object in GraphQL. Both of them have a parameter named `args` for arguments in paritcular field.
```
Expand All @@ -86,19 +97,57 @@ If you are not familiar with GraphQL syntax, We recommended to read on this [spe
}
}
```
- `mutation` represents MUTATION operation in GraphQL. The `mutation` block can have only contains `func`.
#### Relay
- `func` represents as FIELD inside MUTATION block that follow [Relay Input Object Mutations](https://facebook.github.io/relay/graphql/mutations.htm) specification.
```
/*
* mutation updateUserProfile {
* ...
* mutation {
* userLogin(input: {email: "[email protected]", password: "abcd1234"}) {
* accessToken
* user {
* id
* email
* }
* }
* }
*/
Kraph {
mutation("UpdateUserProfile") {
...
mutation {
func("userLogin", input = mapOf("email" to "[email protected]", "password" to "abcd1234")) {
field("accessToken")
fieldObject("") {
field("id")
field("email")
}
}
}
}
```
- `cursorConnection` represents as FIELD that follow [Relay Cursor Connections](https://facebook.github.io/relay/graphql/connections.htm) specification
```
/*
* query {
* users(first: 10, after: "user::1234") {
* edges {
* node {
* id
* name
* }
* }
* }
* }
*/
Kraph {
cursorConnection("users", first = 10, after = "user::1234") {
edges {
node {
field("id")
field("name")
}
}
}
}
```
- `func` represents as FIELD inside MUTATION block. The reason we did not used `fieldObject`
### Contributing to Kraph
We use Github issues for tracking bugs and requests. Any feedback and/or PRs is welcome.
6 changes: 3 additions & 3 deletions core/src/main/kotlin/com/taskworld/kraph/Kraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class Kraph(f: Kraph.() -> Unit) {
addField(name, args)
}

fun connection(name: String, first: Int = -1, last: Int = -1,
before: String? = null, after: String? = null,
builder: (CursorSelectionBuilder.() -> Unit)) {
fun cursorConnection(name: String, first: Int = -1, last: Int = -1,
before: String? = null, after: String? = null,
builder: (CursorSelectionBuilder.() -> Unit)) {
val argsMap = linkedMapOf<String, Any>()
if (first != -1) argsMap.put("first", first)
if (last != -1) argsMap.put("last", last)
Expand Down
26 changes: 13 additions & 13 deletions core/src/test/kotlin/com/taskworld/kraph/test/BuilderSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ class BuilderSpek : Spek({
}, throws(noFieldInSelectionSetMessageMatcher("mutation")))
}
}
given("sample query with cursor connection first 10 items after cursor a1") {
given("sample query with cursor cursorConnection first 10 items after cursor a1") {
val query = Kraph {
query {
connection("users", first = 10, after = "a1") {
cursorConnection("users", first = 10, after = "a1") {
edges {
node {
field("id")
Expand All @@ -154,23 +154,23 @@ class BuilderSpek : Spek({
}
}
}
it("should have cursor connection field") {
it("should have cursor cursorConnection field") {
assertThat(query.document.operation.selectionSet.fields[0] is CursorConnection, equalTo(true))
}
it("should have argument first with value 10 for cursor connection") {
it("should have argument first with value 10 for cursor cursorConnection") {
assertThat(query.document.operation.selectionSet.fields[0].arguments!!.args["first"] as Int, equalTo(10))
}
it("should have argument after with value a1 for cursor connection") {
it("should have argument after with value a1 for cursor cursorConnection") {
assertThat(query.document.operation.selectionSet.fields[0].arguments!!.args["after"] as String, equalTo("a1"))
}
it("should have custom field inside edges block") {
assertThat(query.document.operation.selectionSet.fields[0].selectionSet!!.fields[0].selectionSet!!.fields[1].name, equalTo("custom"))
}
}
given("sample query with cursor connection last 10 items before cursor a2 and PageInfo object") {
given("sample query with cursor cursorConnection last 10 items before cursor a2 and PageInfo object") {
val query = Kraph {
query {
connection("users", last = 10, before = "a2") {
cursorConnection("users", last = 10, before = "a2") {
edges {
node {
field("id")
Expand All @@ -183,13 +183,13 @@ class BuilderSpek : Spek({
}
}
}
it("should have cursor connection field") {
it("should have cursor cursorConnection field") {
assertThat(query.document.operation.selectionSet.fields[0] is CursorConnection, equalTo(true))
}
it("should have argument first with value 10 for cursor connection") {
it("should have argument first with value 10 for cursor cursorConnection") {
assertThat(query.document.operation.selectionSet.fields[0].arguments!!.args["last"] as Int, equalTo(10))
}
it("should have argument after with value a1 for cursor connection") {
it("should have argument after with value a1 for cursor cursorConnection") {
assertThat(query.document.operation.selectionSet.fields[0].arguments!!.args["before"] as String, equalTo("a2"))
}
it("should have custom field next to edges block") {
Expand All @@ -202,12 +202,12 @@ class BuilderSpek : Spek({
assertThat(query.document.operation.selectionSet.fields[0].selectionSet!!.fields[1].selectionSet!!.fields[0].name, equalTo("hasNextPage"))
}
}
given("sample query with cursor connection without arguments") {
given("sample query with cursor cursorConnection without arguments") {
it("should throw IllegalArgumentException with message \"There must be at least 1 argument for Cursor Connection\"") {
assertThat({
Kraph {
query {
connection("users") {
cursorConnection("users") {
edges {
node {
field("title")
Expand All @@ -224,7 +224,7 @@ class BuilderSpek : Spek({
assertThat({
Kraph {
query {
connection("users", first = 10) {
cursorConnection("users", first = 10) {
edges {
node {
field("title")
Expand Down
10 changes: 5 additions & 5 deletions core/src/test/kotlin/com/taskworld/kraph/test/RelayPrintSpek.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,39 @@ class RelayPrintSpek : Spek({
}
}
describe("Relay Cursor Connection print function") {
given("cursor connection named notes with title in node object and only 10 items") {
given("cursor cursorConnection named notes with title in node object and only 10 items") {
val selectionSet = SelectionSet(listOf(Edges(SelectionSet(listOf(Field("title"))))))
val argsNode = Argument(mapOf("first" to 10))
val node = CursorConnection("notes", argsNode, selectionSet)
it("should print notes(first: 10) { edges { node { title } } }") {
assertThat(node.print(), equalTo("notes(first: 10) {\\nedges {\\nnode {\\ntitle\\n}\\n}\\n}"))
}
}
given("cursor connection named notes with title in node object and only next 10 items after cursor named 'abcd1234'") {
given("cursor cursorConnection named notes with title in node object and only next 10 items after cursor named 'abcd1234'") {
val selectionSet = SelectionSet(listOf(Edges(SelectionSet(listOf(Field("title"))))))
val argsNode = Argument(mapOf("first" to 10, "after" to "abcd1234"))
val node = CursorConnection("notes", argsNode, selectionSet)
it("should print notes(first: 10, before: \"abcd1234\") { edges { node { title } } }") {
assertThat(node.print(), equalTo("notes(first: 10, after: \\\"abcd1234\\\") {\\nedges {\\nnode {\\ntitle\\n}\\n}\\n}"))
}
}
given("cursor connection named notes with title in node object and only last 10 items") {
given("cursor cursorConnection named notes with title in node object and only last 10 items") {
val selectionSet = SelectionSet(listOf(Edges(SelectionSet(listOf(Field("title"))))))
val argsNode = Argument(mapOf("last" to 10))
val node = CursorConnection("notes", argsNode, selectionSet)
it("should print notes(last: 10) { edges { node { title } } }") {
assertThat(node.print(), equalTo("notes(last: 10) {\\nedges {\\nnode {\\ntitle\\n}\\n}\\n}"))
}
}
given("cursor connection named notes with title in node object and only last 10 items before cursor named 'abcd1234'") {
given("cursor cursorConnection named notes with title in node object and only last 10 items before cursor named 'abcd1234'") {
val selectionSet = SelectionSet(listOf(Edges(SelectionSet(listOf(Field("title"))))))
val argsNode = Argument(mapOf("last" to 10, "before" to "abcd1234"))
val node = CursorConnection("notes", argsNode, selectionSet)
it("should print notes(last: 10, before: \"abcd1234\") { edges { node { title } } }") {
assertThat(node.print(), equalTo("notes(last: 10, before: \\\"abcd1234\\\") {\\nedges {\\nnode {\\ntitle\\n}\\n}\\n}"))
}
}
given("cursor connection named notes with PageInfo object") {
given("cursor cursorConnection named notes with PageInfo object") {
val pageNode = PageInfo(SelectionSet(listOf(Field("hasNextPage"), Field("hasPreviousPage"))))
val selectionSet = SelectionSet(listOf(Edges(SelectionSet(listOf(Field("title")))), pageNode))
val argsNode = Argument(mapOf("first" to 10))
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/kotlin/StarWarExample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fun main(args: Array<String>) {

val query = Kraph {
query {
connection("allFilms", first = 10) {
cursorConnection("allFilms", first = 10) {
edges {
node {
field("title")
Expand Down

0 comments on commit 96d28c6

Please sign in to comment.