|
16 | 16 |
|
17 | 17 | package com.expediagroup.graphql.spring.model |
18 | 18 |
|
| 19 | +import com.expediagroup.graphql.spring.exception.SimpleKotlinGraphQLError |
19 | 20 | import graphql.ExecutionResult |
| 21 | +import graphql.execution.ExecutionPath |
| 22 | +import graphql.execution.NonNullableValueCoercedAsNullException |
| 23 | +import graphql.language.SourceLocation |
| 24 | +import graphql.language.VariableDefinition |
| 25 | +import graphql.schema.GraphQLTypeReference |
20 | 26 | import io.mockk.every |
21 | 27 | import io.mockk.mockk |
22 | 28 | import org.junit.jupiter.api.Test |
@@ -91,4 +97,57 @@ class GraphQLResponseKtTest { |
91 | 97 | assertNotNull(extensions) |
92 | 98 | assertEquals(expected = "bar", actual = extensions["foo"]) |
93 | 99 | } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun `SimpleKotlinGraphQLError is mapped as expected`() { |
| 103 | + val executionResult: ExecutionResult = mockk { |
| 104 | + every { getData<Any>() } returns mockk() |
| 105 | + every { errors } returns listOf(SimpleKotlinGraphQLError( |
| 106 | + Exception(), |
| 107 | + listOf(SourceLocation(1, 1)), |
| 108 | + ExecutionPath.rootPath().toList() |
| 109 | + )) |
| 110 | + every { extensions } returns mapOf("foo" to "bar") |
| 111 | + } |
| 112 | + |
| 113 | + val result = executionResult.toGraphQLResponse() |
| 114 | + |
| 115 | + assertNotNull(result.data) |
| 116 | + val errors = result.errors |
| 117 | + assertNotNull(errors) |
| 118 | + assertEquals(1, errors.size) |
| 119 | + assert(errors.first() is SimpleKotlinGraphQLError) |
| 120 | + val extensions = result.extensions |
| 121 | + assertNotNull(extensions) |
| 122 | + assertEquals(expected = "bar", actual = extensions["foo"]) |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + fun `error due to an Exception is mapped as expected`() { |
| 127 | + val executionResult: ExecutionResult = mockk { |
| 128 | + every { getData<Any>() } returns mockk() |
| 129 | + every { errors } returns listOf(NonNullableValueCoercedAsNullException( |
| 130 | + VariableDefinition( |
| 131 | + "name", |
| 132 | + mockk() |
| 133 | + ), |
| 134 | + "name", |
| 135 | + GraphQLTypeReference( |
| 136 | + "name" |
| 137 | + ) |
| 138 | + )) |
| 139 | + every { extensions } returns mapOf("foo" to "bar") |
| 140 | + } |
| 141 | + |
| 142 | + val result = executionResult.toGraphQLResponse() |
| 143 | + |
| 144 | + assertNotNull(result.data) |
| 145 | + val errors = result.errors |
| 146 | + assertNotNull(errors) |
| 147 | + assertEquals(1, errors.size) |
| 148 | + assert(errors.first() is SimpleKotlinGraphQLError) |
| 149 | + val extensions = result.extensions |
| 150 | + assertNotNull(extensions) |
| 151 | + assertEquals(expected = "bar", actual = extensions["foo"]) |
| 152 | + } |
94 | 153 | } |
0 commit comments