Skip to content

Instantly share code, notes, and snippets.

View OlegIlyenko's full-sized avatar

ΘLΞG OlegIlyenko

View GitHub Profile
@OlegIlyenko
OlegIlyenko / SangriaMutationExample.scala
Last active October 25, 2019 03:58
Simple example of a mutation with complex input type argument
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
@OlegIlyenko
OlegIlyenko / SangriaReproExample.scala
Last active October 25, 2019 03:59
An example of minimal self-contained snippet that reproduces specific issue
// 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
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
mutation UpdateDC {
updateDiscountCode(
id: "123"
version: 5
actions: [
{setName: {name: {locale: "en", value: "10% off"}}}
{changeIsActive: {isActive: false}}
]
) {
id
input DiscountCodeUpdateAction {
setName: SetDiscountCodeName
setDescription: SetDiscountCodeDescription
setCartPredicate: SetDiscountCodeCartPredicate
setCustomType: SetDiscountCodeCustomType
setCustomField: SetDiscountCodeCustomField
# ...
}
extend type Mutation {
createDiscountCode(draft: DiscountCodeDraft!): DiscountCode
updateDiscountCode(
id: String!,
version: Long!,
actions: [DiscountCodeUpdateAction!]!): DiscountCode
deleteDiscountCode(id: String!, version: Long!): DiscountCode
}
@OlegIlyenko
OlegIlyenko / CovertJson.scala
Created April 19, 2018 19:50
useful for exception handlers
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]
$(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
@OlegIlyenko
OlegIlyenko / CirceJsonPath.scala
Last active March 20, 2018 00:29
Stefan Goessner JsonPath (http://goessner.net/articles/JsonPath/) implementation for Circe
// 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
@OlegIlyenko
OlegIlyenko / renderSchemaWithLegacyDescriptions.scala
Last active December 19, 2017 17:01
Function to render the schema with legacy comment-based descriptions
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(_)))