Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/peburrows/plot
A graphql parser and resolver for Elixir
https://github.com/peburrows/plot
Last synced: about 1 month ago
JSON representation
A graphql parser and resolver for Elixir
- Host: GitHub
- URL: https://github.com/peburrows/plot
- Owner: peburrows
- Created: 2015-07-11T15:33:52.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-12-11T17:46:12.000Z (about 9 years ago)
- Last Synced: 2024-08-01T22:51:23.434Z (4 months ago)
- Language: Elixir
- Homepage:
- Size: 54.7 KB
- Stars: 32
- Watchers: 4
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-graphql - plot - GraphQL parser and resolver for Elixir. (Libraries / Elixir Libraries)
- awesome-graphql - plot - GraphQL parser and resolver for Elixir. (Libraries / Elixir Libraries)
README
Plot
====
A GraphQL parser and resolver for Elixir.This project is still a work in progress, but the eventual goal is to support the full GraphQL spec.
### Basic Usage
*Build a basic AST from a doc*
```elixir
"{
user {
id,
firstName,
lastName
}
}" |> Plot.parse
# returns:
{:ok,
[{:query, nil,
[{:object, "user", nil, [],
[{:field, "id", nil, []},
{:field, "firstName", nil, []},
{:field, "lastName", nil, []}]}]}]}
```*Build Plot objects from a doc*
```elixir
"{
user {
id,
firstName,
lastName
}
}" |> Plot.parse_and_generate
# returns:
%Plot.Document{fragments: [],
operations: [%Plot.Query{name: nil,
objects: [%Plot.Object{alias: nil, args: [], name: "user",
fields: [%Plot.Field{alias: nil, args: [], name: "lastName"},
%Plot.Field{alias: nil, args: [], name: "firstName"},
%Plot.Field{alias: nil, args: [], name: "id"}]}]}],
variables: []}
```### Query resolution
Query resolution works via Elixir protocols. Implement the `Resolution` protocol for the various nodes of your queries.
```elixir
# Implementation
defimpl Plot.Resolution, for: Plot.Object do
def resolve(%Plot.Object{name: "user"} = _obj, _context) do
%{"firstName" => "Phil", "lastName" => "Burrows", dontInclude: "this"}
enddef resolve(%Plot.Object{name: "birthday"}, _context) do
%{"day" => 15, "month" => 12, "year" => 2015}
end
end# Resolution
doc = "{user {firstName, lastName, birthday {month, year}}}" |> Plot.parse_and_generate
doc.operations |> Enum.at(0) |> Plot.Resolver.resolve
# returns:
[%{"user" => %{"birthday" => %{"month" => 12, "year" => 2015},
"firstName" => "Phil", "lastName" => "Burrows"}}]
```