Skip to content

Instantly share code, notes, and snippets.

@cgiffard
Created November 10, 2017 05:20
Show Gist options
  • Save cgiffard/929f5e122e0599ac000713124b204572 to your computer and use it in GitHub Desktop.
Save cgiffard/929f5e122e0599ac000713124b204572 to your computer and use it in GitHub Desktop.
Circular references
defmodule Bureaucrat.Interfaces.GraphQL.SchemaBuilder do
@moduledoc false
require Logger
def build_schema(owner) do
date = DateTime.to_string(DateTime.utc_now)
schema_description =
"Autogenerated bureaucrat schema. Different every time! " <> date
%Absinthe.Type.Object{
description: schema_description,
fields: get_fields(owner),
identifier: :query,
interfaces: [],
is_type_of: nil,
name: "RootQueryType",
field_imports: []
}
end
def get_fields(:not_request_context) do
Logger.debug "somehow we're not in request context (compile time?)"
Logger.debug "Sending blank schema."
%{}
end
def get_fields(owner) do
%{
"cubicobject" => %Absinthe.Type.Field{
__private__: [],
args: %{},
complexity: nil,
config: nil,
default_value: nil,
deprecation: nil,
description: "This looks like a cube, but you'll have to check the " <>
"number of sides to make sure",
identifier: "cubicobject",
middleware: [
fn(resolution, _config) ->
Absinthe.Resolution.put_result(resolution,
{:ok, %{ "sides" => 4 }}
)
end
],
name: "cubicobject",
triggers: [],
type: :cube
}
}
end
end
defmodule Bureaucrat.Interfaces.GraphQL.Schema.Base do
@moduledoc """
The central GraphQL Schema for the API, this file maps out the root
level types.
"""
use Absinthe.Schema
alias Bureaucrat.Interfaces.GraphQL.Schema
require Logger
require Bureaucrat.Interfaces.GraphQL.SchemaBuilder
defmodule Superset do
@moduledoc """
This module contains a superset schema with resolvers for basic types.
"""
use Absinthe.Schema
query do
end
end
def __absinthe_types__ do
Logger.debug "looks like somebody is trying to introspect our types!"
IO.inspect(
Map.put(Schema.Base.Superset.__absinthe_types__(), :cube, "cube")
)
end
def __absinthe_type__(:query) do
Logger.debug "looks like somebody is trying to get our root query type"
Logger.debug "Our pid is: #{inspect self}"
Bureaucrat.Interfaces.GraphQL.SchemaBuilder.build_schema(get_auth_info())
end
def __absinthe_type__(:cube) do
Logger.debug "Getting details for the CUBE"
%Absinthe.Type.Object{
description: "This looks like a cube, but you'll have to check the " <>
"number of sides to make sure",
fields: %{
"sides" => %Absinthe.Type.Field{
__private__: [],
args: %{},
complexity: nil,
config: nil,
default_value: nil,
deprecation: nil,
description: "the number of sides",
identifier: "sides",
middleware: [],
name: "sides",
triggers: [],
type: :integer
},
"child" => %Absinthe.Type.Field{
__private__: [],
args: %{},
complexity: nil,
config: nil,
default_value: nil,
deprecation: nil,
description: "The child cube of this cubic cube.",
identifier: "child",
name: "child",
triggers: [],
type: :cube,
middleware: [
fn(resolution, _config) ->
Absinthe.Resolution.put_result(resolution,
{:ok, %{ "sides" => 4 }}
)
end
]
}
},
identifier: :cube,
interfaces: [],
is_type_of: nil,
name: "cube",
field_imports: []
}
end
def __absinthe_type__(type) do
Logger.debug "Getting details for type #{inspect type}"
Schema.Base.Superset.__absinthe_type__(type)
end
def context(context) do
Logger.debug "Context for the request: #{inspect context}"
context
end
def get_auth_info do
case :erlang.get(:conn_info) do
:undefined -> :not_request_context
%{ conn_private: %{ context: %{ owner: owner }}} -> owner
end
end
end
query cubes {
cubicobject {
sides
child {
sides
child {
sides
child {
sides
child {
sides
}
}
}
}
}
}
{
"data": {
"cubicobject": {
"sides": 4,
"child": {
"sides": 4,
"child": {
"sides": 4,
"child": {
"sides": 4,
"child": {
"sides": 4
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment