Module: Bearcat::Client::GraphQL
- Defined in:
- lib/bearcat/client/graph_ql.rb
Overview
Request body format should be:
- { query: "query string" }
- "query string"
- "path/to/gql/file", may be absolute, relative to application root, or relative to `app/graphql`. `.gql` extension is optional.
Instance Method Summary collapse
Instance Method Details
#graphql_query(query, args = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/bearcat/client/graph_ql.rb', line 17 def graphql_query(query, args = {}) if query.is_a?(String) if /\A\w+\Z/.match?(query) && !query.include?("{") query = query.underscore @@gql_cache ||= {} query = cache_on_class("gql:#{query}") do paths = [] pn = Pathname.new(query) if pn.absolute? paths << query else paths << Rails.root.join("app", "graphql", query) paths << Rails.root.join(query) end paths = paths.flat_map do |path| [path, "#{path}.gql"] end query = paths.find do |path| File.exist?(path) end File.read(query) end end args.symbolize_keys! query = query.gsub(/\{\{(.*?)\}\}/) do |_m| match = Regexp.last_match key = match[1].strip.to_sym args[key] end query = { query: query } end result = post('/api/graphql', query) raise GraphqlError.new("Error running GraphQL query:\n#{result["errors"].to_json}", response: result) if result["errors"].present? # TODO: It'd be nice to unwrap and return result["data"] directly, but we want to keep backwards compatibility result end |