{"id":21144169,"url":"https://github.com/straw-hat-labs/straw_hat_graphql","last_synced_at":"2025-12-11T23:34:16.298Z","repository":{"id":57553717,"uuid":"116933202","full_name":"straw-hat-labs/straw_hat_graphql","owner":"straw-hat-labs","description":"Utility Package for GraphQL","archived":false,"fork":false,"pushed_at":"2020-08-14T21:16:03.000Z","size":166,"stargazers_count":5,"open_issues_count":3,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-07-04T03:42:31.919Z","etag":null,"topics":["absinthe","absinthe-graphql","elixir","elixir-lang","elixir-library","graphql","module"],"latest_commit_sha":null,"homepage":"https://hex.pm/packages/straw_hat_graphql","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/straw-hat-labs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-10T08:55:03.000Z","updated_at":"2021-02-04T21:21:24.000Z","dependencies_parsed_at":"2022-08-28T10:23:48.065Z","dependency_job_id":null,"html_url":"https://github.com/straw-hat-labs/straw_hat_graphql","commit_stats":null,"previous_names":["straw-hat-team/straw_hat_graphql"],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/straw-hat-labs/straw_hat_graphql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/straw-hat-labs%2Fstraw_hat_graphql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/straw-hat-labs%2Fstraw_hat_graphql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/straw-hat-labs%2Fstraw_hat_graphql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/straw-hat-labs%2Fstraw_hat_graphql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/straw-hat-labs","download_url":"https://codeload.github.com/straw-hat-labs/straw_hat_graphql/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/straw-hat-labs%2Fstraw_hat_graphql/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264405867,"owners_count":23603028,"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":["absinthe","absinthe-graphql","elixir","elixir-lang","elixir-library","graphql","module"],"created_at":"2024-11-20T08:08:51.482Z","updated_at":"2025-12-11T23:34:11.277Z","avatar_url":"https://github.com/straw-hat-labs.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# StrawHat.GraphQL\n\n[![Build Status](https://travis-ci.org/straw-hat-team/straw_hat_graphql.svg?branch=master)](https://travis-ci.org/straw-hat-team/straw_hat_graphql)\n[![codecov](https://codecov.io/gh/straw-hat-team/straw_hat_graphql/branch/master/graph/badge.svg)](https://codecov.io/gh/straw-hat-team/straw_hat_graphql)\n[![Inline docs](http://inch-ci.org/github/straw-hat-team/straw_hat_graphql.svg)](http://inch-ci.org/github/straw-hat-team/straw_hat_graphql)\n\nGraphQL utils.\n\n## Installation\n\n```elixir\ndef deps do\n  [\n    {:straw_hat_graphql, \"~\u003e 0.1\"}\n  ]\nend\n```\n\n## Usage\n\nThe package come with predefined types that you could use in your project.\nMost of those types dictate how your data structure and responses from your\nmutations will look like but it will help you to speed up your development.\n\n```elixir\ndefmodule YourApp.AbsintheSchema do\n  use Absinthe.Schema\n  use Absinthe.Schema.Notation\n\n  # Should be include it before you use it\n\n  import_types StrawHat.GraphQL.Types\n  import_types StrawHat.GraphQL.Scalars\n\n  # ...\nend\n```\n\nCheck `StrawHat.GraphQL.Types` and `StrawHat.GraphQL.Scalars` for more\ninformation about the included types.\n\n### Paginations\n\nThere is some types for doing paginations. We follow Scrivener for formatting\nthe response.\n\n**Note:** For now there is some boilerplate code for the paginations. We are\naware of using macros but to be honest sometimes is better a repeated code\nthat shoot ourself in the foot, still thinking about creating macros for this.\n\n```elixir\nobject :person do\n  interface :straw_hat_node\n\n  field :id, non_null(:id)\n\n  # ... more fields\nend\n\n# We recommend to use `_pagination`, this is another place where we could macros.\nobject :people_pagination do\n  # This will force you to defined the schemas\n  interface(:straw_hat_pagination)\n\n  field(:page, non_null(:straw_hat_pagination_page))\n  field(:total_entries, non_null(:integer))\n  field(:total_pages, non_null(:integer))\n\n  # This is where it gets tricky and probably we could use macros\n  # the reason is that we can't put this field inside `:straw_hat_pagination`\n  # because the type is not the same for all the objects.\n  field(:entries, list_of(:person))\nend\n```\n\nNow let's create the query.\n\n```elixir\nfield :people, :people_pagination do\n  arg(:pagination, non_null(:straw_hat_pagination_page_input))\n  resolve(\u0026PeopleResolver.get_people/2)\nend\n```\n\n### Mutations\n\nMutations is something that comes really handy and with a really nice API.\n\n**Note:** For now there is some boilerplate code for the mutations. We are\naware of using macros but to be honest sometimes is better a repeated code\nthat shoot ourself in the foot, still thinking about creating macros for this.\n\nFirst, you need to create the mutation response which normally should extend\nfrom the interface of `mutation_response`\n\n```elixir\nobject :person do\n  interface :straw_hat_node\n\n  field :id, non_null(:id)\n\n  # ... more fields\nend\n\n# We recommend to use `_payload`, this is another place where we could macros\nobject :person_payload do\n  # This will force you to defined the schemas\n  interface :straw_hat_mutation_response\n\n  field :successful, non_null(:boolean)\n  field :errors, list_of(:straw_hat_error)\n\n  # This is where it gets tricky and probably we could use macros\n  # the reason is that we can't put this field inside `:straw_hat_mutation_response`\n  # because the type is not the same for all mutations.\n  field :payload, :person\nend\n```\n\nNow we create the mutation\n\n```elixir\nobject :person_mutations do\n  field :person_update, :person_payload do\n    arg :person, non_null(:person_input)\n\n    # other resolvers, maybe authorization\n    resolve \u0026MyApp.PersonResolvers.update_person/2\n  end\nend\n```\n\nNow what is left is the resolver function itself\n\n```elixir\ndefmodule MyApp.PersonResolvers do\n  alias StrawHat.GraphQL.MutationResponse\n\n  def update_person(%{person: person_params}, _) do\n    response = MyApp.Person.update_person(person_params)\n\n    # We do not want to force anything the responses from your modules\n    # it up to you, make sure you call the correct function for format\n    # the data structure\n    case response do\n      {:ok, response} -\u003e MutationResponse.succeeded(response)\n      {:error, reason} -\u003e MutationResponse.failed(reason)\n    end\n  end\nend\n```\n\nIf you want to avoid doing the pattern matching constantly which it is up to your\napplication design, you could create a function that does it for you\n\n```elixir\ndefmodule MyApp.ResolverHelpers\n  alias StrawHat.GraphQL.MutationResponse\n\n  def response_with({:ok, response}), do: MutationResponse.succeeded(response)\n  def response_with({:error, reason}), do: MutationResponse.failed(reason)\n  def response_with(_), do: raise(ArgumentError)\nend\n```\n\nthen your resolver becomes\n\n```elixir\nimport MyApp.ResolverHelpers\n\ndef update_person(%{person: person_params}, _) do\n  person_params\n  |\u003e MyApp.Person.update_person()\n  |\u003e response_with()\nend\n```\n\nAnd now you are go to go. Or use `StrawHat.GraphQL.MutationResponse.response_with/1`\nbut be aware that it is constrain by the inputs.\n\nAny suggestion and code pattern that you notice in your app, open an issue\nand let's talk about it.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstraw-hat-labs%2Fstraw_hat_graphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstraw-hat-labs%2Fstraw_hat_graphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstraw-hat-labs%2Fstraw_hat_graphql/lists"}