Skip to content

Commit

Permalink
feat: update config settings for subscriptions (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
smyrick authored and dariuszkuc committed Feb 13, 2019
1 parent d0e3cd6 commit 218c997
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ class Application {
}

val schema = toSchema(
queries = queries.toTopLevelObjectDefs(),
mutations = mutations.toTopLevelObjectDefs(),
config = schemaConfig
queries = queries.toTopLevelObjectDefs(),
mutations = mutations.toTopLevelObjectDefs(),
config = schemaConfig
)
logger.info(SchemaPrinter(
SchemaPrinter.Options.defaultOptions()
Expand Down
11 changes: 9 additions & 2 deletions src/main/kotlin/com/expedia/graphql/SchemaGeneratorConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import com.expedia.graphql.hooks.SchemaGeneratorHooks
*/
data class SchemaGeneratorConfig(
val supportedPackages: List<String>,
val topLevelQueryName: String = "Query",
val topLevelMutationName: String = "Mutation",
val topLevelNames: TopLevelNames = TopLevelNames(),
val hooks: SchemaGeneratorHooks = NoopSchemaGeneratorHooks(),
val dataFetcherFactoryProvider: KotlinDataFetcherFactoryProvider = KotlinDataFetcherFactoryProvider(hooks)
)

/**
*
*/
data class TopLevelNames(
val query: String = "Query",
val mutation: String = "Mutation"
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal class MutationTypeBuilder(generator: SchemaGenerator) : TypeBuilder(gen
}

val mutationBuilder = GraphQLObjectType.Builder()
mutationBuilder.name(config.topLevelMutationName)
mutationBuilder.name(config.topLevelNames.mutation)

for (mutation in mutations) {
if (!mutation.kClass.isPublic()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class QueryTypeBuilder(generator: SchemaGenerator) : TypeBuilder(genera
}

val queryBuilder = GraphQLObjectType.Builder()
queryBuilder.name(config.topLevelQueryName)
queryBuilder.name(config.topLevelNames.query)

for (query in queries) {
if (!query.kClass.isPublic()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/com/expedia/graphql/toSchema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ import graphql.schema.GraphQLSchema
/**
* Entry point to generate a graphql schema using reflection on the passed objects.
*
* @param config Schema generation configuration
* @param queries List of [TopLevelObject] to use for GraphQL queries
* @param mutations List of [TopLevelObject] to use for GraphQL mutations
* @param config Schema generation configuration
*
* @return GraphQLSchema from graphql-java
*/
@Throws(GraphQLKotlinException::class)
fun toSchema(
config: SchemaGeneratorConfig,
queries: List<TopLevelObject>,
mutations: List<TopLevelObject> = emptyList(),
config: SchemaGeneratorConfig
mutations: List<TopLevelObject> = emptyList()
): GraphQLSchema {
val generator = SchemaGenerator(config)
return generator.generate(queries, mutations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CustomDataFetcherTests {
@Test
fun `Custom DataFetcher can be used on functions`() {
val config = SchemaGeneratorConfig(supportedPackages = listOf("com.expedia"), dataFetcherFactoryProvider = CustomDataFetcherFactoryProvider())
val schema = toSchema(listOf(TopLevelObject(AnimalQuery())), config = config)
val schema = toSchema(queries = listOf(TopLevelObject(AnimalQuery())), config = config)

val animalType = schema.getObjectType("Animal")
assertEquals("AnimalDetails!", animalType.getFieldDefinition("details").type.deepName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class DataFetchPredicateTests {
fun `A datafetcher execution is stopped if the predicate test is false`() {
val config = getTestSchemaConfigWithHooks(PredicateHooks())
val schema = toSchema(
listOf(TopLevelObject(QueryWithValidations())),
config = config
queries = listOf(TopLevelObject(QueryWithValidations())),
config = config
)
val graphQL = GraphQL.newGraphQL(schema).build()
val result = graphQL.execute("{ greaterThan2Times10(greaterThan2: 1) }")
Expand All @@ -36,8 +36,8 @@ class DataFetchPredicateTests {
fun `A datafetcher execution is stopped if the predicate test is false with complex argument under test`() {
val config = getTestSchemaConfigWithHooks(PredicateHooks())
val schema = toSchema(
listOf(TopLevelObject(QueryWithValidations())),
config = config
queries = listOf(TopLevelObject(QueryWithValidations())),
config = config
)
val graphQL = GraphQL.newGraphQL(schema).build()
val result = graphQL.execute("{ complexPredicate(person: { age: 33, name: \"Alice\"}) }")
Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/com/expedia/graphql/generator/DirectiveTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlin.test.assertTrue
class DirectiveTests {
@Test
fun `SchemaGenerator marks deprecated fields in the return objects`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithDeprecatedFields())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithDeprecatedFields())), config = testSchemaConfig)
val topLevelQuery = schema.getObjectType("Query")
val query = topLevelQuery.getFieldDefinition("deprecatedFieldQuery")
val result = (query.type as? GraphQLNonNull)?.wrappedType as? GraphQLObjectType
Expand All @@ -27,7 +27,7 @@ class DirectiveTests {

@Test
fun `SchemaGenerator marks deprecated queries and documents replacement`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithDeprecatedFields())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithDeprecatedFields())), config = testSchemaConfig)
val topLevelQuery = schema.getObjectType("Query")
val query = topLevelQuery.getFieldDefinition("deprecatedQueryWithReplacement")

Expand All @@ -37,7 +37,7 @@ class DirectiveTests {

@Test
fun `SchemaGenerator marks deprecated queries`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithDeprecatedFields())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithDeprecatedFields())), config = testSchemaConfig)
val topLevelQuery = schema.getObjectType("Query")
val query = topLevelQuery.getFieldDefinition("deprecatedQuery")
assertTrue(query.isDeprecated)
Expand All @@ -46,7 +46,7 @@ class DirectiveTests {

@Test
fun `Default directive names are normalized`() {
val schema = toSchema(listOf(TopLevelObject(QueryObject())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryObject())), config = testSchemaConfig)

val query = schema.queryType.getFieldDefinition("query")
assertNotNull(query)
Expand All @@ -55,7 +55,7 @@ class DirectiveTests {

@Test
fun `Custom directive names are not modified`() {
val schema = toSchema(listOf(TopLevelObject(QueryObject())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryObject())), config = testSchemaConfig)

val directive = assertNotNull(
(schema.getType("Location") as? GraphQLObjectType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class PolymorphicTests {

@Test
fun `Schema generator creates union types from marked up interface`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithUnion())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithUnion())), config = testSchemaConfig)

val graphqlType = schema.getType("BodyPart") as? GraphQLUnionType
assertNotNull(graphqlType)
Expand All @@ -33,7 +33,7 @@ internal class PolymorphicTests {

@Test
fun `SchemaGenerator can expose an interface and its implementations`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithInterface())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithInterface())), config = testSchemaConfig)

val interfaceType = schema.getType("AnInterface")
assertNotNull(interfaceType)
Expand All @@ -47,20 +47,20 @@ internal class PolymorphicTests {
@Test
fun `Interfaces cannot be used as input field types`() {
assertThrows(InvalidInputFieldTypeException::class.java) {
toSchema(listOf(TopLevelObject(QueryWithUnAuthorizedInterfaceArgument())), config = testSchemaConfig)
toSchema(queries = listOf(TopLevelObject(QueryWithUnAuthorizedInterfaceArgument())), config = testSchemaConfig)
}
}

@Test
fun `Union cannot be used as input field types`() {
assertThrows(InvalidInputFieldTypeException::class.java) {
toSchema(listOf(TopLevelObject(QueryWithUnAuthorizedUnionArgument())), config = testSchemaConfig)
toSchema(queries = listOf(TopLevelObject(QueryWithUnAuthorizedUnionArgument())), config = testSchemaConfig)
}
}

@Test
fun `Object types implementing union and interfaces are only created once`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithInterfaceAnUnion())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithInterfaceAnUnion())), config = testSchemaConfig)

val carType = schema.getType("Car") as? GraphQLObjectType
assertNotNull(carType)
Expand All @@ -74,7 +74,7 @@ internal class PolymorphicTests {

@Test
fun `Interfaces can declare properties of their own type`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithRecursiveType())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithRecursiveType())), config = testSchemaConfig)

val personType = schema.getType("Person")
assertNotNull(personType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,31 @@ class SchemaGeneratorAsyncTests {

@Test
fun `SchemaGenerator strips type argument from CompletableFuture to support async servlet`() {
val schema = toSchema(listOf(TopLevelObject(AsyncQuery())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(AsyncQuery())), config = testSchemaConfig)
val returnTypeName =
(schema.getObjectType("Query").getFieldDefinition("asynchronouslyDo").type as? GraphQLNonNull)?.wrappedType?.name
assertEquals("Int", returnTypeName)
}

@Test
fun `SchemaGenerator strips type argument from RxJava2 Observable`() {
val schema = toSchema(listOf(TopLevelObject(RxJava2Query())), config = configWithRxJavaMonads)
val schema = toSchema(queries = listOf(TopLevelObject(RxJava2Query())), config = configWithRxJavaMonads)
val returnTypeName =
(schema.getObjectType("Query").getFieldDefinition("asynchronouslyDo").type as? GraphQLNonNull)?.wrappedType?.name
assertEquals("Int", returnTypeName)
}

@Test
fun `SchemaGenerator strips type argument from RxJava2 Single`() {
val schema = toSchema(listOf(TopLevelObject(RxJava2Query())), config = configWithRxJavaMonads)
val schema = toSchema(queries = listOf(TopLevelObject(RxJava2Query())), config = configWithRxJavaMonads)
val returnTypeName =
(schema.getObjectType("Query").getFieldDefinition("asynchronouslyDoSingle").type as? GraphQLNonNull)?.wrappedType?.name
assertEquals("Int", returnTypeName)
}

@Test
fun `SchemaGenerator strips type argument from RxJava2 Maybe`() {
val schema = toSchema(listOf(TopLevelObject(RxJava2Query())), config = configWithRxJavaMonads)
val schema = toSchema(queries = listOf(TopLevelObject(RxJava2Query())), config = configWithRxJavaMonads)
val returnTypeName =
(schema.getObjectType("Query").getFieldDefinition("maybe").type as? GraphQLNonNull)?.wrappedType?.name
assertEquals("Int", returnTypeName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class SchemaGeneratorTest {
@Test
fun `SchemaGenerator generates a simple GraphQL schema`() {
val schema = toSchema(
listOf(TopLevelObject(QueryObject())),
listOf(TopLevelObject(MutationObject())),
queries = listOf(TopLevelObject(QueryObject())),
mutations = listOf(TopLevelObject(MutationObject())),
config = testSchemaConfig
)
val graphQL = GraphQL.newGraphQL(schema).build()
Expand All @@ -45,7 +45,7 @@ class SchemaGeneratorTest {

@Test
fun `Schema generator exposes arrays of primitive types as function arguments`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithArray())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithArray())), config = testSchemaConfig)
val firstArgumentType = schema.queryType.getFieldDefinition("sumOf").arguments[0].type.deepName
assertEquals("[Int!]!", firstArgumentType)

Expand All @@ -58,7 +58,7 @@ class SchemaGeneratorTest {

@Test
fun `Schema generator exposes arrays of complex types as function arguments`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithArray())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithArray())), config = testSchemaConfig)
val firstArgumentType = schema.queryType.getFieldDefinition("sumOfComplexArray").arguments[0].type.deepName
assertEquals("[ComplexWrappingTypeInput!]!", firstArgumentType)

Expand All @@ -71,7 +71,7 @@ class SchemaGeneratorTest {

@Test
fun `SchemaGenerator ignores fields and functions with @Ignore`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithIgnored())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithIgnored())), config = testSchemaConfig)

assertTrue(schema.queryType.fieldDefinitions.none {
it.name == "ignoredFunction"
Expand All @@ -89,7 +89,7 @@ class SchemaGeneratorTest {

@Test
fun `SchemaGenerator generates a GraphQL schema with repeated types to test conflicts`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithRepeatedTypes())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithRepeatedTypes())), config = testSchemaConfig)
val resultType = schema.getObjectType("Result")
val topLevelQuery = schema.getObjectType("Query")
assertEquals("Result!", topLevelQuery.getFieldDefinition("query").type.deepName)
Expand All @@ -102,7 +102,7 @@ class SchemaGeneratorTest {

@Test
fun `SchemaGenerator generates a GraphQL schema with mixed nullity`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithNullableAndNonNullTypes())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithNullableAndNonNullTypes())), config = testSchemaConfig)
val resultType = schema.getObjectType("MixedNullityResult")
val topLevelQuery = schema.getObjectType("Query")
assertEquals("MixedNullityResult!", topLevelQuery.getFieldDefinition("query").type.deepName)
Expand All @@ -112,7 +112,7 @@ class SchemaGeneratorTest {

@Test
fun `SchemaGenerator generates a GraphQL schema where the input types differ from the output types`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithInputObject())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithInputObject())), config = testSchemaConfig)
val topLevelQuery = schema.getObjectType("Query")
assertEquals(
"SomeObjectInput!",
Expand All @@ -123,7 +123,7 @@ class SchemaGeneratorTest {

@Test
fun `SchemaGenerator generates a GraphQL schema where the input and output enum is the same`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithInputEnum())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithInputEnum())), config = testSchemaConfig)
val topLevelQuery = schema.getObjectType("Query")
assertEquals("SomeEnum!", topLevelQuery.getFieldDefinition("query").getArgument("someEnum").type.deepName)
assertEquals("SomeEnum!", topLevelQuery.getFieldDefinition("query").type.deepName)
Expand All @@ -132,8 +132,8 @@ class SchemaGeneratorTest {
@Test
fun `SchemaGenerator documents types annotated with @Description`() {
val schema = toSchema(
listOf(TopLevelObject(QueryObject())),
listOf(TopLevelObject(MutationObject())),
queries = listOf(TopLevelObject(QueryObject())),
mutations = listOf(TopLevelObject(MutationObject())),
config = testSchemaConfig
)
val geo = schema.getObjectType("Geography")
Expand All @@ -143,8 +143,8 @@ class SchemaGeneratorTest {
@Test
fun `SchemaGenerator documents arguments annotated with @Description`() {
val schema = toSchema(
listOf(TopLevelObject(QueryObject())),
listOf(TopLevelObject(MutationObject())),
queries = listOf(TopLevelObject(QueryObject())),
mutations = listOf(TopLevelObject(MutationObject())),
config = testSchemaConfig
)
val documentation = schema.queryType.fieldDefinitions.first().arguments.first().description
Expand All @@ -154,8 +154,8 @@ class SchemaGeneratorTest {
@Test
fun `SchemaGenerator documents properties annotated with @Description`() {
val schema = toSchema(
listOf(TopLevelObject(QueryObject())),
listOf(TopLevelObject(MutationObject())),
queries = listOf(TopLevelObject(QueryObject())),
mutations = listOf(TopLevelObject(MutationObject())),
config = testSchemaConfig
)
val documentation = schema.queryType.fieldDefinitions.first().description
Expand All @@ -164,7 +164,7 @@ class SchemaGeneratorTest {

@Test
fun `SchemaGenerator can expose functions on result classes`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithDataThatContainsFunction())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithDataThatContainsFunction())), config = testSchemaConfig)
val resultWithFunction = schema.getObjectType("ResultWithFunction")
val repeatFieldDefinition = resultWithFunction.getFieldDefinition("repeat")
assertEquals("repeat", repeatFieldDefinition.name)
Expand All @@ -175,7 +175,7 @@ class SchemaGeneratorTest {

@Test
fun `SchemaGenerator can execute functions on result classes`() {
val schema = toSchema(listOf(TopLevelObject(QueryWithDataThatContainsFunction())), config = testSchemaConfig)
val schema = toSchema(queries = listOf(TopLevelObject(QueryWithDataThatContainsFunction())), config = testSchemaConfig)
val graphQL = GraphQL.newGraphQL(schema).build()
val result = graphQL.execute("{ query(something: \"thing\") { repeat(n: 3) } }")
val data: Map<String, Map<String, Any>> = result.getData()
Expand All @@ -186,7 +186,7 @@ class SchemaGeneratorTest {
@Test
fun `SchemaGenerator ignores private fields`() {
val schema =
toSchema(listOf(TopLevelObject(QueryWithPrivateParts())), config = testSchemaConfig)
toSchema(queries = listOf(TopLevelObject(QueryWithPrivateParts())), config = testSchemaConfig)
val topLevelQuery = schema.getObjectType("Query")
val query = topLevelQuery.getFieldDefinition("query")
val resultWithPrivateParts = query.type as? GraphQLObjectType
Expand All @@ -199,14 +199,14 @@ class SchemaGeneratorTest {
@Test
fun `SchemaGenerator throws when encountering java stdlib`() {
assertFailsWith(GraphQLKotlinException::class) {
toSchema(listOf(TopLevelObject(QueryWithJavaClass())), config = testSchemaConfig)
toSchema(queries = listOf(TopLevelObject(QueryWithJavaClass())), config = testSchemaConfig)
}
}

@Test
fun `SchemaGenerator throws when encountering list of java stdlib`() {
assertFailsWith(GraphQLKotlinException::class) {
toSchema(listOf(TopLevelObject(QueryWithListOfJavaClass())), config = testSchemaConfig)
toSchema(queries = listOf(TopLevelObject(QueryWithListOfJavaClass())), config = testSchemaConfig)
}
}

Expand Down
Loading

0 comments on commit 218c997

Please sign in to comment.