-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding an api endpoint to wrap the graphql crawling query so I can pa…
…ss the page cursor as a url parameter. This is because Yext's connectors can't support graphql variables natively (adding variables to the request body from the crawler).
- Loading branch information
Showing
1 changed file
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
type ResponseData = { | ||
message: string | ||
} | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<ResponseData> | ||
) { | ||
try { | ||
if (!req) { | ||
return; | ||
} | ||
const cursor = req.query['cursor']; | ||
console.log('Cursor param=' + cursor); | ||
|
||
const simpleQuery= ` | ||
query SimpleQuery{ | ||
layout(site: "suncoast", routePath: "/", language: "en") { | ||
item { | ||
rendered | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const crawlQuery = ` | ||
query YextSiteCrawl( | ||
$numResults: Int | ||
$after: String | ||
$rootItem: String! | ||
$hasLayout: String! | ||
$noIndex: Int | ||
) { | ||
search( | ||
where: { | ||
AND: [ | ||
{ name: "_path", value: $rootItem, operator: EQ } | ||
{ name: "_hasLayout", value: $hasLayout } | ||
{ name: "noIndex", value: $noIndex, operator: NEQ } | ||
] | ||
} | ||
first: $numResults | ||
after: $after | ||
) { | ||
total | ||
pageInfo { | ||
endCursor | ||
hasNext | ||
} | ||
results { | ||
id | ||
name | ||
path | ||
url { | ||
path | ||
url | ||
} | ||
fields { | ||
name | ||
jsonValue | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const headers = { | ||
'content-type': 'application/json', | ||
'sc_apikey': process.env.SUNCOAST_DEV_API_KEY ?? '' | ||
}; | ||
console.log('api key=' + process.env.SUNCOAST_DEV_API_KEY) | ||
console.log('Header ' + JSON.stringify(headers)); | ||
|
||
const requestBody = { | ||
query: crawlQuery, | ||
variables: { | ||
"numResults" : 10, | ||
"after" : cursor ?? "", | ||
"rootItem": "{1E411842-1C89-4016-AEF2-EF80CCFABFE6}", | ||
"hasLayout": "true", | ||
"noIndex": 1 | ||
} | ||
}; | ||
console.log('Request Body '+ JSON.stringify(requestBody)); | ||
const options = { | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify(requestBody) | ||
}; | ||
/* | ||
const testResp = await (await fetch('https://edge.sitecorecloud.io/api/graphql/v1', options)); | ||
console.log('status= ' + testResp.status); | ||
console.log('response= '+ JSON.stringify(testResp.json())); | ||
*/ | ||
const response = await (await fetch('https://edge.sitecorecloud.io/api/graphql/v1', options)).json(); | ||
console.log('RESPONSE FROM FETCH REQUEST', response?.data); | ||
res.status(200).json(response?.data); | ||
} | ||
catch (err) { | ||
console.log('ERROR DURING FETCH REQUEST', err); | ||
} | ||
finally { | ||
|
||
} | ||
}; |