-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
58 lines (45 loc) · 1.83 KB
/
Copy pathindex.js
File metadata and controls
58 lines (45 loc) · 1.83 KB
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
const path = require('path');
const { config } = require('../config');
const { introspectionFromSchema } = require('graphql');
const { loadSchema } = require('@graphql-tools/load');
const { UrlLoader } = require('@graphql-tools/url-loader');
const { mergeTypeDefs } = require('@graphql-tools/merge');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { writeFileSync } = require('mz/fs');
const pkg = require('../package.json');
const fetch = require('node-fetch'); // Ensure this is installed
const graphqlApis = `https://${config.host}/stacks/${config.apiKey}?environment=${config.environment}`;
const limit = 100;
console.log('Loading Contentstack Schema download...');
const fetchSchemas = async () => {
const schemas = [];
let skip = 0;
while (skip < config.contentTypes) {
const currentFetchCount = Math.min(skip + limit, config.contentTypes);
console.log(`Fetching ${currentFetchCount} of ${config.contentTypes} Content Types.`);
const url = `${graphqlApis}&limit=${limit}&skip=${skip}`;
const remoteSchema = await loadSchema(url, {
headers: {
access_token: config.deliveryToken,
'X-User-Agent': `${pkg.name}/${pkg.version}`
},
fetch,
loaders: [new UrlLoader()]
});
schemas.push(remoteSchema);
skip += limit;
}
console.log(`${config.contentTypes} Content Types schema loaded.`);
const mergedTypeDefs = mergeTypeDefs(schemas);
const schema = makeExecutableSchema({ typeDefs: mergedTypeDefs });
const pathForSchema = path.join(__dirname, `${config.fileName}`);
console.log(`Saving schema to ${pathForSchema}`);
writeFileSync(
pathForSchema,
JSON.stringify(introspectionFromSchema(schema), null, 2)
);
console.log('Schema saved successfully.');
};
fetchSchemas().catch((err) => {
console.error('Failed to fetch schemas:', err);
});