|
| 1 | +Resources: |
| 2 | + Api: |
| 3 | + Type: AWS::AppSync::GraphQLApi |
| 4 | + Properties: |
| 5 | + Name: Api |
| 6 | + AuthenticationType: AWS_LAMBDA |
| 7 | + LambdaAuthorizerConfig: |
| 8 | + AuthorizerUri: !GetAtt Authorizer.Arn |
| 9 | + |
| 10 | + ApiSchema: |
| 11 | + Type: AWS::AppSync::GraphQLSchema |
| 12 | + Properties: |
| 13 | + ApiId: !GetAtt Api.ApiId |
| 14 | + Definition: | |
| 15 | + type Query { |
| 16 | + sayHello: String! |
| 17 | + } |
| 18 | + schema { |
| 19 | + query: Query |
| 20 | + } |
| 21 | +
|
| 22 | + NoneDataSource: |
| 23 | + Type: AWS::AppSync::DataSource |
| 24 | + Properties: |
| 25 | + Type: NONE |
| 26 | + ApiId: !GetAtt Api.ApiId |
| 27 | + Name: NoneDataSource |
| 28 | + |
| 29 | + SayHelloResolver: |
| 30 | + DependsOn: ApiSchema |
| 31 | + Type: AWS::AppSync::Resolver |
| 32 | + Properties: |
| 33 | + ApiId: !GetAtt Api.ApiId |
| 34 | + TypeName: Query |
| 35 | + FieldName: sayHello |
| 36 | + Kind: PIPELINE |
| 37 | + PipelineConfig: |
| 38 | + Functions: |
| 39 | + - !GetAtt SayHelloFunc.FunctionId |
| 40 | + Code: | |
| 41 | + export function request(ctx) { |
| 42 | + return {}; |
| 43 | + } |
| 44 | +
|
| 45 | + export function response(ctx) { |
| 46 | + return ctx.prev.result; |
| 47 | + } |
| 48 | + Runtime: |
| 49 | + Name: APPSYNC_JS |
| 50 | + RuntimeVersion: 1.0.0 |
| 51 | + |
| 52 | + SayHelloFunc: |
| 53 | + Type: AWS::AppSync::FunctionConfiguration |
| 54 | + Properties: |
| 55 | + ApiId: !GetAtt Api.ApiId |
| 56 | + Name: SayHelloFunc |
| 57 | + DataSourceName: !GetAtt NoneDataSource.Name |
| 58 | + Code: | |
| 59 | + export function request(ctx) { |
| 60 | + return {}; |
| 61 | + } |
| 62 | +
|
| 63 | + export function response(ctx) { |
| 64 | + return "Hello World"; |
| 65 | + } |
| 66 | + Runtime: |
| 67 | + Name: APPSYNC_JS |
| 68 | + RuntimeVersion: 1.0.0 |
| 69 | + |
| 70 | + GraphQlApiToLambdaConnector: |
| 71 | + Type: AWS::Serverless::Connector |
| 72 | + Properties: |
| 73 | + Source: |
| 74 | + Id: Api |
| 75 | + Destination: |
| 76 | + Id: Authorizer |
| 77 | + Permissions: |
| 78 | + - Write |
| 79 | + |
| 80 | + Authorizer: |
| 81 | + Type: AWS::Serverless::Function |
| 82 | + Properties: |
| 83 | + InlineCode: | |
| 84 | + exports.handler = async (_) => { |
| 85 | + return { |
| 86 | + isAuthorized: true, |
| 87 | + deniedFields: [], |
| 88 | + } |
| 89 | + } |
| 90 | + PackageType: Zip |
| 91 | + Runtime: nodejs14.x |
| 92 | + Handler: index.handler |
| 93 | + |
| 94 | + TriggerFunction: |
| 95 | + Type: AWS::Serverless::Function |
| 96 | + Properties: |
| 97 | + Environment: |
| 98 | + Variables: |
| 99 | + GRAPHQL_URL: !GetAtt Api.GraphQLUrl |
| 100 | + Runtime: nodejs14.x |
| 101 | + Handler: index.handler |
| 102 | + InlineCode: | |
| 103 | + const https = require("https"); |
| 104 | +
|
| 105 | + exports.handler = async (_) => { |
| 106 | + const queries = { |
| 107 | + sayHello: /* GraphQL */ ` |
| 108 | + query { |
| 109 | + sayHello |
| 110 | + } |
| 111 | + `, |
| 112 | + }; |
| 113 | +
|
| 114 | +
|
| 115 | + const fetch = async (url, options) => |
| 116 | + new Promise((resolve, reject) => { |
| 117 | + const req = https.request(url, options, (res) => { |
| 118 | + const body = []; |
| 119 | + res.on("data", (chunk) => body.push(chunk)); |
| 120 | + res.on("end", () => { |
| 121 | + const resString = Buffer.concat(body).toString(); |
| 122 | + resolve(resString); |
| 123 | + }); |
| 124 | + }); |
| 125 | +
|
| 126 | + req.on("error", (err) => { |
| 127 | + reject(err); |
| 128 | + }); |
| 129 | +
|
| 130 | + req.on("timeout", () => { |
| 131 | + req.destroy(); |
| 132 | + reject(new Error("Request time out")); |
| 133 | + }); |
| 134 | +
|
| 135 | + req.write(options.body); |
| 136 | + req.end(); |
| 137 | + }); |
| 138 | +
|
| 139 | +
|
| 140 | + const makeRequest = async (queryName) => { |
| 141 | + const options = { |
| 142 | + method: "POST", |
| 143 | + headers: { |
| 144 | + "Authorization": "n'importe quoi", |
| 145 | + }, |
| 146 | + body: JSON.stringify({ query: queries[queryName] }), |
| 147 | + timeout: 10000, // ms |
| 148 | + }; |
| 149 | +
|
| 150 | + const response = await fetch(process.env.GRAPHQL_URL, options); |
| 151 | + const body = JSON.parse(response); |
| 152 | + const data = body.data?.[queryName]; |
| 153 | +
|
| 154 | + if (body.errors !== undefined) { |
| 155 | + throw JSON.stringify(body.errors); |
| 156 | + } |
| 157 | + |
| 158 | + if (data !== "Hello World") { |
| 159 | + throw new Error(`${queryName} error: '${data}' must be 'Hello World'`); |
| 160 | + } |
| 161 | +
|
| 162 | + return body.data; |
| 163 | + }; |
| 164 | +
|
| 165 | +
|
| 166 | + return await makeRequest("sayHello"); |
| 167 | + }; |
| 168 | +Metadata: |
| 169 | + SamTransformTest: true |
0 commit comments