Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
options: GenerationOptions
): number {
let writtenFiles = 0;
validateQueryDocument(schema, document);
const { rootPath = process.cwd() } = options;
if (outputPath.split(".").length <= 1 && !fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}
if (target === "swift") {
options.addTypename = true;
const context = compileToIR(schema, document, options);
const outputIndividualFiles =
fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory();
const suppressSwiftMultilineStringLiterals = Boolean(
options.suppressSwiftMultilineStringLiterals
);
const generator = generateSwiftSource(
context,
outputIndividualFiles,
suppressSwiftMultilineStringLiterals,
only
);
if (outputIndividualFiles) {
writeGeneratedFiles(generator.generatedFiles, outputPath, "\n");
writtenFiles += Object.keys(generator.generatedFiles).length;
} else {
export default function generate(
document: DocumentNode,
schema: GraphQLSchema,
outputPath: string,
only: string | undefined,
target: TargetType,
tagName: string,
nextToSources: boolean | string,
options: GenerationOptions
): number {
let writtenFiles = 0;
validateQueryDocument(schema, document);
const { rootPath = process.cwd() } = options;
if (outputPath.split(".").length <= 1 && !fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}
if (target === "swift") {
options.addTypename = true;
const context = compileToIR(schema, document, options);
const outputIndividualFiles =
fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory();
const suppressSwiftMultilineStringLiterals = Boolean(
options.suppressSwiftMultilineStringLiterals
);
const generator = generateSwiftSource(
context,
);
writtenFiles += 1;
}
} else if (target === "typescript" || target === "ts") {
const context = compileToIR(schema, document, options);
const generatedFiles = generateTypescriptLocalSource(context);
const generatedGlobalFile = generateTypescriptGlobalSource(context);
const outFiles: {
[fileName: string]: BasicGeneratedFile;
} = {};
if (
nextToSources ||
(fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory())
) {
if (options.globalTypesFile) {
const globalTypesDir = path.dirname(options.globalTypesFile);
if (!fs.existsSync(globalTypesDir)) {
fs.mkdirSync(globalTypesDir);
}
} else if (nextToSources && !fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}
const globalSourcePath =
options.globalTypesFile ||
path.join(
outputPath,
`globalTypes.${options.tsFileExtension ||
TYPESCRIPT_DEFAULT_FILE_EXTENSION}`
} else if (target === "typescript" || target === "ts") {
const context = compileToIR(schema, document, options);
const generatedFiles = generateTypescriptLocalSource(context);
const generatedGlobalFile = generateTypescriptGlobalSource(context);
const outFiles: {
[fileName: string]: BasicGeneratedFile;
} = {};
if (
nextToSources ||
(fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory())
) {
if (options.globalTypesFile) {
const globalTypesDir = path.dirname(options.globalTypesFile);
if (!fs.existsSync(globalTypesDir)) {
fs.mkdirSync(globalTypesDir);
}
} else if (nextToSources && !fs.existsSync(outputPath)) {
fs.mkdirSync(outputPath);
}
const globalSourcePath =
options.globalTypesFile ||
path.join(
outputPath,
`globalTypes.${options.tsFileExtension ||
TYPESCRIPT_DEFAULT_FILE_EXTENSION}`
);
outFiles[globalSourcePath] = {
output: generatedGlobalFile.fileContents
};
generatedFiles.forEach(({ sourcePath, fileName, content }) => {
const dir = path.join(
path.dirname(path.posix.relative(rootPath, toPath(sourcePath))),
outputPath
);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
outFiles[path.join(dir, fileName)] = {
output: content.fileContents + common
};
});
outputPath
);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
outFiles[path.join(dir, fileName)] = {
output: content.fileContents + common
};
});
writeGeneratedFiles(outFiles, path.resolve("."));
writtenFiles += Object.keys(outFiles).length;
} else if (
fs.existsSync(outputPath) &&
fs.statSync(outputPath).isDirectory()
) {
generatedFiles.forEach(({ fileName, content }) => {
outFiles[fileName] = {
output: content.fileContents + common
};
});
writeGeneratedFiles(outFiles, outputPath);
writtenFiles += Object.keys(outFiles).length;
} else {
fs.writeFileSync(
outputPath,
generatedFiles.map(o => o.content.fileContents).join("\n") + common
);
export function findAndLoadConfig(
dir: string,
defaultEndpoint: boolean,
defaultSchema: boolean
): ApolloConfig {
if (fs.existsSync(join(dir, "apollo.config.js"))) {
return loadConfigFromFile(
join(dir, "apollo.config.js"),
defaultEndpoint,
defaultSchema
);
} else if (fs.existsSync(join(dir, "package.json"))) {
return loadConfigFromFile(
join(dir, "package.json"),
defaultEndpoint,
defaultSchema
);
} else {
return loadConfig({}, dir, dir, defaultEndpoint, defaultSchema);
}
}
export const fetchSchema = async (
{ url, headers, skipSSLValidation }: EndpointConfig,
projectFolder?: string
): Promise => {
if (!url) throw new Error("No endpoint provided when fetching schema");
const filePath = projectFolder ? path.resolve(projectFolder, url) : url;
if (fs.existsSync(filePath)) return fromFile(filePath);
var options: HttpLink.Options = { uri: url, fetch };
if (skipSSLValidation) {
const urlObject = new URL(url);
const host = urlObject.host;
const port = +urlObject.port || 443;
const agentOptions: AgentOptions = {
host: host,
port: port,
rejectUnauthorized: false
};
const agent = new Agent(agentOptions);
generatedFiles.forEach(({ sourcePath, fileName, content }) => {
let dir = outputPath;
if (nextToSources) {
dir = path.join(
path.dirname(path.relative(rootPath, toPath(sourcePath))),
dir
);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
}
const outFilePath = path.join(dir, fileName);
outFiles[outFilePath] = {
output: content({ outputPath: outFilePath, globalSourcePath })
.fileContents
};
});