-
Notifications
You must be signed in to change notification settings - Fork 1
/
schemaTest.js
102 lines (92 loc) · 2.77 KB
/
schemaTest.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
const { loadServerlessConfig } = require('./loadServerlessConfig');
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const uuid = require('uuid/v4');
const { createSchema: createSchemaCore } = require('./schema');
const { PubSub } = require('graphql-subscriptions');
const { wrapSchema } = require('./schemaWrapper');
const { cloudFormationProcessor } = require('./cloudFormationProcessor');
const createDBNames = async (serverlessConfig, dynamodb) => {
const {
resources: { Resources: resources },
} = serverlessConfig;
const resourceTables = Object.values(resources)
.filter(resource => resource.Type === 'AWS::DynamoDB::Table')
.reduce(
(sum, resource) => ({
...sum,
[resource.Properties.TableName]: resource,
}),
{},
);
const list = await Promise.all(
Object.values(resourceTables).map(async ({ Properties: props }) => {
const testName = uuid();
const tableConfig = {
...props,
TableName: testName,
};
await dynamodb.createTable(tableConfig).promise();
return { testName, tableName: props.TableName };
}),
);
return list.reduce(
(sum, { testName, tableName }) => ({
...sum,
[tableName]: testName,
}),
{},
);
};
const createSchema = async ({
serverless,
schemaPath = null,
dynamodb,
} = {}) => {
const {
config: serverlessConfig,
directory: serverlessDirectory,
} = await loadServerlessConfig(serverless);
assert(serverlessConfig, 'must have config');
assert(serverlessDirectory, 'must have serverless directory');
// 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 dynamodbTables = await createDBNames(
// process serverless config through CF in case we're using some to craft table names.
cloudFormationProcessor(serverlessConfig, { dynamodbTables: {} }),
dynamodb,
);
// process serverless config again with our aliases.
const cfConfig = cloudFormationProcessor(serverlessConfig, {
dynamodbTables,
});
const pubsub = new PubSub();
const { schema, subscriptions } = await createSchemaCore({
dynamodb,
dynamodbTables,
graphqlSchema,
serverlessDirectory,
serverlessConfig: cfConfig,
pubsub,
});
const close = async () =>
Promise.all(
Object.values(dynamodbTables).map(async table =>
dynamodb.deleteTable({ TableName: table }).promise(),
),
);
return {
schema,
dynamodb,
pubsub,
subscriptions,
tables: dynamodbTables,
close,
};
};
module.exports = { createSchema };