{"id":13878552,"url":"https://github.com/Garllon/graphql_connector","last_synced_at":"2025-07-16T14:32:34.740Z","repository":{"id":35145185,"uuid":"212621679","full_name":"Garllon/graphql_connector","owner":"Garllon","description":"An easy connector to call your graphql server","archived":false,"fork":false,"pushed_at":"2024-01-16T12:46:57.000Z","size":119,"stargazers_count":11,"open_issues_count":9,"forks_count":4,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-11-18T13:25:37.840Z","etag":null,"topics":["client","connector","graphql","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","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/Garllon.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-03T16:05:26.000Z","updated_at":"2023-05-20T13:12:36.000Z","dependencies_parsed_at":"2024-11-20T13:32:51.384Z","dependency_job_id":null,"html_url":"https://github.com/Garllon/graphql_connector","commit_stats":{"total_commits":122,"total_committers":7,"mean_commits":"17.428571428571427","dds":0.4918032786885246,"last_synced_commit":"1d6748179423784fae12e35257c84b40bc7c0b48"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Garllon%2Fgraphql_connector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Garllon%2Fgraphql_connector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Garllon%2Fgraphql_connector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Garllon%2Fgraphql_connector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Garllon","download_url":"https://codeload.github.com/Garllon/graphql_connector/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226138849,"owners_count":17579496,"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":["client","connector","graphql","ruby"],"created_at":"2024-08-06T08:01:52.886Z","updated_at":"2024-11-24T07:31:07.921Z","avatar_url":"https://github.com/Garllon.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"\n# GraphqlConnector\n\n[![Gem\nVersion](https://badge.fury.io/rb/graphql_connector.svg)](https://badge.fury.io/rb/graphql_connector)\n[![CI](https://github.com/Garllon/graphql_connector/workflows/CI/badge.svg)](https://github.com/Garllon/graphql_connector/actions?query=workflow%3ACI)\n[![Maintainability](https://api.codeclimate.com/v1/badges/548db3cf0d078b379c84/maintainability)](https://codeclimate.com/github/Garllon/graphql_connector/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/548db3cf0d078b379c84/test_coverage)](https://codeclimate.com/github/Garllon/graphql_connector/test_coverage)\n\nAn easy connector to call your `graphql` server. Currently there is no schema\ncheck in the code, but i will add it.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'graphql_connector'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install graphql_connector\n\n## Usage\n\nYou need to configure the `graphql_connector` first:\n``` ruby\nGraphqlConnector.configure do |config|\n  config.add_server(name: 'Foo', uri: 'http://foo.com/api/graphql', headers: {}, connector: {}, httparty_adapter_options: {})\nend\n```\n\n* `name` (**mandatory**): Add a namespace under which the API is reachable. In the above example `'Foo'` means that `GraphqlConnector::Foo` provides API functionality for the configured `add_server`\n\n* `uri` (**mandatory**): Add uri of grapqhl API server that accepts **POST** requests.\n\n* `connector` (**optionally**): Add a  **Authentication Token class** in the following format:\n```ruby\nconnector: { base: AuthTokenAgent.instance, method: 'get_authorization_header' }\n```\n`base` is an instance of Authentication Token class and `method` represents the function where it gets the token.\n```ruby\nclass AuthTokenAgent\n   [...]\n   def get_authorization_header\n      [...]\n      { 'Authorization' =\u003e 'Token HERE' }\n   end\nend\n```\n:warning: The function under `method` must be a public one in your connector class.\u003cbr /\u003e\n:warning: When you set a connector, it will override the setting in the headers for Authorization.\n\n\n* `httparty_adapter_options` (**optionally**): Add any [`connection_adapter`](https://github.com/jnunemaker/httparty/blob/master/lib/httparty/connection_adapter.rb) options that `httparty` supports in a hash format e.g. `{ timeout: 4 }`\n\n\nFor each graphql server you wish to query use `add_server`.\n\nAfterwards you will have the following options to fetch and/or mutate data with\none or many graphql servers:\n\n* `raw_query` --\u003e [Examples](examples/raw_query_examples.rb)\n* `query` --\u003e [Examples](examples/query_examples.rb)\n* `mutation` --\u003e [Examples](examples/mutation_examples.rb)\n* `service class inclusion` --\u003e [Examples](examples/departments_service_class_examples.rb)\n\nSee the following sub sections for details\n\n### raw_query\n\nYou can call your graphql_endpoint via:\n```ruby\nGraphqlConnector::\u003cname\u003e.raw_query('query { products { id name } }')\n\nGraphqlConnector::\u003cname\u003e.raw_query('query { products($id: [ID!]) { products(id: $id) { id name } } }', variables: { id: 1 })\n\nGraphqlConnector::\u003cname\u003e.raw_query('mutation { createProduct(name: \"Coca-Cola\") { id name } }')\n```\n\nOr if you want to **override**/**set** any `httparty_adapter_options` it is also possible to pass them in:\n```ruby\nGraphqlConnector::\u003cname\u003e.raw_query('query { products { id name } }', httparty_adapter_options: { timeout: 3, verify: true })\n\nGraphqlConnector::\u003cname\u003e.raw_query('query { products($id: [ID!]) { products(id: $id) { id name } } }', variables: { id: 1 }, httparty_adapter_options: { timeout: 3, verify: true })\n\nGraphqlConnector::\u003cname\u003e.raw_query('mutation { createProduct(name: \"Coca-Cola\") { id name } }', httparty_adapter_options: { timeout: 3, verify: true })\n```\n\n\nNote that `\u003cname\u003e` has to be replaced by any of the ones added via `add_server`\n\nSee also [here](examples/raw_query_examples.rb) for example usage\n\n---\n### query\n\nYou can also use the more comfortable `query`:\n```ruby\nGraphqlConnector::\u003cname\u003e.query('products', { id:  [1,2] } , ['id','name'])\n```\n\nOr if you want to **override**/**set** any `httparty` adapter_options, it is also possible\nto pass them in:\n\n```ruby\nGraphqlConnector::\u003cname\u003e.query('products', { id:  [1,2] } , ['id','name'], httparty_adapter_options: { timeout: 3 })\n```\n\n| Variable                 | Required                      | DataType                | Example                                 |\n|--------------------------|-------------------------------|-------------------------|-----------------------------------------|\n| model                    | Yes                           | String                  | 'product'                               |\n| condition                | Yes (but can be empty)        | Hash(key, value)        | { id: 1 }                               |\n| selected_fields          | Yes                           | Array of Strings/Hashes | ['id', 'name', productCategory: ['id']] |\n| httparty_adapter_options | No                            | Hash                    | { timeout: 3 }                          |\n\nSee also [here](examples/query_examples.rb) for example usage\n\n#### selected_fields\n\nThe syntax for the associations looks like the following:\n```\n['\u003cattribute_name\u003e', \u003cassociation_name\u003e: ['\u003cattribute_name_of_the_association\u003e']]\n```\n\nExample:\n```ruby\n['id', 'name', productCategory: ['id', 'name']]\n```\n\n---\n\n### mutation\n\nWorks in the same way as [query](#query)\n\nSee also [here](examples/mutation_examples.rb) for example usage\n\n### Service class inclusion\n\nThis approach can be used to `graphqlize` **any** kind of ruby (service) class\nso that it has re-usable graphql `query` and `mutation` **class methods**.\n\n* First add `extend GraphqlConnector::\u003cserver\u003e::Query` in the the class(es) that should be `graphqlized`\n\n* Then you can aliases as many graphql server types via `add_query` and/or `add_raw_query` and/or `add_mutation`:\n\n```ruby\nadd_query products_by: :products, params: [:id], returns: [:id, :name]\n\nadd_raw_query products_raw_by: 'query { products(($id: [ID!]) products { id name } }', params: [:id]\n\nadd_mutation create: :createProduct, params: [:name], returns: [:id, :name]\n```\n* :grey_exclamation: If not needed omit `params`\n\nOr if you want to **override**/**set** any `httparty` adapter_options, it is also possible\nto pass them in:\n\n```ruby\nadd_query products_by: :products, params: [:id], returns: [:id, :name], httparty_adapter_options: { timeout: 3 }\n\nadd_raw_query products_raw_by: 'query { products(($id: [ID!]) products { id name } }', params: [:id], httparty_adapter_options: { verify: true }\n\nadd_mutation create: :createProduct, params: [:name], returns: [:id, :name], httparty_adapter_options: { timeout: 5, verify: false }\n```\n\nSee also [here](examples/departments_service_class_examples.rb) and also here for complete example usage:\n\n```ruby\nGraphqlConnector.configure do |config|\n  config.add_server(name: 'Foo', uri: 'http://foo.com/api/graphql', headers: {})\nend\n\n# product.rb\nclass Product\n  extend GraphqlConnector::Foo::Query\n\n  add_query all: :products,\n            returns: [:id, :name]\n\n  add_query by_id: :products,\n            params: :id,\n            returns: [:name, product_category: [:id, :name]],\n            httparty_adapter_options: { timeout: 3 }\n\n  add_query by_names: :products,\n            params: :names,\n            returns: [:id, :name, product_category: [:id, :name]]\n\n  add_query by: :products,\n            params: [:id, :name],\n            returns: [:name]\n\n  add_query by_category_id: :products,\n            params: :product_category,\n            returns: [product_category: [:id, :name]]\n\n  add_mutation create: :createProduct,\n               params: [:name, :catgetoryId],\n               returns: [:id, :name],\n               httparty_adapter_options: { verify: false }\nend\n\nProduct.all\n=\u003e [OpenStruct\u003cid=1, name='Demo Product', ...]\n\nProduct.by_id(id: 1)\n=\u003e [OpenStruct\u003cname='Demo Product', product_category=\u003cProductCategory\u003cid=10, name='Demo Category'\u003e\u003e]\n\nProduct.by_names(names: ['Demo Product', 'Non Demo Product'])\n=\u003e [OpenStruct\u003cid=1, name='Demo Product', product_category=\u003cProductCategory\u003cid=10, name='Demo Category'\u003e\u003e, Product\u003cid=2, name='Demo Product' ...]\n\nProduct.by(id: 1, name: 'Demo Product')\n=\u003e OpenStruct\u003cname='Demo Product'\u003e\n\nProduct.by_category_id(product_category: { id: 10})\n=\u003e OpenStruct\u003cproduct_category=\u003cProductCategory\u003cid=10, name='Demo Category'\u003e\u003e\n\nProduct.create(name: 'Another Product', catgetoryId: 10)\n=\u003e OpenStruct\u003cid=10, name='Another Product'\u003e\n```\n\nAlso custom **class methods** can used to call any kind of `query` and do further selection instead:\n\n```ruby\nclass Product\n  extend GraphqlConnector::Foo::Query\n\n  add_query all: :products, returns: [:name]\n\n  def self.by_id(id:)\n    all.select { |products| products.id == id }.first\n  end\nend\n\nProduct.by_id(id: 1)\n=\u003e OpenStruct\u003cid=1, name='Demo Product'\u003e\u003e\n```\n\n:warning: Ensure that your custom **class method** never has the **same name** as an `\u003calias\u003e` of `add_query`, `add_raw_query` or `add_mutation`. Otherwise the associated grapqhl query will not be performed because of [Ruby Open Class principle](https://ruby-lang.co/ruby-open-class/)\n\n\nExample for `raw_query`:\n\n```ruby\nclass Product\n  extend GraphqlConnector::Foo::Query\n\n  add_raw_query all: ' query { products { id name } } '\n  add_raw_query by: ' query products($id: !ID, $name: !String) '\\\n                '{ products(id: $id, name: $name) { id name } }',\n                params: [:id, :name]\n\nend\n\nProduct.all\n=\u003e [ { id: '1', name: 'Demo Product' }, ...]\n\nProduct.by(id: '1', name: 'Demo Product')\n=\u003e { id: '1', name: 'Demo Product' }\n\n```\n\n:exclamation: There is no `add_raw_mutation` since `add_raw_query` does already cover such a case\n\n## Development\n\nAfter checking out the repo, run\n```shell\nbundle install\n```\n\nThen, run\n```shell\nbundle exec rspec spec\n```\nto run the tests.\nYou can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at [https://github.com/garllon/graphql_connector](https://github.com/garllon/graphql_connector). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the GraphqlConnector project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Garllon/graphql_connector/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGarllon%2Fgraphql_connector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGarllon%2Fgraphql_connector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGarllon%2Fgraphql_connector/lists"}