Class: Printavo::GraphqlClient
- Inherits:
-
Object
- Object
- Printavo::GraphqlClient
- Defined in:
- lib/printavo/graphql_client.rb
Constant Summary collapse
- SAFE_ERROR_EXTENSION_KEYS =
%w[classification code].freeze
- SAFE_METADATA_HEADERS =
%w[ retry-after x-request-id x-ratelimit-limit x-ratelimit-remaining x-ratelimit-reset ].freeze
Instance Method Summary collapse
-
#initialize(connection, cache: nil, default_ttl: 300, sensitive_values: []) ⇒ GraphqlClient
constructor
A new instance of GraphqlClient.
-
#mutate(mutation_string, variables: {}) ⇒ Hash
Executes a GraphQL mutation and returns the parsed
datahash. -
#mutate_envelope(mutation_string, variables: {}) ⇒ Printavo::ResponseEnvelope
Mutation equivalent of #query_envelope.
-
#paginate(query_string, path:, variables: {}, first: 25) {|nodes| ... } ⇒ Object
Iterates all pages of a paginated GraphQL query, yielding each page's nodes array.
-
#query(query_string, variables: {}) ⇒ Hash
Executes a GraphQL query and returns the parsed
datahash. -
#query_envelope(query_string, variables: {}) ⇒ Printavo::ResponseEnvelope
Executes a GraphQL query without discarding partial data when GraphQL errors are present.
Constructor Details
#initialize(connection, cache: nil, default_ttl: 300, sensitive_values: []) ⇒ GraphqlClient
Returns a new instance of GraphqlClient.
24 25 26 27 28 29 30 31 32 |
# File 'lib/printavo/graphql_client.rb', line 24 def initialize(connection, cache: nil, default_ttl: 300, sensitive_values: []) @connection = connection @cache = cache @default_ttl = default_ttl @sensitive_values = sensitive_values.filter_map do |value| string = value.to_s string unless string.empty? end.freeze end |
Instance Method Details
#mutate(mutation_string, variables: {}) ⇒ Hash
Executes a GraphQL mutation and returns the parsed data hash.
Semantically equivalent to query — both POST to the same endpoint —
but distinguishes write intent at the call site.
74 75 76 |
# File 'lib/printavo/graphql_client.rb', line 74 def mutate(mutation_string, variables: {}) execute(mutation_string, variables: variables) end |
#mutate_envelope(mutation_string, variables: {}) ⇒ Printavo::ResponseEnvelope
Mutation equivalent of #query_envelope. Callers must decide whether a partial mutation response represents an uncertain write.
91 92 93 |
# File 'lib/printavo/graphql_client.rb', line 91 def mutate_envelope(mutation_string, variables: {}) execute_envelope(mutation_string, variables: variables) end |
#paginate(query_string, path:, variables: {}, first: 25) {|nodes| ... } ⇒ Object
Iterates all pages of a paginated GraphQL query, yielding each page's
nodes array. The query must accept $first: Int and $after: String
variables, and the target connection must expose nodes and pageInfo.
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/printavo/graphql_client.rb', line 116 def paginate(query_string, path:, variables: {}, first: 25) after = nil loop do data = execute(query_string, variables: variables.merge(first: first, after: after)) conn = dig_path(data, path) nodes = conn&.fetch('nodes', []) || [] yield nodes page_info = conn&.fetch('pageInfo', {}) || {} break unless page_info['hasNextPage'] after = page_info['endCursor'] end end |
#query(query_string, variables: {}) ⇒ Hash
Executes a GraphQL query and returns the parsed data hash.
46 47 48 49 50 51 52 |
# File 'lib/printavo/graphql_client.rb', line 46 def query(query_string, variables: {}) return execute(query_string, variables: variables) unless @cache @cache.fetch(cache_key(query_string, variables), expires_in: @default_ttl) do execute(query_string, variables: variables) end end |
#query_envelope(query_string, variables: {}) ⇒ Printavo::ResponseEnvelope
Executes a GraphQL query without discarding partial data when GraphQL errors are present. HTTP and transport failures still raise their identifier-only SDK exceptions.
83 84 85 |
# File 'lib/printavo/graphql_client.rb', line 83 def query_envelope(query_string, variables: {}) execute_envelope(query_string, variables: variables) end |