The solution is to run multiple post using GRAPHQL by splitting the first function response and passing the value to the second function by dynamically created query of GRAPHQL.
mainQueries.ts
export const generalLandingPageBySlugQuery: string = `
query ($slug: String!, $isPreview: Boolean = false, $locale: String!) {
templateGeneralLandingPageCollection(where: {slug: $slug}, locale: $locale, preview: $isPreview, limit: 1) {
items {
title
slug
metadata {
...metadataFields
}
components: componentsCollection(limit: 10) {
items {
__typename
... on TextCardsComponent {
...textCardsFields
}
... on TwoColumnImageTextComponent {
...twoColumnImageTextFields
}
}
}
}
}
} ${metadataFields}
${components.twoColumnImageText}
${components.accordion}
`;
Fragments.ts
export const components = {
textCards: `fragment textCardsFields on TextCardsComponent {
rtHeading: heading {
json
}
backgroundColor
links: linksCollection {
items {
title
url
openInNewTab
}
}
}`,
twoColumnImageText: `
fragment twoColumnImageTextFields on TwoColumnImageTextComponent {
rtTitle:title {
json
}
variant
backgroundColor
rtHeading: heading {
json
}
rtBlurb: blurb {
json
}
cta {
title
url
openInNewTab
}
eyebrow
cardTitle
cardDescription
cardLink
image {
...assetFields
}
videoType
videoId
}`,
Angular Service.ts
Function one
generalLandingPageBySlug(slug: string) {
const queryVariables = JSON.stringify({
slug,
isPreview: this.isPreview,
locale: this.coreService.locale.code,
});
return this.http
.post<ContentfulApiResponse>( // REQUEST ONE
environment.local ? LOCAL_GRAPHQL : `${GRAPHQL}/general-lp-${slug}`,
{
query: Utils.compressGraphQl(generalLandingPageBySlugQuery),
variables: queryVariables,
}
)
.pipe(
map((response: ContentfulApiResponse) => {
this.typename =
response.data.templateGeneralLandingPageCollection?.items[0].components.items;
//Components Lists
const currentComponents = [
...new Map(
this.typename.map((obj: any) => [JSON.stringify(obj), obj])
).values(),
];
this.typename = currentComponents;
const choosenComponents = this.typename.map(function (typeName: {
__typename: any;
}) {
return typeName.__typename;
});
//Dynamic Query
const queryData =
'query ($slug: String!, $isPreview: Boolean = false, $locale: String!) {' +
'templateGeneralLandingPageCollection(where: {slug: $slug}, locale: $locale, preview: $isPreview, limit: 1) {' +
'items {' +
'title ' +
'slug ' +
'metadata {' +
'...metadataFields' +
'}' +
'components: componentsCollection(limit: 15) {' +
'items {' +
'__typename ' +
(choosenComponents.includes('TextCardsComponent')
? '... on TextCardsComponent {...textCardsFields}'
: '') +
(choosenComponents.includes('TwoColumnImageTextComponent')
? '... on TwoColumnImageTextComponent {...twoColumnImageTextFields}'
: '') +
'}' +
'}' +
'}' +
'}' +
'}' +
'fragment metadataFields on Metadata{title metaTitle keyword description facebookType image{...assetFields}canonical noIndex} ' +
(choosenComponents.includes('TextCardsComponent')
? 'fragment textCardsFields on TextCardsComponent{rtHeading:heading{json}backgroundColor links:linksCollection{items{title url openInNewTab}}}'
: '') +
(choosenComponents.includes('TwoColumnImageTextComponent')
? 'fragment twoColumnImageTextFields on TwoColumnImageTextComponent{rtTitle:title{json}variant backgroundColor rtHeading:heading{json}rtBlurb:blurb{json}cta{title url openInNewTab}eyebrow cardTitle cardDescription cardLink image{...assetFields}videoType videoId}'
: '') +
';
return queryData;
}),
mergeMap((payload) => {
return this.generalFinalData(payload, slug);
})
);
}
Function Two
generalFinalData(payload: string, slug: string) {
const queryVariables = JSON.stringify({
slug,
isPreview: this.isPreview,
locale: this.coreService.locale.code,
});
return this.http
.post<ContentfulApiResponse>( // REQUEST TWO
environment.local ? LOCAL_GRAPHQL : `${GRAPHQL}/general-lp-${slug}`,
{
query: Utils.compressGraphQl(payload),
variables: queryVariables,
}
)
.pipe(
map((response: ContentfulApiResponse) => {
let contentfulResponse: ContentfulResponse = {
__typename: '',
pageData: {},
components: [],
};
return this.hasError(
response,
response.data.templateGeneralLandingPageCollection
)
? contentfulResponse
: this.buildData(
this.unwrapResponse(
response.data.templateGeneralLandingPageCollection
),
contentfulResponse
);
})
);
}