Created
January 27, 2025 10:36
-
-
Save bschulz87/9de4a8dada0cdde9e5db918dd99496b5 to your computer and use it in GitHub Desktop.
Webiny DataStore Resolver
This file contains hidden or 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
import { util } from "@aws-appsync/utils"; | |
/** | |
* Sends a POST request to create a new user | |
* @param {import('@aws-appsync/utils').Context<{input: {id: string; name:string}}>} ctx the context | |
* @returns the request | |
*/ | |
export function request(ctx) { | |
return fetch("/graphql", { | |
method: "POST", | |
headers: { | |
"Content-Type": "application/json", | |
Authorization: `Bearer ${ctx.env.APPSYNC_WEBINY_MANAGE_API_KEY}`, | |
}, | |
body: { | |
query: `query Pages($limit: Int, $lastSync: AWSTimestamp, $nextToken: String){ | |
syncInfoPages(limit: $limit, lastSync: $lastSync, nextToken: $nextToken) { | |
items | |
nextToken | |
startedAt | |
} | |
}`, | |
operationName: "Pages", | |
variables: ctx.args, | |
}, | |
}); | |
} | |
/** | |
* Process the HTTP response | |
* @param {import('@aws-appsync/utils').Context} ctx the context | |
* @returns {*} the publish response | |
*/ | |
export function response(ctx) { | |
const { statusCode, body } = ctx.result; | |
util.appendError(body, statusCode); | |
if (statusCode === 200) { | |
const res = JSON.parse(body)["data"].syncInfoPages; | |
res.items = res.items.map((e) => { | |
const item = JSON.parse(e); | |
item["_lastChangedAt"] = util.time.parseISO8601ToEpochMilliSeconds( | |
item._lastChangedAt, | |
); | |
item.title = { | |
de: item.title, | |
}; | |
item.content = { | |
de: item.content, | |
}; | |
return item; | |
}); | |
return res; | |
} | |
// TODO: check body for graphql errors and append error if not empty | |
util.appendError(body, statusCode); | |
} | |
/** | |
* Sends an HTTP request | |
* @param {string} resourcePath path of the request | |
* @param {Object} [options] values to publish | |
* @param {'PUT' | 'POST' | 'GET' | 'DELETE' | 'PATCH'} [options.method] the request method | |
* @param {Object.<string, string>} [options.headers] the request headers | |
* @param {string | Object.<string, any>} [options.body] the request body | |
* @param {Object.<string, string>} [options.query] Key-value pairs that specify the query string | |
* @returns {import('@aws-appsync/utils').HTTPRequest} the request | |
*/ | |
function fetch(resourcePath, options) { | |
const { method = "GET", headers, body: _body, query } = options; | |
const body = typeof _body === "object" ? JSON.stringify(_body) : _body; | |
return { | |
resourcePath, | |
method, | |
params: { headers, query, body }, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment