Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danielefongo/yson
Run json/graphql requests and parse responses in an easy way.
https://github.com/danielefongo/yson
dsl graphql json parsing schema
Last synced: 3 months ago
JSON representation
Run json/graphql requests and parse responses in an easy way.
- Host: GitHub
- URL: https://github.com/danielefongo/yson
- Owner: danielefongo
- License: gpl-3.0
- Created: 2021-03-14T12:17:34.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-11T17:24:57.000Z (over 3 years ago)
- Last Synced: 2024-09-16T18:56:48.559Z (4 months ago)
- Topics: dsl, graphql, json, parsing, schema
- Language: Elixir
- Homepage:
- Size: 239 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: LICENSE
Awesome Lists containing this project
README
# Yson
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/danielefongo/yson/ci)
![Coveralls](https://img.shields.io/coveralls/github/danielefongo/yson)
[![Hex pm](http://img.shields.io/hexpm/v/yson.svg?style=flat)](https://hex.pm/packages/yson)
![Hex.pm](https://img.shields.io/hexpm/l/yson)Run json/graphql requests and parse responses in an easy way.
## Installation
The package can be installed by adding `yson` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:yson, "~> 0.2.2"}
]
end
```## Usage
`yson` can be used both to run GraphQL requests and to parse JSON responses.
### GraphQL
First create a `Yson.GraphQL.Schema` object:
```elixir
defmodule User do
use Yson.GraphQL.Schemaquery :user do # defines a query on :user
arg :person do
arg(:full_name, :string)
end
endroot do
value(:email)map :person, resolver: &User.person/1 do
interface :natural_person do
value(:first_name)
value(:last_name)
endinterface :legal_person do
value(:company_name)
end
end
enddef person(%{company_name: name}), do: %{full_name: name}
def person(%{first_name: name, last_name: surname}), do: %{full_name: "#{name} #{surname}"}
end
```Then run a Graphql request using `Yson.GraphQL.Api`:
```elixir
alias Yson.GraphQL.Apivariables = %{full_name: "john doe"}
headers = [] #optional
options = [] #optional
{:ok, result} = Api.run(User, variables, "https://mysite.com/graphql", headers, options)
```The result will be already mapped using resolvers, so it could be something like the following:
```elixir
%{
user: %{
email: "[email protected]",
person: %{
full_name: "legal name"
}
}
}
```#### Custom usage
The `Yson.GraphQL.Schema` object exposes two methods:
- `&describe/0`, to build the object description.
- `&resolvers/0`, to build the object resolvers tree.that can be combined with the modules `Yson.GraphQL.Builder` and `Yson.Parser`.
##### Create query
`Yson.GraphQL.Builder.build/2` accepts a Yson description and the variables. It can be used as follows:
```elixir
iex> Yson.GraphQL.Builder.build(User.describe(), variables)
iex> %{
query: "query ($fullName: String) {\n user(person: {fullName: $fullName}) {\n email\n person {\n ... on LegalPerson {\n companyName\n }\n ... on NaturalPerson {\n firstName\n lastName\n }\n }\n }\n}",
variables: %{full_name: "john doe"}
}
```##### Parse response
`Yson.Parser.parse/3` accepts a Yson resolvers tree and the payload (map with atom keys). It can be used as follows:
```elixir
iex> payload = %{user: %{email: "[email protected]", person: %{companyName: "legal name"}}}
iex> Yson.Parser.parse(User.resolvers(), payload, :snake)
iex> %{
user: %{
email: "[email protected]",
user: %{
full_name: "legal name"
}
}
}
```### JSON
Currently there is no implemented `Api` module for running Json requests, but it is still possible to use `yson` to parse responses.
#### Define schema
The first step is to define a `Yson.Json.Schema` schema.
```elixir
defmodule User do
use Yson.Json.Schemaroot do # defines the object root
value(:email)map :person, resolver: &User.person/1 do
interface :natural_person do
value(:first_name)
value(:last_name)
endinterface :legal_person do
value(:company_name)
end
end
enddef person(%{company_name: name}), do: %{full_name: name}
def person(%{first_name: name, last_name: surname}), do: %{full_name: "#{name} #{surname}"}
end
```#### Parse response
`Yson.Parser.parse/3` accepts a Yson resolvers tree and the payload (map with atom keys). It can be used as follows:
```elixir
iex> payload = %{email: "[email protected]", person: %{companyName: "legal name"}}
iex> Yson.Parser.parse(User.resolvers(), payload, :snake)
iex> %{email: "[email protected]", user: %{full_name: "legal name"}}
```The available strategies for key recasing are:
- `:snake` to convert payload keys to snake case before parsing
- `:camel` to convert payload keys to camel case before parsing
- `:no_case` to preserve the original casing