Skip to content

Commit

Permalink
Enable post requests for introspection (#41)
Browse files Browse the repository at this point in the history
* Enable post requests for introspection

* Scalafmt
  • Loading branch information
muuki88 authored Jul 26, 2018
1 parent ee719a9 commit 4d96cfb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ graphqlProductionSchema := GraphQLSchemaLoader
.fromIntrospection("http://prod.your-graphql.net/graphql", streams.value.log)
.withHeaders("X-Api-Version" -> "1", "X-Api-Key" -> "4198ab84-e992-42b0-8742-225ed15a781e")
.loadSchema()

// from a graphql endpoint via introspection with post request
graphqlProductionSchema := GraphQLSchemaLoader
.fromIntrospection("http://prod.your-graphql.net/graphql", streams.value.log)
.withPost()
.loadSchema()
```


Expand Down
46 changes: 39 additions & 7 deletions src/main/scala/rocks/muki/graphql/schema/SchemaLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class FileSchemaLoader(file: File) extends SchemaLoader {
*/
case class IntrospectSchemaLoader(url: String,
log: Logger,
headers: Seq[(String, String)] = Seq.empty)
headers: Seq[(String, String)] = Seq.empty,
method: IntrospectSchemaLoader.Method =
IntrospectSchemaLoader.GET)
extends SchemaLoader {

override def loadSchema(): Schema[Any, Any] =
Expand All @@ -86,22 +88,52 @@ case class IntrospectSchemaLoader(url: String,
copy(headers = headers.toList)
}

/**
* @return a new schema loader that uses a POST requests instead of a get request
*/
def withPost(): IntrospectSchemaLoader = {
copy(method = IntrospectSchemaLoader.POST)
}

/**
* @see https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js
* @return the introspect query result
*/
private def introspect(): Json = {
log.info(s"Introspect graphql endpoint: $url")
val response = Http(url)
.headers(headers)
.param("query", introspectionQuery.renderCompact)
.asString
log.info(s"Introspect graphql endpoint: ${method.name} : $url")

val response = method match {
case IntrospectSchemaLoader.POST =>
val body = Json
.obj("query" -> Json.fromString(introspectionQuery.renderCompact))
.noSpaces
Http(url).headers(headers).method("POST").postData(body).asString
case IntrospectSchemaLoader.GET =>
Http(url)
.headers(headers)
.param("query", introspectionQuery.renderCompact)
.asString
}

parse(response.body) match {
case Right(json) => json
case Left(error) =>
log.error("JSON parse errors:")
log.error(error.message)
sys.error(s"Invalid JSON was returned from graphql endpoint $url")
log.error("Body received")
log.error(response.body)
sys.error(
s"Invalid JSON was returned from graphql endpoint ${method.name} : $url")
}
}
}

object IntrospectSchemaLoader {

/**
* http method for introspection query
*/
sealed abstract class Method(val name: String)
case object POST extends Method("POST")
case object GET extends Method("GET")
}

0 comments on commit 4d96cfb

Please sign in to comment.