Skip to content

Instantly share code, notes, and snippets.

Martin Elvar MartinElvar

Block or report user

Report or block MartinElvar

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View help.js
import React, { Component } from "react";
import PropTypes from "prop-types";
import { Container, Row, Col } from "reactstrap";
import { graphql } from "react-apollo";
import { withRouter } from "react-router";
import ArticleCard from "../../components/ArticleCard";
import Paginator from "../../components/Paginator";
import TAG_QUERY from "../../graphql/queries/TagQuery.graphql";
import MORE_ARTICLES_TAG_QUERY from "../../graphql/queries/MoreArticlesTagQuery.graphql";
View graphql_helpers.ex
defmodule GraphqlHelpers do
import Ecto.Query
@doc """
## example
field :storages, list_of(:storage), do: has_many(:storages)
"""
defmacro has_many(model) do
quote do
resolve fn subject, _, _ ->
View protected_fields.ex
defmodule Users.Types do
use Absinthe.Schema.Notation
import GraphqlHelpers
object :user do
field :id, :id
field :first_name, :string
field :last_name, :string
field :email, :string
field :phone, :string
View GraphQLHelpers.ex
defmodule GraphqlHelpers do
import Ecto.Query
defmacro has_many(model, foreign_key) do
quote do
resolve fn subject, _, _ ->
batch({GraphqlHelpers, :process_has_many, {unquote(model), unquote(foreign_key)}}, subject.id, fn (batch_result) ->
{:ok, Map.get(batch_result, subject.id, %{})}
end)
end
View macro.ex
defmacro belongs_to(model) do
[_ | module] = Module.split(unquote(model))
key = Macro.underscore(module) <> "_id" |> String.to_atom
quote do
.....
> belongs_to(Users.User)
== Compilation error on file lib/storages/schemas/storage_schema.ex ==
** (FunctionClauseError) no function clause matching in Module.split/1
View plug_error.ex
defmodule ApiEndpoint.Endpoint do
# ....
plug Absinthe.Plug,
schema: ApiEndpoint.Schema
end
defmodule ApiEndpoint.Router do
forward "/api/v2", Absinthe.Plug, schema: ApiEndpoint.Schema
end
View has_many.ex
defmodule ApiEndpoint.Schema.Helpers do
def has_many({model, foreign_key}, ids) do
import Ecto.Query
model
|> where([m], field(m, ^foreign_key) in ^ids)
|> Storages.Repo.all
|> Enum.group_by(fn storage -> Map.get(storage, foreign_key) end)
end
end
View batching.ex
# User has many storages.
#
# Give me a user, along with the storages it owns.
# q={user(id: 131) {name, storages{title} }}
defmodule Users.Types do
use Absinthe.Schema.Notation
object :user do
field :id, :id
View skygge-tracking.js
// Find all assignments & views.
var assignments = App.__container__.lookup('store:main').all("assignment");
var views = App.__container__.lookup("component:assignment-frame")._viewRegistry;
$.each(views, function(i, view) {
// If the view is empty, or does not have a controller context, skip.
if (view == null || view._controller.model == null) {
return;
}
// Get the id controller context model.
@MartinElvar
MartinElvar / builder.ex
Last active Sep 30, 2015
Ecto builder Arithmetic support
View builder.ex
# The idea is to add support for something like.
# query = from s in Subscription,
# order_by: s.price + s.registration_fee
#
# subscriptions = Repo.all(query)
# Arithmetic
def escape({operator, _, [left, right]} = expr, type, params, vars, env) when operator in ~w(+ - * /)a do
ltype = quoted_type(right, vars)
rtype = quoted_type(left, vars)
You can’t perform that action at this time.