Skip to content

Instantly share code, notes, and snippets.

@BjRo
BjRo / preview-fields.scala
Created March 13, 2018 17:01
Preview fields using FieldTags in Sangria
class PreviewSchemaFilter extends DefaultAstSchemaBuilder[RequestContext] {
override def extendObjectType(origin: MatOrigin,
existing: ObjectType[RequestContext, _],
extensions: List[TypeExtensionDefinition],
fields: () => List[Field[RequestContext, Any]],
interfaces: List[InterfaceType[RequestContext, Any]],
mat: AstSchemaMaterializer[RequestContext]): ObjectType[RequestContext, Any] = {
existing.asInstanceOf[ObjectType[RequestContext, Any]].copy(fieldsFn = () => fields().filterNot(_.tags.contains(Preview)))
}
@BjRo
BjRo / capability-testing.scala
Last active January 27, 2018 12:26
GraphQL testing in XING One
package com.xing.one.engine
import akka.http.scaladsl.model._
import com.xing.one.GraphqlEngine
import com.xing.one.helpers.EngineSpecification
import com.xing.one.helpers.Matchers._
class BasicInterfaceSpec extends EngineSpecification {
isolated
@BjRo
BjRo / sangria_error.txt
Created December 4, 2017 09:57
Parser errors after upgrade to 1.3.3
[error] sangria.parser.SyntaxError: Syntax error while parsing GraphQL query. Invalid input ""/test") {\n}", expected ValueConst, ArgumentConst, DirectiveConst or InputValueDefinition (line 7, column 32):
[error] input MyResolver @httpGet(url: "/test") {
[error] ^ (QueryParser.scala:433)
@BjRo
BjRo / xing-one-dsl.scala
Created June 21, 2017 16:15
XING One DSL example
object Address extends ApiDSL {
val t = ApiObjectType(
"Address",
"A standard container for representing an address",
interfaces(Addressable.t),
fields(
optionalApiField("city", StringType),
optionalApiField("province", StringType),
optionalApiField("street", StringType),
optionalApiField("zip", StringType),
@BjRo
BjRo / scenario.graphql
Last active February 22, 2017 18:22
Batching experiments with different server-side GraphQL implementations
fragment profileFragment on ProfileRef {
__typename
... on ActiveProfileRef {
id
firstName
lastName
}
... on InactiveProfileRef {
@BjRo
BjRo / unions.scala
Created February 10, 2017 16:56
union types in sangria
object ProfileRef {
val inactiveDefinition = ObjectType(
"InactiveProfileRef",
"An inactive profile",
fields = fields[UserContext, JsObject](
Field("id", IntType, None, resolve = ctx => (ctx.value \ "id").as[Int])
)
)
val activeDefinition = ObjectType(
val routes =
(post & path("graphql")) {
optionalHeaderValueByName("ACCEPT_LANGUAGE") { accept_language =>
entity(as[JsValue]) { requestJson =>
val context = Context.from(accept_language)
println("ACCEPT_LANGUAGE: " + context.language)
graphQLEndpoint(requestJson, context)
}
}
} ~
@BjRo
BjRo / logjam_zmq.ex
Created August 25, 2016 10:00
Logjam ZMQ info headers
@meta_info_version 1
@meta_info_tag 0xcabd
@meta_info_device_number 0
@compression_method 0
def pack_info(sequence) do
<<@meta_info_tag::big-integer-unsigned-size(16),
@compression_method::big-integer-unsigned-size(8),
@meta_info_version::integer-unsigned-size(8),
@meta_info_device_number::big-integer-unsigned-size(32),
zclock_time::big-integer-unsigned-size(64),
# Move around the buffer in vim style
setw -g mode-keys vi
# Shortcuts
bind r source-file ~/.tmux.conf\; display "Reloaded!" #reload configuration
bind | split-window -h -c '#{pane_current_path}' #split window horizontally
bind - split-window -v -c '#{pane_current_path}' #split window vertically
bind h select-pane -L #vim style pane navigation
bind j select-pane -D #vim style pane navigation
bind k select-pane -U #vim style pane navigation
@BjRo
BjRo / Messages.Time.ex
Created April 22, 2016 14:47
Dialyzer is our friend again :-)
defmodule Messages.Time do
use Timex
alias Timex.DateTime
@spec timestamp() :: DateTime.t
def timestamp, do: DateTime.now(:utc)
@spec timestamp_iso8601() :: String.t
@dialyzer {:nowarn_function, timestamp_iso8601: 0}
def timestamp_iso8601 do