-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update Junit dep to Junit5 * Add support for nested queries via custom datafetchers * Add example of custom datafetchers via spring beans * Mark lateinit field as nullable
- Loading branch information
Showing
13 changed files
with
215 additions
and
29 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
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
26 changes: 26 additions & 0 deletions
26
example/src/main/kotlin/com.expedia.graphql.sample/dataFetchers/SpringDataFetcherFactory.kt
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.expedia.graphql.sample.dataFetchers | ||
|
||
import com.expedia.graphql.schema.extensions.deepName | ||
import graphql.schema.DataFetcher | ||
import graphql.schema.DataFetcherFactory | ||
import graphql.schema.DataFetcherFactoryEnvironment | ||
import org.springframework.beans.factory.BeanFactory | ||
import org.springframework.beans.factory.BeanFactoryAware | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class SpringDataFetcherFactory: DataFetcherFactory<Any>, BeanFactoryAware { | ||
private lateinit var beanFactory: BeanFactory | ||
|
||
override fun setBeanFactory(beanFactory: BeanFactory?) { | ||
this.beanFactory = beanFactory!! | ||
} | ||
|
||
override fun get(environment: DataFetcherFactoryEnvironment?): DataFetcher<Any> { | ||
|
||
//Strip out possible `Input` and `!` suffixes added to by the SchemaGenerator | ||
val targetedTypeName = environment?.fieldDefinition?.type?.deepName?.removeSuffix("!")?.removeSuffix("Input") | ||
return beanFactory.getBean("${targetedTypeName}DataFetcher") as DataFetcher<Any> | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
example/src/main/kotlin/com.expedia.graphql.sample/query/NestedQueries.kt
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.expedia.graphql.sample.query | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore | ||
import graphql.schema.DataFetcher | ||
import graphql.schema.DataFetchingEnvironment | ||
import org.springframework.beans.factory.BeanFactory | ||
import org.springframework.beans.factory.BeanFactoryAware | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.beans.factory.getBean | ||
import org.springframework.context.annotation.Scope | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class NestedQueries : Query { | ||
fun findAnimal(context: String): NestedAnimal = NestedAnimal(1, "cat") | ||
} | ||
|
||
data class NestedAnimal( | ||
val id: Int, | ||
val type: String | ||
) { | ||
@JsonIgnore | ||
lateinit var details: NestedAnimalDetails | ||
} | ||
|
||
@Component | ||
@Scope("prototype") | ||
data class NestedAnimalDetails @Autowired(required = false) constructor(private val animalId: Int) { | ||
fun veryDetailledFunction(): String = "Details($animalId)" | ||
} | ||
|
||
@Component("NestedAnimalDetailsDataFetcher") | ||
@Scope("prototype") | ||
class AnimalDetailsDataFetcher : DataFetcher<NestedAnimalDetails>, BeanFactoryAware { | ||
|
||
private lateinit var beanFactory: BeanFactory | ||
|
||
override fun setBeanFactory(beanFactory: BeanFactory) { | ||
this.beanFactory = beanFactory | ||
} | ||
|
||
override fun get(environment: DataFetchingEnvironment?): NestedAnimalDetails { | ||
val id = environment?.getSource<NestedAnimal>()?.id | ||
if (id == null) { | ||
throw Exception("Cannot retrieve animal details, the id is null") | ||
} else { | ||
return beanFactory.getBean(id) | ||
} | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/test/kotlin/com/expedia/graphql/schema/dataFetchers/CustomDataFetcherTests.kt
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.expedia.graphql.schema.dataFetchers | ||
|
||
import com.expedia.graphql.TopLevelObjectDef | ||
import com.expedia.graphql.schema.SchemaGeneratorConfig | ||
import com.expedia.graphql.schema.extensions.deepName | ||
import com.expedia.graphql.toSchema | ||
import graphql.GraphQL | ||
import graphql.schema.DataFetcher | ||
import graphql.schema.DataFetcherFactory | ||
import graphql.schema.DataFetcherFactoryEnvironment | ||
import graphql.schema.DataFetchingEnvironment | ||
import org.junit.jupiter.api.Test | ||
import kotlin.test.assertEquals | ||
|
||
class CustomDataFetcherTests { | ||
@Test | ||
fun `Custom DataFetcher can be used on functions`() { | ||
val config = SchemaGeneratorConfig(supportedPackages = "com.expedia", dataFetcherFactory = PetDataFetcherFactory()) | ||
val schema = toSchema(listOf(TopLevelObjectDef(AnimalQuery())), config = config) | ||
|
||
val animalType = schema.getObjectType("Animal") | ||
assertEquals("AnimalDetails", animalType.getFieldDefinition("details").type.deepName) | ||
|
||
val graphQL = GraphQL.newGraphQL(schema).build() | ||
val execute = graphQL.execute("{ findAnimal { id type details { specialId } } }") | ||
|
||
val data = execute.getData<Map<String, Any>>()["findAnimal"] as? Map<*, *> | ||
assertEquals(1, data?.get("id")) | ||
assertEquals("cat", data?.get("type")) | ||
|
||
val details = data?.get("details") as? Map<*, *> | ||
assertEquals(11, details?.get("specialId")) | ||
} | ||
} | ||
|
||
class AnimalQuery { | ||
fun findAnimal(): Animal = Animal(1, "cat") | ||
} | ||
|
||
data class Animal( | ||
val id: Int, | ||
val type: String | ||
) { | ||
lateinit var details: AnimalDetails | ||
} | ||
|
||
data class AnimalDetails(val specialId: Int) | ||
|
||
class PetDataFetcherFactory : DataFetcherFactory<Any> { | ||
override fun get(environment: DataFetcherFactoryEnvironment?): DataFetcher<Any> = AnimalDetailsDataFetcher() | ||
} | ||
|
||
class AnimalDetailsDataFetcher : DataFetcher<Any> { | ||
|
||
override fun get(environment: DataFetchingEnvironment?): AnimalDetails { | ||
val animal = environment?.getSource<Animal>() | ||
val specialId = animal?.id?.plus(10) ?: 0 | ||
return animal.let { AnimalDetails(specialId) } | ||
} | ||
} |
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
2 changes: 1 addition & 1 deletion
2
src/test/kotlin/com/expedia/graphql/schema/generator/KClassExtensionsTest.kt
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
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