-
Notifications
You must be signed in to change notification settings - Fork 940
/
appsyncRequest.ts
58 lines (49 loc) · 1.45 KB
/
appsyncRequest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as https from 'https'
import { URL } from 'url'
const AWS = require('aws-sdk')
import { HttpRequest, Endpoint } from 'aws-sdk'
const region = process.env.AWS_REGION!
export type QueryDetails = {
query: string
variables?: { [key: string]: any }
}
export interface GraphQLResult<T = object> {
data?: T
errors?: any[]
extensions?: { [key: string]: any }
}
/**
*
* @param {Object} queryDetails the query, operationName, and variables
* @param {String} appsyncUrl url of your AppSync API
* @param {String} apiKey the api key to include in headers. if null, will sign with SigV4
*/
const request = <T = object>(
queryDetails: QueryDetails,
appsyncUrl: string,
apiKey?: string
) => {
const endpoint = new URL(appsyncUrl).hostname
const req = new HttpRequest(new Endpoint(endpoint), region)
req.method = 'POST'
req.path = '/graphql'
req.headers.host = endpoint
req.headers['Content-Type'] = 'application/json'
req.body = JSON.stringify(queryDetails)
if (apiKey) {
req.headers['x-api-key'] = apiKey
} else {
const signer = new AWS.Signers.V4(req, 'appsync', true)
signer.addAuthorization(AWS.config.credentials, AWS.util.date.getDate())
}
return new Promise<GraphQLResult<T>>((resolve, reject) => {
const httpRequest = https.request({ ...req, host: endpoint }, (result) => {
result.on('data', (data) => {
resolve(JSON.parse(data.toString()))
})
})
httpRequest.write(req.body)
httpRequest.end()
})
}
export default request