Class: Utcp::Providers::GraphQLProvider
- Inherits:
-
BaseProvider
- Object
- BaseProvider
- Utcp::Providers::GraphQLProvider
- Defined in:
- lib/utcp/providers/graphql_provider.rb
Overview
Simple GraphQL over HTTP tool.provider: { “provider_type”:“graphql”, “url”:“https://…”,
"query":"query ($code:String!) { country(code:$code){name}}",
"operationName": "...optional..." }
Instance Attribute Summary
Attributes inherited from BaseProvider
Instance Method Summary collapse
- #call_tool(tool, arguments = {}, &block) ⇒ Object
- #discover_tools! ⇒ Object
-
#initialize(name:, auth: nil, headers: {}) ⇒ GraphQLProvider
constructor
A new instance of GraphQLProvider.
Constructor Details
#initialize(name:, auth: nil, headers: {}) ⇒ GraphQLProvider
Returns a new instance of GraphQLProvider.
17 18 19 20 |
# File 'lib/utcp/providers/graphql_provider.rb', line 17 def initialize(name:, auth: nil, headers: {}) super(name: name, provider_type: "graphql", auth: auth) @headers = headers || {} end |
Instance Method Details
#call_tool(tool, arguments = {}, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/utcp/providers/graphql_provider.rb', line 26 def call_tool(tool, arguments = {}, &block) p = tool.provider url = Utils::Subst.apply(p["url"]) uri = URI(url) body = { "query" => Utils::Subst.apply(p["query"]), "variables" => Utils::Subst.apply(arguments || {}) } body["operationName"] = p["operationName"] if p["operationName"] req = Net::HTTP::Post.new(uri) headers = { "Content-Type" => "application/json" }.merge(@headers) @auth&.apply_headers(headers) headers.each { |k, v| req[k] = v } req.body = JSON.dump(body) http = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") begin res = http.request(req) JSON.parse(res.body) rescue res.body ensure http.finish if http.active? end end |
#discover_tools! ⇒ Object
22 23 24 |
# File 'lib/utcp/providers/graphql_provider.rb', line 22 def discover_tools! raise ProviderError, "GraphQL is an execution provider only" end |