Instantly share code, notes, and snippets.

View SangriaMutationExample.scala
import sangria.schema._
import sangria.execution._
import sangria.macros._
import sangria.macros.derive._
import sangria.marshalling.circe._
import scala.concurrent.ExecutionContext.Implicits.global
import io.circe.generic.auto._
// Some basic data structures
View SangriaReproExample.scala
// Imports that you are using
import sangria.schema._
import sangria.execution._
import sangria.macros._
import sangria.marshalling.circe._
import scala.concurrent.ExecutionContext.Implicits.global
// The schema definition
View DiscountCodeUpdateAction.scala
sealed abstract class DiscountCodeUpdateAction
case class SetDiscountCodeName(name: Option[LocalizedString])
extends DiscountCodeUpdateAction
case class SetDiscountCodeDescription(description: Option[LocalizedString])
extends DiscountCodeUpdateAction
case class SetDiscountCodeCartPredicate(cartPredicate: Option[String])
extends DiscountCodeUpdateAction
View DiscountCodeUpdateExample.graphql
mutation UpdateDC {
updateDiscountCode(
id: "123"
version: 5
actions: [
{setName: {name: {locale: "en", value: "10% off"}}}
{changeIsActive: {isActive: false}}
]
) {
id
View DiscountCodeUpdateActions.graphql
input DiscountCodeUpdateAction {
setName: SetDiscountCodeName
setDescription: SetDiscountCodeDescription
setCartPredicate: SetDiscountCodeCartPredicate
setCustomType: SetDiscountCodeCustomType
setCustomField: SetDiscountCodeCustomField
# ...
}
View DiscountCodeMutations.graphql
extend type Mutation {
createDiscountCode(draft: DiscountCodeDraft!): DiscountCode
updateDiscountCode(
id: String!,
version: Long!,
actions: [DiscountCodeUpdateAction!]!): DiscountCode
deleteDiscountCode(id: String!, version: Long!): DiscountCode
}
View CovertJson.scala
import sangria.marshalling.MarshallingUtil._
import sangria.marshalling.circe._
val marchaller: ResultMarshaller = ??? // provided in ExceptionHandler
val json: Json = ??? // your custom json
implicit val mForType = SimpleResultMarshallerForType[marchaller.Node](marchaller)
val converted: marchaller.Node = json.convertMarshaled[marchaller.Node]
View app-graphiql.js
$(function (global) {
/**
* This GraphiQL example illustrates how to use some of GraphiQL's props
* in order to enable reading and updating the URL parameters, making
* link sharing of queries a little bit easier.
*
* This is only one example of this kind of feature, GraphiQL exposes
* various React params to enable interesting integrations.
*/
var graphiql = null
View CirceJsonPath.scala
// For build.sbt:
//
// libraryDependencies ++= Seq(
// "io.circe" %% "circe-core" % "0.9.2",
// "io.circe" %% "circe-parser" % "0.9.2",
// "com.jayway.jsonpath" % "json-path" % "2.3.0")
import com.jayway.jsonpath.{InvalidJsonException, JsonPathException, Configuration, JsonPath, TypeRef}
import com.jayway.jsonpath.spi.json.JsonProvider
import com.jayway.jsonpath.spi.mapper.MappingProvider
View renderSchemaWithLegacyDescriptions.scala
import sangria.ast
import sangria.ast.AstVisitor
import sangria.schema._
import sangria.renderer.{QueryRenderer, QueryRendererConfig, SchemaFilter}
import sangria.visitor.VisitorCommand
def renderSchemaWithLegacyDescriptions(schema: Schema[_, _], filter: SchemaFilter = SchemaFilter.withoutSangriaBuiltIn, config: QueryRendererConfig = QueryRenderer.Pretty) = {
def commentDescription(node: ast.WithDescription) =
node.description.toVector.flatMap(sv sv.value.split("\\r?\\n").toVector.map(ast.Comment(_)))