{"id":17948811,"url":"https://github.com/danielefongo/yson","last_synced_at":"2025-10-16T19:09:18.089Z","repository":{"id":57558333,"uuid":"347628281","full_name":"danielefongo/yson","owner":"danielefongo","description":"Run json/graphql requests and parse responses in an easy way.","archived":false,"fork":false,"pushed_at":"2021-06-11T17:24:57.000Z","size":245,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-19T05:07:08.144Z","etag":null,"topics":["dsl","graphql","json","parsing","schema"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielefongo.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-14T12:17:34.000Z","updated_at":"2024-05-31T17:22:30.000Z","dependencies_parsed_at":"2022-08-28T09:32:18.478Z","dependency_job_id":null,"html_url":"https://github.com/danielefongo/yson","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielefongo%2Fyson","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielefongo%2Fyson/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielefongo%2Fyson/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielefongo%2Fyson/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielefongo","download_url":"https://codeload.github.com/danielefongo/yson/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245366205,"owners_count":20603438,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dsl","graphql","json","parsing","schema"],"created_at":"2024-10-29T09:10:02.105Z","updated_at":"2025-10-16T19:09:13.044Z","avatar_url":"https://github.com/danielefongo.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Yson\n\n![GitHub Workflow Status](https://img.shields.io/github/workflow/status/danielefongo/yson/ci)\n![Coveralls](https://img.shields.io/coveralls/github/danielefongo/yson)\n[![Hex pm](http://img.shields.io/hexpm/v/yson.svg?style=flat)](https://hex.pm/packages/yson)\n![Hex.pm](https://img.shields.io/hexpm/l/yson)\n\nRun json/graphql requests and parse responses in an easy way.\n\n## Installation\n\nThe package can be installed by adding `yson` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:yson, \"~\u003e 0.2.2\"}\n  ]\nend\n```\n\n## Usage\n\n`yson` can be used both to run GraphQL requests and to parse JSON responses.\n\n### GraphQL\n\nFirst create a `Yson.GraphQL.Schema` object:\n\n```elixir\ndefmodule User do\n  use Yson.GraphQL.Schema\n\n  query :user do # defines a query on :user\n    arg :person do\n      arg(:full_name, :string)\n    end\n  end\n\n  root do\n    value(:email)\n\n    map :person, resolver: \u0026User.person/1 do\n      interface :natural_person do\n        value(:first_name)\n        value(:last_name)\n      end\n\n      interface :legal_person do\n        value(:company_name)\n      end\n    end\n  end\n\n  def person(%{company_name: name}), do: %{full_name: name}\n  def person(%{first_name: name, last_name: surname}), do: %{full_name: \"#{name} #{surname}\"}\nend\n```\n\nThen run a Graphql request using `Yson.GraphQL.Api`:\n\n```elixir\nalias Yson.GraphQL.Api\n\nvariables = %{full_name: \"john doe\"}\nheaders = [] #optional\noptions = [] #optional\n{:ok, result} = Api.run(User, variables, \"https://mysite.com/graphql\", headers, options)\n```\n\nThe result will be already mapped using resolvers, so it could be something like the following:\n\n```elixir\n%{\n  user: %{\n    email: \"a@b.c\",\n    person: %{\n      full_name: \"legal name\"\n    }\n  }\n}\n```\n\n#### Custom usage\n\nThe `Yson.GraphQL.Schema` object exposes two methods:\n\n- `\u0026describe/0`, to build the object description.\n- `\u0026resolvers/0`, to build the object resolvers tree.\n\nthat can be combined with the modules `Yson.GraphQL.Builder` and `Yson.Parser`.\n\n##### Create query\n\n`Yson.GraphQL.Builder.build/2` accepts a Yson description and the variables. It can be used as follows:\n\n```elixir\niex\u003e Yson.GraphQL.Builder.build(User.describe(), variables)\niex\u003e %{\n  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}\",\n  variables: %{full_name: \"john doe\"}\n}\n```\n\n##### Parse response\n\n`Yson.Parser.parse/3` accepts a Yson resolvers tree and the payload (map with atom keys). It can be used as follows:\n\n```elixir\niex\u003e payload = %{user: %{email: \"a@b.c\", person: %{companyName: \"legal name\"}}}\niex\u003e Yson.Parser.parse(User.resolvers(), payload, :snake)\niex\u003e %{\n  user: %{\n    email: \"a@b.c\",\n    user: %{\n      full_name: \"legal name\"\n    }\n  }\n}\n```\n\n### JSON\n\nCurrently there is no implemented `Api` module for running Json requests, but it is still possible to use `yson` to parse responses.\n\n#### Define schema\n\nThe first step is to define a `Yson.Json.Schema` schema.\n\n```elixir\ndefmodule User do\n  use Yson.Json.Schema\n\n  root do # defines the object root\n    value(:email)\n\n    map :person, resolver: \u0026User.person/1 do\n      interface :natural_person do\n        value(:first_name)\n        value(:last_name)\n      end\n\n      interface :legal_person do\n        value(:company_name)\n      end\n    end\n  end\n\n  def person(%{company_name: name}), do: %{full_name: name}\n  def person(%{first_name: name, last_name: surname}), do: %{full_name: \"#{name} #{surname}\"}\nend\n```\n\n#### Parse response\n\n`Yson.Parser.parse/3` accepts a Yson resolvers tree and the payload (map with atom keys). It can be used as follows:\n\n```elixir\niex\u003e payload = %{email: \"a@b.c\", person: %{companyName: \"legal name\"}}\niex\u003e Yson.Parser.parse(User.resolvers(), payload, :snake)\niex\u003e %{email: \"a@b.c\", user: %{full_name: \"legal name\"}}\n```\n\nThe available strategies for key recasing are:\n\n- `:snake` to convert payload keys to snake case before parsing\n- `:camel` to convert payload keys to camel case before parsing\n- `:no_case` to preserve the original casing\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielefongo%2Fyson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielefongo%2Fyson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielefongo%2Fyson/lists"}