Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

omit invalid naptime resource schemas in graphql schema builder #286

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import sangria.schema.Value
import sangria.schema.Field
import sangria.schema.ObjectType

import scala.util.Failure
import scala.util.Success
import scala.util.Try

class SangriaGraphQlSchemaBuilder(resources: Set[Resource], schemas: Map[String, RecordDataSchema])
extends StrictLogging {

Expand Down Expand Up @@ -66,10 +70,26 @@ class SangriaGraphQlSchemaBuilder(resources: Set[Resource], schemas: Map[String,
val schemaErrors = topLevelResourceObjectsAndErrors.foldLeft(SchemaErrors.empty)(_ ++ _.errors)

val dedupedResources = topLevelResourceObjects.groupBy(_.name).map(_._2.head).toList

// first, try to individually generate graphql schemas from each resource, and discard

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should have references to our internal tools in the OSS repo.

Should this be an option? We should maintain compatibility with existing integrations. Alternatively, we could potentially have a 2nd implementation to provide this functionality.

// invalid schemas. this is done so that assembler can start even if some upstreams expose
// invalid schemas.
val validResources = dedupedResources
.filter { resource =>
val resourceRootObject =
ObjectType[SangriaGraphQlContext, DataMap](name = "root", fields = List(resource))
Try(Schema(resourceRootObject)) match {
case Success(_) => true
case Failure(exception) =>
this.logger.error(s"schema validation failed for ${resource.name}", exception)
false
}
}

val rootObject = ObjectType[SangriaGraphQlContext, DataMap](
name = "root",
description = "Top-level accessor for Naptime resources",
fields = dedupedResources)
fields = validResources)

WithSchemaErrors(Schema(rootObject), schemaErrors)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.coursera.naptime.ari.graphql

import com.google.inject.Injector
import com.linkedin.data.schema.RecordDataSchema
import org.coursera.naptime.ResourceName
import org.coursera.naptime.ari.FullSchema
import org.coursera.naptime.ari.LocalSchemaProvider
import org.coursera.naptime.ari.SchemaProvider
import org.coursera.naptime.ari.graphql.models.MergedCourse
import org.coursera.naptime.ari.graphql.models.MergedInstructor
import org.coursera.naptime.ari.graphql.models.MergedPartner
import org.coursera.naptime.ari.graphql.models.RecordWithUnionTypes
import org.coursera.naptime.model.Keyed
import org.coursera.naptime.router2.NaptimeRoutes
import org.coursera.naptime.router2.ResourceRouterBuilder
import org.coursera.naptime.schema.Handler
import org.coursera.naptime.schema.HandlerKind
import org.coursera.naptime.schema.Parameter
import org.coursera.naptime.schema.Resource
import org.coursera.naptime.schema.ResourceKind
import org.junit.Test
import org.mockito.Mockito._
import org.scalatest.junit.AssertionsForJUnit
import org.scalatest.mockito.MockitoSugar

class SangriaGraphQlSchemaBuilderTest extends AssertionsForJUnit {
@Test
def badResourceSchemaOmitFromGraphqlSchema(): Unit = {
val schemaTypes = Map(
"org.coursera.naptime.ari.graphql.models.MergedCourse" -> MergedCourse.SCHEMA,
"org.coursera.naptime.ari.graphql.models.FakeModel" -> RecordWithUnionTypes.SCHEMA,
"org.coursera.naptime.ari.graphql.models.MergedPartner" -> MergedPartner.SCHEMA,
"org.coursera.naptime.ari.graphql.models.MergedInstructor" -> MergedInstructor.SCHEMA
)
val resourcesWithInvalidResource =
Set(
Models.courseResource.copy(version = Some(-1)),
Models.instructorResource,
Models.partnersResource,
Models.fakeModelResource
)
val builder = new SangriaGraphQlSchemaBuilder(resourcesWithInvalidResource, schemaTypes)
val schema = builder.generateSchema().data

val allResources2 =
Set(
Models.instructorResource,
Models.partnersResource,
Models.fakeModelResource
)
val builder2 = new SangriaGraphQlSchemaBuilder(allResources2, schemaTypes)
val schema2 = builder2.generateSchema().data

assertResult(schema2.renderPretty)(schema.renderPretty)
}
}