Skip to content

Commit

Permalink
VACMS-17582: Rearchitects query parameters (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryguyk authored Mar 26, 2024
1 parent 9be5781 commit 85feac9
Show file tree
Hide file tree
Showing 21 changed files with 263 additions and 131 deletions.
57 changes: 10 additions & 47 deletions src/data/queries/alertSingle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { QueryFormatter } from 'next-drupal-query'
import { QueryFormatter, QueryParams } from 'next-drupal-query'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'
import { ParagraphAlertSingle } from '@/types/drupal/paragraph'
import {
AlertSingle,
Expand All @@ -8,52 +9,14 @@ import {
import { formatParagraph } from '@/lib/drupal/paragraphs'
import { queries } from '.'

// TODO:
//
// EDIT:
// It seems *maybe* that next-drupal-query is designed to handle this already.
// You can pass an `id` to queries.getParams().
// E.g.
// queries.getParams('paragraph--alert_single'))
//
// We don't need this here:
// export const params: QueryParams<null> = () => {
// return new DrupalJsonApiParams()
// .addInclude([
// 'field_alert_block_reference',
// 'field_alert_block_reference.field_alert_content',
// 'field_alert_non_reusable_ref',
// 'field_alert_non_reusable_ref.field_va_paragraphs',
// ])
// }
//
// ...but it seems like we might want to define
// paragraph-specific `includes` within the paragraph's
// query file (this file), and then somehow reference these in
// the node query when we need to include fields of a specific
// paragraph type.
//
// E.g.
// export const include = [
// 'field_alert_block_reference',
// 'field_alert_block_reference.field_alert_content',
// 'field_alert_non_reusable_ref',
// 'field_alert_non_reusable_ref.field_va_paragraphs',
// ]
//
// /* Some util file*/
// export const getIncludedSubFields = (
// parentField: string,
// subFields: string[]
// ) => [parentField, ...subFields.map((subField) => `${parentField}.${subField}`)]
//
// /* src/data/queries/resourcesSupport.ts */
// import { include } from '@/data/queries/alertSingle'
// export const params: QueryParams<null> = () => {
// return new DrupalJsonApiParams().addInclude([
// ...getIncludedSubFields('field_alert_single', include)
// ])
// }
export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
'field_alert_block_reference',
'field_alert_block_reference.field_alert_content',
'field_alert_non_reusable_ref',
'field_alert_non_reusable_ref.field_va_paragraphs',
])
}

export const formatter: QueryFormatter<ParagraphAlertSingle, AlertSingle> = (
entity: ParagraphAlertSingle
Expand Down
11 changes: 10 additions & 1 deletion src/data/queries/audienceTopics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// Define the query params for fetching node--news_story.
import { ParagraphAudienceTopics } from '@/types/drupal/paragraph'
import { QueryFormatter } from 'next-drupal-query'
import { QueryFormatter, QueryParams } from 'next-drupal-query'
import { AudienceTopic, AudienceTopics } from '@/types/formatted/audienceTopics'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'

export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
'field_audience_beneficiares',
'field_non_beneficiares',
'field_topics',
])
}

const getTagsList = (
entity: ParagraphAudienceTopics
Expand Down
8 changes: 7 additions & 1 deletion src/data/queries/benefitsHubLinks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { QueryFormatter } from 'next-drupal-query'
import { QueryFormatter, QueryParams } from 'next-drupal-query'
import { NodeLandingPage } from '@/types/drupal/node'
import { BenefitsHubLink } from '@/types/formatted/benefitsHub'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'

// Define the query params for fetching node--landing_page.
export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude(['field_support_services'])
}

// Format NodeLandingPage (Benefits Hub) into link teasers.
export const formatter: QueryFormatter<NodeLandingPage[], BenefitsHubLink[]> = (
Expand Down
15 changes: 14 additions & 1 deletion src/data/queries/collapsiblePanel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { ParagraphCollapsiblePanel } from '@/types/drupal/paragraph'
import { QueryFormatter } from 'next-drupal-query'
import { QueryFormatter, QueryParams } from 'next-drupal-query'
import { CollapsiblePanel } from '@/types/formatted/collapsiblePanel'
import { formatParagraph } from '@/lib/drupal/paragraphs'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'
import { getNestedIncludes } from '@/lib/utils/queries'
import { PARAGRAPH_RESOURCE_TYPES } from '@/lib/constants/resourceTypes'

// Define the query params for fetching paragraph--collapsible_panel.
export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
...getNestedIncludes(
'field_va_paragraphs',
PARAGRAPH_RESOURCE_TYPES.COLLAPSIBLE_PANEL_ITEM
),
])
}

export const formatter: QueryFormatter<
ParagraphCollapsiblePanel,
Expand Down
8 changes: 7 additions & 1 deletion src/data/queries/collapsiblePanelItem.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ParagraphCollapsiblePanelItem } from '@/types/drupal/paragraph'
import { QueryFormatter } from 'next-drupal-query'
import { QueryFormatter, QueryParams } from 'next-drupal-query'
import { CollapsiblePanelItem } from '@/types/formatted/collapsiblePanel'
import { formatParagraph } from '@/lib/drupal/paragraphs'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'

// Define the query params for fetching paragraph--collapsible_panel_item.
export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude(['field_va_paragraphs'])
}

export const formatter: QueryFormatter<
ParagraphCollapsiblePanelItem,
Expand Down
55 changes: 36 additions & 19 deletions src/data/queries/contactInfo.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
import { QueryFormatter } from 'next-drupal-query'
import { QueryFormatter, QueryParams } from 'next-drupal-query'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'
import { ParagraphContactInformation } from '@/types/drupal/paragraph'
import { ContactInfo, AdditionalContact } from '@/types/formatted/contactInfo'
import { queries } from '.'
import { formatParagraph } from '@/lib/drupal/paragraphs'
import { getNestedIncludes } from '@/lib/utils/queries'
import { RESOURCE_TYPES } from '@/lib/constants/resourceTypes'

// export const params: QueryParams<null> = () => {
// return new DrupalJsonApiParams().addInclude([
// 'field_additional_contact', // can be paragraph--phone_number or paragraph--email_contact
// 'field_contact_default',
// 'field_contact_default.field_office',
// 'field_benefit_hub_contacts',
// 'field_benefit_hub_contacts.field_support_services', // this is the additional contact info for a Benefit Hub, node--support_service
// ])
// }
export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
'field_additional_contact',
...getNestedIncludes(
'field_contact_default',
RESOURCE_TYPES.SUPPORT_SERVICES
),
...getNestedIncludes(
'field_benefit_hub_contacts',
RESOURCE_TYPES.BENEFITS_HUB
),
])
}

// paragraph--contact_information is essentially a wrapper paragraph around several other entity references
export const formatter: QueryFormatter<
Expand All @@ -24,17 +31,27 @@ export const formatter: QueryFormatter<
id: entity.id,
entityId: entity.drupal_internal__id,
contactType: entity.field_contact_info_switch as ContactInfo['contactType'],
defaultContact: {
title: entity.field_contact_default.title,
value: entity.field_contact_default.field_phone_number,
href: entity.field_contact_default.field_link.uri,
},
defaultContact: queries.formatData(
RESOURCE_TYPES.SUPPORT_SERVICES,
entity.field_contact_default
),
additionalContact: formatParagraph(
entity.field_additional_contact
) as AdditionalContact,
benefitHubContacts: queries.formatData(
'node--support_service',
entity.field_benefit_hub_contacts?.field_support_services
),

//TODO:
// This should likely be: `queries.formatData(RESOURCE_TYPES.BENEFITS_HUB, entity.field_benefit_hub_contacts)
// since `entity.field_benefit_hub_contacts` will be a reference to a Benefits Hub Landing Page,
// but the formatter for Benefits Hub Landing Pages is currently just a "partial"/"teaser" formatter
// that doesn't include everything we'd need. That likely needs to be broken into "full" and "teaser"
// files so we have a formatter for each.
//
// For now, we can drill down into `field_support_services` and pass that to the formatter for
// RESOURCE_TYPES.SUPPORT_SERVICE.
benefitHubContacts:
entity.field_benefit_hub_contacts?.field_support_services.map(
(supportService) =>
queries.formatData(RESOURCE_TYPES.SUPPORT_SERVICES, supportService)
),
}
}
4 changes: 2 additions & 2 deletions src/data/queries/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
fetchSingleEntityOrPreview,
} from '@/lib/drupal/query'
import { RESOURCE_TYPES } from '@/lib/constants/resourceTypes'
import { getNestedIncludes } from '@/lib/utils/queries'

export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
'field_media',
'field_media.image',
...getNestedIncludes('field_media', 'media--image'),
'field_listing',
'field_administration',
'field_facility_location',
Expand Down
7 changes: 6 additions & 1 deletion src/data/queries/featuredContent.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { QueryFormatter } from 'next-drupal-query'
import { QueryFormatter, QueryParams } from 'next-drupal-query'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'
import { ParagraphFeaturedContent } from '@/types/drupal/paragraph'
import { FeaturedContent } from '@/types/formatted/featuredContent'
import { queries } from '.'

export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude(['field_cta'])
}

export const formatter: QueryFormatter<
ParagraphFeaturedContent,
FeaturedContent
Expand Down
6 changes: 2 additions & 4 deletions src/data/queries/headerFooter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Menu, HeaderMegaMenu } from '@/types/drupal/menu'
import { HeaderFooterData } from '@/types/formatted/headerFooter'
import { buildHeaderFooterData } from '@/lib/utils/headerFooter'
import { getMenu } from '@/lib/drupal/query'
import { getNestedIncludes } from '@/lib/utils/queries'

export type RawHeaderFooterData = {
footerColumns: Menu
Expand All @@ -14,10 +15,7 @@ export type RawHeaderFooterData = {
// Define extra equery params for fetching header megamenu data. Include referenced promo block data, if present
export const megaMenuParams: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
'field_promo_reference',
'field_promo_reference.field_image',
'field_promo_reference.field_image.image',
'field_promo_reference.field_promo_link',
...getNestedIncludes('field_promo_reference', 'block_content--promo'),
])
}

Expand Down
9 changes: 8 additions & 1 deletion src/data/queries/healthServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import {
HealthService as FormattedHealthService,
HealthServices as FormattedHealthServices,
} from '@/types/formatted/healthServices'
import { QueryFormatter } from 'next-drupal-query'
import { QueryFormatter, QueryParams } from 'next-drupal-query'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'

export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
'field_service_name_and_descripti',
])
}

export const formatter: QueryFormatter<
FieldHealthServicesArray,
Expand Down
7 changes: 7 additions & 0 deletions src/data/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ export const QUERIES_MAP = {
'vamc-ehr': VamcEhr,
}

// All resource types that have a `params` function defined
export type ParamsType = {
[K in keyof typeof QUERIES_MAP]: 'params' extends keyof (typeof QUERIES_MAP)[K]
? K
: never
}[keyof typeof QUERIES_MAP]

// All resource types that have a `data` function defined
export type QueryableType = {
[K in keyof typeof QUERIES_MAP]: 'data' extends keyof (typeof QUERIES_MAP)[K]
Expand Down
4 changes: 2 additions & 2 deletions src/data/queries/newsStory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
fetchSingleEntityOrPreview,
} from '@/lib/drupal/query'
import { RESOURCE_TYPES } from '@/lib/constants/resourceTypes'
import { getNestedIncludes } from '@/lib/utils/queries'

// Define the query params for fetching node--news_story.
export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
'field_media',
'field_media.image',
...getNestedIncludes('field_media', 'media--image'),
'field_author',
'field_listing',
'field_administration',
Expand Down
4 changes: 2 additions & 2 deletions src/data/queries/newsStoryTeaser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { queries } from '.'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'
import { NodeNewsStory } from '@/types/drupal/node'
import { NewsStoryTeaser } from '@/types/formatted/newsStory'
import { getNestedIncludes } from '@/lib/utils/queries'

// Define the query params for fetching node--news_story.
export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
'field_media',
'field_media.image',
...getNestedIncludes('field_media', 'media--image'),
'field_listing',
])
}
Expand Down
4 changes: 2 additions & 2 deletions src/data/queries/promoBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { DrupalJsonApiParams } from 'drupal-jsonapi-params'
import { drupalClient } from '@/lib/drupal/drupalClient'
import { BlockPromo } from '@/types/drupal/block'
import { MegaMenuPromoColumn } from '@/types/formatted/headerFooter'
import { getNestedIncludes } from '@/lib/utils/queries'

// Define the query params for fetching block_content--promo.
export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
'field_image',
'field_image.image',
...getNestedIncludes('field_image', 'media--image'),
'field_promo_link',
])
}
Expand Down
12 changes: 11 additions & 1 deletion src/data/queries/qaGroup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { QueryFormatter } from 'next-drupal-query'
import { QueryFormatter, QueryParams } from 'next-drupal-query'
import { ParagraphQaGroup } from '@/types/drupal/paragraph'
import { QaGroup } from '@/types/formatted/qaGroup'
import { QaGroupQa } from '@/types/formatted/qaGroup'
import { formatParagraph } from '@/lib/drupal/paragraphs'
import { DrupalJsonApiParams } from 'drupal-jsonapi-params'
import { getNestedIncludes } from '@/lib/utils/queries'
import { RESOURCE_TYPES } from '@/lib/constants/resourceTypes'

// Define the query params for fetching paragraph--q_a_group.
export const params: QueryParams<null> = () => {
return new DrupalJsonApiParams().addInclude([
...getNestedIncludes('field_q_as', RESOURCE_TYPES.QA),
])
}

export const formatter: QueryFormatter<ParagraphQaGroup, QaGroup> = (
entity: ParagraphQaGroup
Expand Down
Loading

0 comments on commit 85feac9

Please sign in to comment.