Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function enumerationDeclaration(
generator: CodeGenerator,
type: GraphQLEnumType
) {
const { name, description } = type;
const values = type.getValues();
generator.printNewlineIfNeeded();
if (description) {
description.split("\n").forEach(line => {
generator.printOnNewline(`// ${line.trim()}`);
});
}
generator.printOnNewline(`export type ${name} =`);
const nValues = values.length;
sortEnumValues(values).forEach((value, i) => {
if (!value.description || value.description.indexOf("\n") === -1) {
generator.printOnNewline(
` "${value.value}"${i === nValues - 1 ? ";" : " |"}${wrap(
" // ",
value.description || undefined
)}`
);
} else {
if (value.description) {
value.description.split("\n").forEach(line => {
generator.printOnNewline(` // ${line.trim()}`);
});
}
generator.printOnNewline(
` "${value.value}"${i === nValues - 1 ? ";" : " |"}`
);
propertiesForSelectionSet(
selectionSet: SelectionSet,
namespace?: string
): (Field & Property & Struct)[] | undefined {
const properties = collectAndMergeFields(selectionSet, true)
.filter(field => field.name !== "__typename")
.map(field => this.propertyFromField(field, namespace));
// If we're not merging in fields from fragment spreads, there is no guarantee there will a generated
// type for a composite field, so to avoid compiler errors we skip the initializer for now.
if (
selectionSet.selections.some(
selection => selection.kind === "FragmentSpread"
) &&
properties.some(property => isCompositeType(getNamedType(property.type)))
) {
return undefined;
}
return properties;
}
export function generateSource(context: LegacyCompilerContext) {
const generator = new CodeGenerator(context);
generator.printOnNewline("/* tslint:disable */");
generator.printOnNewline(
"// This file was automatically generated and should not be edited."
);
context.typesUsed.forEach(type =>
typeDeclarationForGraphQLType(generator, type)
);
Object.values(context.operations).forEach(operation => {
interfaceVariablesDeclarationForOperation(generator, operation);
interfaceDeclarationForOperation(generator, operation);
});
Object.values(context.fragments).forEach(operation =>
interfaceDeclarationForFragment(generator, operation)
);
export function generateSource(context: LegacyCompilerContext) {
const generator = new CodeGenerator(context);
generator.printOnNewline("/* @flow */");
generator.printOnNewline("/* eslint-disable */");
generator.printOnNewline(
"// This file was automatically generated and should not be edited."
);
context.typesUsed.forEach(type =>
typeDeclarationForGraphQLType(generator, type)
);
Object.values(context.operations).forEach(operation => {
interfaceVariablesDeclarationForOperation(generator, operation);
typeDeclarationForOperation(generator, operation);
});
Object.values(context.fragments).forEach(fragment => {
typeDeclarationForFragment(generator, fragment);
});
export function generateSource(context: LegacyCompilerContext) {
const generator = new CodeGenerator(context);
generator.printOnNewline(
"// This file was automatically generated and should not be edited."
);
generator.printNewline();
if (context.options.namespace) {
packageDeclaration(generator, context.options.namespace);
}
context.typesUsed.forEach(type => {
typeDeclarationForGraphQLType(generator, type);
});
Object.values(context.operations).forEach(operation => {
classDeclarationForOperation(generator, operation);
t.TSTypeAnnotation(type)
);
// TODO: Check if this works
propertySignatureType.optional =
keyInheritsNullability && this.isNullableType(type);
if (this.options.useReadOnlyTypes) {
propertySignatureType.readonly = true;
}
if (description) {
propertySignatureType.leadingComments = [
{
type: "CommentBlock",
value: commentBlockContent(description)
} as t.CommentBlock
];
}
return propertySignatureType;
});
}
);
// Nullable fields on input objects do not have to be defined
// as well, so allow these fields to be "undefined"
objectTypeProperty.optional =
keyInheritsNullability &&
annotation.type === "NullableTypeAnnotation";
if (this.options.useReadOnlyTypes) {
objectTypeProperty.variance = { kind: "plus" };
}
if (description) {
objectTypeProperty.leadingComments = [
{
type: "CommentBlock",
value: commentBlockContent(description)
} as t.CommentBlock
];
}
return objectTypeProperty;
})
);
async function fromFile(
file: string
): Promise {
let result;
try {
result = fs.readFileSync(file, {
encoding: "utf-8"
});
} catch (err) {
throw new Error(`Unable to read file ${file}. ${err.message}`);
}
const ext = path.extname(file);
// an actual introspectionQuery result
if (ext === ".json") {
const parsed = JSON.parse(result);
const schemaData = parsed.data
? parsed.data.__schema
: parsed.__schema
? parsed.__schema
: parsed;
task: (ctx, task) => {
// Make sure the expectations of our context are correct.
assert.strictEqual(typeof ctx.queryDocuments, "undefined");
ctx.queryDocuments = loadQueryDocuments(
ctx.documentSets[0].documentPaths,
flags.tagName
);
task.title = `Scanning for GraphQL queries (${
ctx.queryDocuments.length
} found)`;
}
});
test("handles multiline graphql comments", () => {
const miscSchema = loadSchema(
require.resolve("../../../../__fixtures__/misc/schema.json")
);
const document = parse(`
query CustomScalar {
commentTest {
multiLine
}
}
`);
const output = generateSource(
compileToIR(miscSchema, document, {
mergeInFieldsFromFragmentSpreads: true,
addTypename: true
})
);
expect(output).toMatchSnapshot();
});
});