Skip to content

Commit a947383

Browse files
committed
Added S3 PutObjectRequest config option
1 parent c990f7c commit a947383

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

packages/aws-appsync/src/client.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { createRetryLink } from './link/retry-link';
3535
import { boundEnqueueDeltaSync, buildSync, DELTASYNC_KEY, hashForOptions } from "./deltaSync";
3636
import { Subscription } from 'apollo-client/util/Observable';
3737
import { CONTROL_EVENTS_KEY } from './link/subscription-handshake-link';
38+
import { S3Config } from "./link/complex-object-link";
3839

3940
export { defaultDataIdFromObject };
4041

@@ -77,18 +78,20 @@ export const createAppSyncLink = ({
7778
complexObjectsCredentials,
7879
resultsFetcherLink = createHttpLink({ uri: url }),
7980
conflictResolver,
81+
s3Config
8082
}: {
8183
url: string,
8284
region: string,
8385
auth: AuthOptions,
8486
complexObjectsCredentials: CredentialsGetter,
8587
resultsFetcherLink?: ApolloLink,
8688
conflictResolver?: ConflictResolver,
89+
s3Config?: S3Config,
8790
}) => {
8891
const link = ApolloLink.from([
8992
createLinkWithStore((store) => new OfflineLink(store)),
9093
new ConflictResolutionLink(conflictResolver),
91-
new ComplexObjectLink(complexObjectsCredentials),
94+
new ComplexObjectLink(complexObjectsCredentials, s3Config),
9295
createRetryLink(ApolloLink.from([
9396
createAuthLink({ url, region, auth }),
9497
createSubscriptionHandshakeLink(url, resultsFetcherLink)
@@ -135,6 +138,7 @@ export interface AWSAppSyncClientOptions {
135138
cacheOptions?: ApolloReducerConfig,
136139
disableOffline?: boolean,
137140
offlineConfig?: OfflineConfig,
141+
s3Config?: S3Config,
138142
}
139143

140144
export interface OfflineConfig {
@@ -181,6 +185,7 @@ class AWSAppSyncClient<TCacheShape extends NormalizedCacheObject> extends Apollo
181185
callback = () => { },
182186
storeCacheRootMutation = false,
183187
} = {},
188+
s3Config = {}
184189
}: AWSAppSyncClientOptions, options?: Partial<ApolloClientOptions<TCacheShape>>) {
185190
const { cache: customCache = undefined, link: customLink = undefined } = options || {};
186191

@@ -218,7 +223,7 @@ class AWSAppSyncClient<TCacheShape extends NormalizedCacheObject> extends Apollo
218223
};
219224
});
220225
});
221-
const link = waitForRehydrationLink.concat(customLink || createAppSyncLink({ url, region, auth, complexObjectsCredentials, conflictResolver }));
226+
const link = waitForRehydrationLink.concat(customLink || createAppSyncLink({ url, region, auth, complexObjectsCredentials, conflictResolver , s3Config}));
222227

223228
const newOptions = {
224229
...options,

packages/aws-appsync/src/link/complex-object-link-uploader.native.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
import * as S3 from 'aws-sdk/clients/s3';
1010

11-
export default (fileField, { credentials }) => {
11+
export default (fileField, { credentials, s3Config }) => {
1212
const {
1313
bucket: Bucket,
1414
key: Key,
@@ -22,10 +22,16 @@ export default (fileField, { credentials }) => {
2222
region,
2323
});
2424

25-
return s3.upload({
25+
let params: S3.PutObjectRequest = {
2626
Bucket,
2727
Key,
2828
Body,
2929
ContentType,
30-
}).promise();
30+
};
31+
32+
if (s3Config.modifyPutObjectRequest != null) {
33+
params = s3Config.modifyPutObjectRequest(params);
34+
}
35+
36+
return s3.upload(params).promise();
3137
};

packages/aws-appsync/src/link/complex-object-link-uploader.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99
import * as S3 from 'aws-sdk/clients/s3';
1010

11-
export default (fileField, { credentials }) => {
11+
export default (fileField, { credentials, s3Config }) => {
1212
const {
1313
bucket: Bucket,
1414
key: Key,
@@ -22,10 +22,16 @@ export default (fileField, { credentials }) => {
2222
region,
2323
});
2424

25-
return s3.upload({
25+
let params: S3.PutObjectRequest = {
2626
Bucket,
2727
Key,
2828
Body,
2929
ContentType,
30-
}).promise();
30+
};
31+
32+
if (s3Config.modifyPutObjectRequest != null) {
33+
params = s3Config.modifyPutObjectRequest(params);
34+
}
35+
36+
return s3.upload(params).promise();
3137
};

packages/aws-appsync/src/link/complex-object-link.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,30 @@ import { ExecutionResult, GraphQLError } from 'graphql';
1414

1515
import upload from "./complex-object-link-uploader";
1616
import { AWSAppsyncGraphQLError } from '../types';
17+
import { S3 } from "aws-sdk";
18+
19+
export interface S3Config{
20+
modifyPutObjectRequest?: (request: S3.PutObjectRequest) => S3.PutObjectRequest
21+
}
1722

1823
export class ComplexObjectLink extends ApolloLink {
1924

2025
private link: ApolloLink;
2126

22-
constructor(credentials) {
27+
constructor(credentials, s3Config = null) {
2328
super();
2429

25-
this.link = complexObjectLink(credentials);
30+
this.link = complexObjectLink(credentials, s3Config);
2631
}
2732

2833
request(operation, forward) {
2934
return this.link.request(operation, forward);
3035
}
3136
}
3237

33-
export const complexObjectLink = (credentials) => {
38+
39+
40+
export const complexObjectLink = (credentials, s3Config = null) => {
3441
return new ApolloLink((operation, forward) => {
3542
return new Observable(observer => {
3643
let handle;
@@ -46,7 +53,7 @@ export const complexObjectLink = (credentials) => {
4653

4754
uploadPromise = Promise.resolve(uploadCredentials)
4855
.then(credentials => {
49-
const uploadPromises = Object.entries(objectsToUpload).map(([_, fileField]) => upload(fileField, { credentials }));
56+
const uploadPromises = Object.entries(objectsToUpload).map(([_, fileField]) => upload(fileField, { credentials, s3Config }));
5057

5158
return Promise.all([operation, ...uploadPromises] as Promise<any>[]);
5259
})

0 commit comments

Comments
 (0)