Module: FragmentGraphQl

Extended by:
T::Sig
Defined in:
lib/fragment_client.rb

Overview

A support module for the client

Defined Under Namespace

Classes: CustomClient

Constant Summary collapse

VERSION =
FragmentSDK::VERSION
CustomHTTP =
Class.new(GraphQL::Client::HTTP) do
  extend T::Sig
  sig { params(context: T.untyped).returns(T::Hash[T.untyped, T.untyped]) }
  def headers(context)
    { 'Authorization' => format('Bearer %s', context[:access_token]),
      'X-Fragment-Client' => format('ruby-client@%s', FragmentGraphQl::VERSION) }
  end
end
HTTP =
T.let(CustomHTTP.new('https://api.fragment.dev/graphql'), GraphQL::Client::HTTP)
FragmentSchema =
T.let(GraphQL::Client.load_schema("#{__dir__}/fragment.schema.json"), T.untyped)
Client =

Use our custom client instead of the base GraphQL::Client

T.let(CustomClient.new(schema: FragmentSchema, execute: HTTP), CustomClient)
FragmentQueries =
T.let(parse_queries("#{__dir__}/queries.graphql"), T.untyped)

Class Method Summary collapse

Class Method Details

.method_name_for(operation_name) ⇒ Object



90
91
92
93
94
# File 'lib/fragment_client.rb', line 90

def self.method_name_for(operation_name)
  operation_name.gsub(/[a-z]([A-Z])/) do |m|
    format('%<lower>s_%<upper>s', lower: m[0], upper: T.must(m[1]).downcase)
  end.gsub(/^[A-Z]/, &:downcase)
end

.operation(name) ⇒ Object



83
84
85
# File 'lib/fragment_client.rb', line 83

def self.operation(name)
  FragmentQueries.const_get(name)
end

.operation_method_namesObject



101
102
103
# File 'lib/fragment_client.rb', line 101

def self.operation_method_names
  @operation_method_names ||= T.let([], T.nilable(T::Array[String]))
end

.operationsObject



116
117
118
119
# File 'lib/fragment_client.rb', line 116

def self.operations
  @operations ||= T.let({}, T.nilable(T::Hash[String,
                                              GraphQL::Language::Nodes::OperationDefinition]))
end

.parse_queries(filename) ⇒ Object



71
72
73
74
# File 'lib/fragment_client.rb', line 71

def self.parse_queries(filename)
  file_text = File.read(filename)
  Client.parse(file_text)
end

.record_document(source) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/fragment_client.rb', line 122

def self.record_document(source)
  GraphQL.parse(source).definitions.each do |definition|
    next unless definition.is_a?(GraphQL::Language::Nodes::OperationDefinition)

    name = definition.name
    operations[name] ||= definition if name
  end
end

.record_operations(queries) ⇒ Object



106
107
108
109
110
111
# File 'lib/fragment_client.rb', line 106

def self.record_operations(queries)
  queries.constants.each do |constant|
    name = method_name_for(constant.to_s)
    operation_method_names << name unless operation_method_names.include?(name)
  end
end

.reset_operations!Object



133
134
135
136
137
138
# File 'lib/fragment_client.rb', line 133

def self.reset_operations!
  operation_method_names.clear
  operations.clear
  record_operations(FragmentQueries)
  record_document(File.read("#{__dir__}/queries.graphql"))
end