From 16aa0f02e9c54a9c77b2fa17c40baf41e280fd7c Mon Sep 17 00:00:00 2001 From: Tyrone Tse Date: Wed, 10 Apr 2024 16:51:55 -0500 Subject: [PATCH] Added function export async function graphqlFilterOnMarketingInitiative(marketingInitiative) Which calls the persisted query gmo/filter-on-marketing-initiative with example parameters { "marketingInitiative": "FY24Q1-Q2_AdobeExpress_level Up" } https://author-p108396-e1046543.adobeaemcloud.com/graphql/execute.json/gmo/filter-on-marketing-initiative%3BmarketingInitiative=FY24Q1-Q2_AdobeExpress_level%20Up --- scripts/graphql.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/scripts/graphql.js b/scripts/graphql.js index 4da06be6..b1da6493 100644 --- a/scripts/graphql.js +++ b/scripts/graphql.js @@ -58,6 +58,37 @@ export async function graphqlCampaignByName(campaignName) { }); } +export async function graphqlFilterOnMarketingInitiative(marketingInitiative) { + + const baseApiUrl = `${await getGraphqlEndpoint()}/graphql/execute.json`; + const projectId = 'gmo'; + const queryName = 'filter-on-marketing-initiative'; + const encodedMarketingInitiative = encodeURIComponent(marketingInitiative); + const encodedSemiColon = encodeURIComponent(';'); + //persisted query URLs have to be encoded together with the first semicolon + const graphqlEndpoint = `${baseApiUrl}/${projectId}/${queryName}${encodedSemiColon}marketingInitiative=${encodedMarketingInitiative}`; + const jwtToken = await getBearerToken(); + + // Return the fetch promise chain so that it can be awaited outside + return fetch(graphqlEndpoint, { + method: 'GET', + headers: { + Authorization: jwtToken, + }, + }).then(response => { + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + return response.json(); + }).then(data => { + return data; // Make sure to return the data so that the promise resolves with it + }).catch(error => { + console.error('Error fetching data: ', error); + throw error; // Rethrow or handle error as appropriate + }); +} + + async function getGraphqlEndpoint() { const result = await getAdminConfig();