Class: Ecoportal::API::GraphQL::Compat::Pages

Inherits:
Object
  • Object
show all
Defined in:
lib/ecoportal/api/graphql/compat/pages.rb

Overview

v2-compatible pages API backed by GraphQL. Exposes the same interface as ecoportal-api-v2's pages object so that existing eco-helpers scripts work without modification.

Key difference from v2:

  • get_new / create is a 2-step GraphQL workflow (build then create)
  • update requires patchVer — must always fetch the page first
  • search is via the GraphQL org search (register_filter + searchConf)

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Pages

Returns a new instance of Pages.



14
15
16
# File 'lib/ecoportal/api/graphql/compat/pages.rb', line 14

def initialize(client)
  @client = client
end

Instance Method Details

#create(page, from: nil) ⇒ Object

Create a page from a filled draft. Serialises any modified data fields from the draft into dataFields.updates. Returns a PageReference with .page_id and .active_stage_id.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ecoportal/api/graphql/compat/pages.rb', line 54

def create(page, from: nil)
  template_id   = source_template_id(page, from)
  field_updates = page.respond_to?(:data_fields_updates) ? page.data_fields_updates : []

  # dataFields is a required (non-null) field on CreateFromTemplateInput — always
  # send it, with an empty updates list when the draft has no field changes.
  input = { templateId: template_id, showHiddenData: true, dataFields: { updates: field_updates } }

  mutation = Mutation::Page::CreateFromTemplate.new(@client)
  payload  = mutation.query(input: input)
  resp     = Response.new(payload)
  return PageReference.new(payload.item) if resp.success?

  resp
end

#each(search_conf: {}, first: 50, &block) ⇒ Object

Cursor-paginated iteration over pages matching searchConf. Yields full PageUnion objects (with patchVer and field IDs). Required for update workflows — register search returns PreviewPage only.

Usage: api.pages.each(searchConf: SearchConf[:state].is(:active).to_h, first: 50) do |page| # page is a Model::Page::Basic or Model::Page::Phased end



113
114
115
# File 'lib/ecoportal/api/graphql/compat/pages.rb', line 113

def each(search_conf: {}, first: 50, &block)
  Query::Pages.new(@client).each(searchConf: search_conf, first: first, &block)
end

#get(id, stage_id: nil) ⇒ Object

Fetch a page by ID with full data — metadata, stages, sections, data fields. When stage_id is provided, wraps the result in a StageView that filters sections and components to that stage only — matching v2 per-stage behaviour.



21
22
23
24
25
26
# File 'lib/ecoportal/api/graphql/compat/pages.rb', line 21

def get(id, stage_id: nil)
  page = Query::Page.new(@client).query(id: id) do
    spread :CommonPageUnion
  end
  stage_id ? StageView.new(page, stage_id) : page
end

#get_body(page) ⇒ Object

Returns the pending changes on a page wrapped under the 'page' key. v2 equivalent: DocHelpers#get_body(doc, level: 'page') → { 'page' => ... }. eco-helpers' OozeBaseCase reads patch_doc(ooze)'page', so the body must be wrapped to match the v2 contract — otherwise ['page'] is always nil → diff never prints and "No changes to update" misfires.

CRITICAL: the body is the SAME input #update/#create would send — NOT page.as_update. On a phased page, as_update only carries page-SCALAR changes; data-field edits (the bulk of what scripts do — set/select/add-reference) live in data_fields_updates and are invisible to as_update. Reading as_update here made the dry-run blind to every field change: toocs coded a Breakdown Agency reference yet the preview showed "No changes to update", while a real run (made dirty by submit!) DID patch it. Build the faithful body via the Input serialisers so the dry-run shows exactly what will be written. nil when there is genuinely nothing to send (and no submit/sign-off pending).



101
102
103
# File 'lib/ecoportal/api/graphql/compat/pages.rb', line 101

def get_body(page)
  { 'page' => patch_body(page) }
end

#get_new(template_id) ⇒ Object

Build a draft page from a template. Returns the built PageUnion with server-assigned field IDs and structure. This is step (a) of the 2-step creation sequence: (a) get_new → inspect/modify field values → (b) create



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ecoportal/api/graphql/compat/pages.rb', line 32

def get_new(template_id)
  mutation = Mutation::Page::BuildFromTemplate.new(@client)
  payload  = mutation.query(input: { templateId: template_id, showHiddenData: true }) do
    clientMutationId
    errors {
      details
      fullMessages
    }
    item { spread :CommonPageUnion }
  end
  # Record the source template on the built draft: the read fragment does not fetch
  # sourceTemplateId, and a built draft carries a (real) patchVer, so without this
  # marker the dry-run can neither detect the draft nor recover its templateId — it
  # would mis-preview a CREATE as an UPDATE. See #new_draft? / #create_body.
  payload.item&.tap do |draft|
    draft._compat_source_template_id = template_id if draft.respond_to?(:_compat_source_template_id=)
  end
end

#update(page, **kargs) ⇒ Object

Update a page. Builds the mutation input from the page model diff. Returns a Compat::Response with .success? and .status. Returns nil (no-op) if there are no changes.

When called with no explicit operation (the base eco-helpers update_ooze path does pages.update(page)), any captured submit!/sign_off! intent on the page is folded in so the SINGLE updatePage carries both the dirty fields and the submit.



77
78
79
80
81
82
83
84
# File 'lib/ecoportal/api/graphql/compat/pages.rb', line 77

def update(page, **kargs)
  kargs = compat_submit_kargs(page) if kargs.empty?
  input = Input::Page::Update.from_model(page, **kargs)
  return nil if input.nil?

  payload = Builder::Page.new(@client).update(input: input)
  Response.new(payload)
end