-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
122 lines (110 loc) · 3.29 KB
/
server.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const { findServerlessPath } = require('@conduitvc/aws-utils/findServerless');
const { loadServerlessConfig } = require('./loadServerlessConfig');
const { PubSub } = require('graphql-subscriptions');
const fs = require('fs');
const path = require('path');
const { createSchema: createSchemaCore } = require('./schema');
const createServerCore = require('./serverCore');
const log = require('logdown')('appsync-emulator:server');
const { wrapSchema } = require('./schemaWrapper');
const { cloudFormationProcessor } = require('./cloudFormationProcessor');
const {
getAppSyncConfig,
flatteningMappingTemplatesAndDataSources,
} = require('./util');
const ensureDynamodbTables = async (
dynamodb,
serverlessConfig,
appSyncConfig,
) => {
const { dataSources } = appSyncConfig;
const { resources: { Resources: resources = {} } = {} } = serverlessConfig;
await Promise.all(
Object.values(resources)
.filter(resource => resource.Type === 'AWS::DynamoDB::Table')
.map(async resource => {
const { Properties: params } = resource;
try {
log.info('creating table', params);
await dynamodb.createTable(params).promise();
} catch (err) {
if (err.code !== 'ResourceInUseException') throw err;
}
}),
);
return dataSources.filter(source => source.type === 'AMAZON_DYNAMODB').reduce(
(sum, source) => ({
...sum,
[source.config.tableName]: source.config.tableName,
}),
{},
);
};
const createSchema = async ({
serverless,
schemaPath = null,
pubsub,
dynamodb,
} = {}) => {
let serverlessConfig = {};
let serverlessDirectory;
if (typeof serverless === 'object') {
serverlessConfig = serverless.service;
const appSyncConfig = getAppSyncConfig(serverlessConfig);
flatteningMappingTemplatesAndDataSources(appSyncConfig);
serverlessConfig.appSyncConfig = appSyncConfig;
serverlessDirectory = serverless.config.servicePath;
} else {
serverlessDirectory =
typeof serverless === 'string'
? path.dirname(serverless)
: findServerlessPath();
const config = await loadServerlessConfig(serverlessDirectory);
serverlessConfig = config.config;
}
const cfConfig = cloudFormationProcessor(serverlessConfig, {
// we do not use aliases for the dynamodb tables in server like we do in testing.
dynamodbTables: {},
});
// eslint-disable-next-line
schemaPath = schemaPath || path.join(serverlessDirectory, 'schema.graphql');
if (!fs.existsSync(schemaPath)) {
throw new Error(`schema file: ${schemaPath} must exist`);
}
const graphqlSchema = wrapSchema(fs.readFileSync(schemaPath, 'utf8'));
const appSyncConfig = getAppSyncConfig(cfConfig);
const dynamodbTables = await ensureDynamodbTables(
dynamodb,
cfConfig,
appSyncConfig,
);
return createSchemaCore({
dynamodb,
dynamodbTables,
graphqlSchema,
serverlessDirectory,
serverlessConfig: cfConfig,
pubsub,
});
};
const createServer = async ({
wsPort,
port,
dynamodb,
...createSchemaOpts
}) => {
const pubsub = new PubSub();
const { schema, subscriptions } = await createSchema({
...createSchemaOpts,
dynamodb,
pubsub,
});
return createServerCore({
wsPort,
port,
pubsub,
schema,
subscriptions,
});
};
module.exports = createServer;