@shopify/api-codegen-preset
Version:
Preset for graphql-codegen to parse and type queries to Shopify APIs
57 lines (53 loc) • 1.91 kB
JavaScript
;
var types = require('../types.js');
var apiConfigs = require('./api-configs.js');
function getSchemaData(outputDir, apiType, values) {
switch (apiType) {
case types.ApiType.Customer:
if (!values.apiKey) {
throw new Error('The customer API requires an API key');
}
return getCustomerApiSchema(outputDir, values);
default:
return getSchema(apiType, outputDir, values);
}
}
function getSchema(apiType, outputDir, values) {
const config = apiConfigs.apiConfigs[apiType];
let schema = config.schema;
let schemaFile = config.schemaFile;
if (values.apiVersion) {
schema = schema.replace('%%API_VERSION%%', `/${values.apiVersion}`);
schemaFile = schemaFile.replace('%%API_VERSION%%', `-${values.apiVersion}`);
}
else {
schema = schema.replace('%%API_VERSION%%', '');
schemaFile = schemaFile.replace('%%API_VERSION%%', '');
}
return {
schema,
schemaFile: `${outputDir}/${schemaFile}`,
};
}
function getCustomerApiSchema(outputDir, values) {
const config = apiConfigs.apiConfigs[types.ApiType.Customer];
let schema = config.schema;
let schemaFile = config.schemaFile;
if (values.apiVersion) {
schema = schema.replace('%%API_VERSION%%', values.apiVersion);
schemaFile = schemaFile.replace('%%API_VERSION%%', `-${values.apiVersion}`);
}
else {
schema = schema.replace('&api_version=%%API_VERSION%%', '');
schemaFile = schemaFile.replace('%%API_VERSION%%', '');
}
if (values.apiKey) {
schema = schema.replace('%%API_KEY%%', values.apiKey);
}
return {
schema: [{ [schema]: { method: 'GET', headers: {} } }],
schemaFile: `${outputDir}/${schemaFile}`,
};
}
exports.getSchemaData = getSchemaData;
//# sourceMappingURL=get-schema-data.js.map