Best JavaScript code snippet using stryker-parent
schemaController.js
Source:schemaController.js
1const { buildClientSchema, getIntrospectionQuery } = require('graphql');2const fetch = require('node-fetch');3// const fs = require('fs');4// const path = require('path');5const schemaController = {};6schemaController.introspect = (req, res, next) => {7 fetch(req.body.endpoint, {8 method: 'POST',9 headers: { 'Content-Type': 'application/json' },10 body: JSON.stringify({"query": getIntrospectionQuery()})11 }).then((res) =>res.json())12 .then((data) => {13 res.locals.introspectionResult = data.data;14 return next();15 })16 .catch(() => next({17 log: 'There was a problem running the introspection query',18 status: 400,19 message: { err: 'There was a problem running the introspection query' },20 }));21};22// converts schema into an object of Types and their respective fields (along with references to other Types)23// output is in format e.g.:24schemaController.convertSchema = (req, res, next) => {25 try {26 const { sourceSchema } = req.body;27 const cleanedSchema = cleanSchema(sourceSchema);28 const d3Json = schemaToD3(cleanedSchema);29 // Writes and saves the JSON file into root folder30 // fs.writeFileSync(path.resolve(__dirname, 'SWschema.json'), JSON.stringify(sourceSchema, null, 2));31 // Stores the file path for future middleware to access to implement in d332 // res.locals.path = path.resolve(__dirname, 'd3schema.json');33 res.locals.schema = buildClientSchema(sourceSchema);34 res.locals.d3json = d3Json;35 return next();36 } catch {37 return next({38 log: 'There was a problem converting the introspected schema',39 status: 500,40 message: { err: 'There was a problem converting the introspected schema' },41 });42 }43};44// cleanSchema cleans the schema received from the client's graphQL endpoint into format:45/*46{47 Query: [48 'info',49 { teams: 'Team' },50 { players: 'Player' },51 { games: 'Fixture' },52 { fixtures: 'Fixture' }53 ],54 ....55 Result: [ 'goalsHomeTeam', 'goalsAwayTeam' ]56}57*/58const cleanSchema = (sourceSchema) => {59 const schemaTypes = sourceSchema.__schema.types;60 const types = {};61 for (let i = 0; i < schemaTypes.length; i++) {62 // iterate only through relevant types (tables)63 if (64 schemaTypes[i].fields !== null &&65 schemaTypes[i].name.indexOf('__') === -166 ) {67 const fieldsList = [];68 // Iterate through the fields array of each type (table)69 for (let j = 0; j < schemaTypes[i].fields.length; j++) {70 if (71 schemaTypes[i].fields[j].name &&72 !schemaTypes[i].fields[j].isDeprecated73 ) {74 // checks if the type of a field references another Type75 if (schemaTypes[i].fields[j].type.kind === 'OBJECT') {76 const fieldsLink = {};77 fieldsLink[schemaTypes[i].fields[j].name] =78 schemaTypes[i].fields[j].type.name;79 fieldsList.push(fieldsLink);80 }81 // checks if the type of a field is a list and references another Type82 else if (83 schemaTypes[i].fields[j].type.kind === 'LIST' &&84 schemaTypes[i].fields[j].type.ofType.kind === 'OBJECT'85 ) {86 const fieldsLink = {};87 fieldsLink[schemaTypes[i].fields[j].name] =88 schemaTypes[i].fields[j].type.ofType.name;89 fieldsList.push(fieldsLink);90 } else if (91 schemaTypes[i].fields[j].type.ofType &&92 schemaTypes[i].fields[j].type.ofType.ofType93 ) {94 // creates a key-value pair of relationship between the field name and the Type if it points to another Type95 const fieldsLink = {};96 fieldsLink[schemaTypes[i].fields[j].name] =97 schemaTypes[i].fields[j].type.ofType.ofType.name;98 fieldsList.push(fieldsLink);99 } else {100 fieldsList.push(schemaTypes[i].fields[j].name);101 }102 }103 }104 types[schemaTypes[i].name] = fieldsList;105 }106 }107 return types;108};109// schemaToD3 converts the "cleaned" schema into a JSON file for d3 in format:110/*111{112 nodes: [113 { name: 'Query', type: 'Type' },114 { name: 'info', type: 'field' },115 ....116 ],117 links: [118 { source: 'info', target: 'Info' },119 { source: 'Query', target: 'info' },120 ...121 ]122}123*/124const schemaToD3 = (cleanedSchema) => {125 const d3Json = {};126 const linksArray = [];127 const duplicateChecker = {};128 const duplicateFields = [];129 // name each node with an & followed by the Type that the field belongs to, to solve for cases130 // where multiple fields have the same name across different Types131/* eslint-disable */132// Loop through the cleanedSchema once to check for duplicate fields133 for (let key in cleanedSchema) {134 for (let i = 0; i < cleanedSchema[key].length; i++) {135 const field = Object.keys(cleanedSchema[key][i]);136 if (typeof cleanedSchema[key][i] === 'object') {137 const fieldObject = JSON.stringify(cleanedSchema[key][i]);138 // if current field element already exists in duplicateTracker139 if (duplicateChecker[fieldObject]) {140 duplicateFields.push(field[0]);141 } else duplicateChecker[fieldObject] = true;142 }143 }144 }145 for (let key in cleanedSchema) {146 for (let i = 0; i < cleanedSchema[key].length; i++) {147 const fieldName = Object.keys(cleanedSchema[key][i]);148 if (typeof cleanedSchema[key][i] !== 'object') {149 // create links from each Type to their fields150 let sourceName = key + '&';151 let targetName = cleanedSchema[key][i]+ '&' + key;152 if (duplicateFields.includes(cleanedSchema[key][i])) {153 targetName = cleanedSchema[key][i]+ '&multiple';154 }155 linksArray.push(createNode(sourceName, 'Type', targetName, 'field'))156 } 157 // if an object158 else {159 // Create link from current Type to field160 let sourceName = key + '&';161 let targetName = fieldName[0] + '&' + key;162 if (duplicateFields.includes(fieldName[0])) {163 targetName = fieldName[0]+ '&multiple';164 }165 linksArray.push(createNode(sourceName, 'Type', targetName, 'field'));166 // Create link from fields to other Types167 sourceName = fieldName[0] + '&' + key;168 if (duplicateFields.includes(fieldName[0])) {169 sourceName = fieldName[0]+ '&multiple';170 }171 targetName = cleanedSchema[key][i][fieldName[0]] + '&';172 linksArray.push(createNode(sourceName, 'field', targetName, 'Type'));173 }174 }175 }176 d3Json.links = linksArray;177 d3Json.duplicates = duplicateFields;178 return d3Json;179};180const createNode = (sourceName, sourceType, targetName, targetType) => {181 const linkObj = {};182 const nodeSource = {};183 const nodeTarget = {};184 nodeSource.name = sourceName;185 nodeSource.type = sourceType;186 nodeSource.highlighted = false;187 linkObj.source = nodeSource;188 nodeTarget.name = targetName;189 nodeTarget.type = targetType;190 nodeTarget.highlighted = false;191 linkObj.target = nodeTarget;192 return linkObj;193};...
generate-json-schema-to-ts.js
Source:generate-json-schema-to-ts.js
1// @ts-check2const { compile } = require('json-schema-to-typescript');3const fs = require('fs');4const path = require('path');5const glob = require('glob');6const { promisify } = require('util');7const writeFile = promisify(fs.writeFile);8const mkdir = promisify(fs.mkdir);9const resolveFromParent = path.resolve.bind(path, __dirname, '..');10const globAsPromised = promisify(glob);11/**12 * 13 * @param {string} schemaFile 14 */15async function generate(schemaFile) {16 const parsedFile = path.parse(schemaFile);17 const schema = preprocessSchema(require(schemaFile));18 const resolveOutputDir = path.resolve.bind(path, parsedFile.dir, '..', 'src-generated');19 const outFile = resolveOutputDir(`${parsedFile.name}.ts`);20 await mkdir(resolveOutputDir(), { recursive: true });21 let ts = (await compile(schema, parsedFile.base, {22 style: {23 singleQuote: true,24 },25 $refOptions: {26 resolve: {27 http: false // We're not interesting in generating exact babel / tsconfig / etc types28 }29 },30 bannerComment: `/**31 * This file was automatically generated by ${path.basename(__filename)}.32 * DO NOT MODIFY IT BY HAND. Instead, modify the source file JSON file: ${path.basename(schemaFile)},33 * and run 'npm run generate' from monorepo base directory.34 */`35 }));36 await writeFile(outFile, ts, 'utf8');37 console.info(`â
${path.relative(path.resolve(__dirname, '..'), path.resolve(__dirname, schemaFile))} -> ${path.relative(path.resolve(__dirname, '..'), resolveFromParent(outFile))}`);38}39async function generateAllSchemas() {40 const files = await globAsPromised('packages/!(core)/schema/*.json', { cwd: resolveFromParent() });41 await Promise.all(files.map(fileName => generate(resolveFromParent(fileName))));42}43generateAllSchemas().catch(err => {44 console.error('TS generation failed', err);45 process.exitCode = 1;46});47function preprocessSchema(inputSchema) {48 const cleanedSchema = cleanExternalRef(inputSchema);49 try {50 switch (cleanedSchema.type) {51 case 'object':52 const inputRequired = cleanedSchema.required || [];53 const outputSchema = {54 ...cleanedSchema,55 properties: preprocessProperties(cleanedSchema.properties),56 definitions: preprocessProperties(cleanedSchema.definitions),57 required: preprocessRequired(cleanedSchema.properties, inputRequired)58 }59 if (cleanedSchema.definitions) {60 outputSchema.definitions = preprocessProperties(cleanedSchema.definitions);61 }62 return outputSchema;63 case 'array':64 return {65 ...cleanedSchema,66 items: preprocessSchema(cleanedSchema.items)67 }68 default:69 if (cleanedSchema.$ref) {70 // Workaround for: https://github.com/bcherny/json-schema-to-typescript/issues/19371 return {72 $ref: cleanedSchema.$ref73 }74 }75 if(cleanedSchema.oneOf) {76 return {77 ...cleanedSchema,78 oneOf: cleanedSchema.oneOf.map(preprocessSchema)79 }80 }81 return cleanedSchema;82 }83 } catch (err) {84 if (err instanceof SchemaError) {85 throw err86 } else {87 throw new SchemaError(`Schema failed: ${JSON.stringify(inputSchema)}, ${err.stack}`);88 }89 }90}91class SchemaError extends Error { }92function preprocessProperties(inputProperties) {93 if (inputProperties) {94 const outputProperties = {};95 Object.entries(inputProperties).forEach(([name, value]) => outputProperties[name] = preprocessSchema(value));96 return outputProperties;97 }98}99function cleanExternalRef(inputSchema) {100 if(inputSchema.$ref && inputSchema.$ref.startsWith('http')) {101 return {102 ...inputSchema,103 $ref: undefined104 }105 }106 return inputSchema;107}108function preprocessRequired(inputProperties, inputRequired) {109 if (inputProperties) {110 return Object.entries(inputProperties)111 .filter(([name, value]) => 'default' in value || inputRequired.indexOf(name) >= 0)112 .map(([name]) => name)113 } else {114 return inputRequired;115 }...
Using AI Code Generation
1var cleanedSchema = require('stryker-parent').cleanedSchema;2var schema = {3 "properties": {4 "foo": {5 },6 "bar": {7 }8 }9};10var cleanSchema = cleanedSchema(schema);11console.log(cleanSchema);12{ foo: { type: 'string' }, bar: { type: 'boolean' } }13const cleanedSchema = require('stryker-parent').cleanedSchema;
Using AI Code Generation
1const cleanedSchema = require('stryker-parent').cleanedSchema;2const schema = cleanedSchema({3 properties: {4 foo: { type: 'string' },5 bar: { type: 'string' }6 }7});8console.log(schema);9{10 properties: {11 foo: { type: 'string' },12 bar: { type: 'string' }13 }14}15const cleanedSchema = require('stryker-parent').cleanedSchema;16const schema = cleanedSchema({17 properties: {18 foo: { type: 'string' },19 bar: { type: 'string' }20 }21});22console.log(schema);23{24 properties: {25 foo: { type: 'string' },26 bar: { type: 'string' }27 }28}29const cleanedSchema = require('stryker-parent').cleanedSchema;30const schema = cleanedSchema({31 properties: {32 foo: { type: 'string' },33 bar: { type: 'string' }34 }35});36console.log(schema);37{38 properties: {39 foo: { type: 'string' },40 bar: { type: 'string' }41 }42}43const cleanedSchema = require('stryker-parent').cleanedSchema;44const schema = cleanedSchema({45 properties: {46 foo: { type: 'string' },47 bar: { type: 'string' }48 }49});50console.log(schema);51{52 properties: {53 foo: { type: 'string' },54 bar: { type: 'string' }55 }56}57const cleanedSchema = require('stryker-parent').cleanedSchema;58const schema = cleanedSchema({59 properties: {60 foo: { type: 'string' },61 bar: { type: '
Using AI Code Generation
1const { cleanedSchema } = require('stryker-parent');2const schema = require('./schema.json');3const cleanedSchema = cleanedSchema(schema);4console.log(JSON.stringify(cleanedSchema, null, 2));5{6 "properties": {7 "a": {8 },9 "b": {10 }11 }12}13{14 "properties": {15 "a": {16 },17 "b": {18 }19 }20}21module.exports = function(config) {22 config.set({23 karma: {24 config: {25 },26 }27 });28};29import { cleanedSchema } from 'stryker-parent';30const config: cleanedSchema = {31};32import { cleanedSchema } from 'stryker-parent';33const config: cleanedSchema = {34};35import { cleanedSchema } from 'stryker-parent';36const config: cleanedSchema = {37};38import { cleanedSchema } from 'stryker-parent';39const config: cleanedSchema = {40};41import { cleanedSchema } from 'stryker-parent';42const config: cleanedSchema = {43};
Using AI Code Generation
1var strykerParent = require('stryker-parent');2var cleanedSchema = strykerParent.cleanedSchema;3var schema = cleanedSchema(require('./schema.json'));4var strykerParent = require('stryker-parent');5var cleanedSchema = strykerParent.cleanedSchema;6var schema = cleanedSchema(require('./schema.json'));7var strykerParent = require('stryker-parent');8var cleanedSchema = strykerParent.cleanedSchema;9var schema = cleanedSchema(require('./schema.json'));10var strykerParent = require('stryker-parent');11var cleanedSchema = strykerParent.cleanedSchema;12var schema = cleanedSchema(require('./schema.json'));13var strykerParent = require('stryker-parent');14var cleanedSchema = strykerParent.cleanedSchema;15var schema = cleanedSchema(require('./schema.json'));16var strykerParent = require('stryker-parent');17var cleanedSchema = strykerParent.cleanedSchema;18var schema = cleanedSchema(require('./schema.json'));19var strykerParent = require('stryker-parent');20var cleanedSchema = strykerParent.cleanedSchema;21var schema = cleanedSchema(require('./schema.json'));22var strykerParent = require('stryker-parent');23var cleanedSchema = strykerParent.cleanedSchema;24var schema = cleanedSchema(require('./schema.json'));25var strykerParent = require('stryker-parent');26var cleanedSchema = strykerParent.cleanedSchema;27var schema = cleanedSchema(require('./schema.json'));28var strykerParent = require('stryker-parent');29var cleanedSchema = strykerParent.cleanedSchema;30var schema = cleanedSchema(require('./schema.json'));31var strykerParent = require('stryker-parent');32var cleanedSchema = strykerParent.cleanedSchema;33var schema = cleanedSchema(require('./schema.json'));
Using AI Code Generation
1const schema = require('stryker-parent/schema/cleanedSchema.json');2const schema = require('stryker/schema/cleanedSchema.json');3const schema = require('stryker-parent/schema/cleanedSchema.json');4const schema = require('stryker/schema/cleanedSchema.json');5const schema = require('stryker-parent/schema/cleanedSchema.json');6const schema = require('stryker/schema/cleanedSchema.json');7const schema = require('stryker-parent/schema/cleanedSchema.json');8const schema = require('stryker/schema/cleanedSchema.json');9const schema = require('stryker-parent/schema/cleanedSchema.json');10const schema = require('stryker/schema/cleanedSchema.json');11const schema = require('stryker-parent/schema/cleanedSchema.json');12const schema = require('stryker/schema/cleanedSchema.json');13const schema = require('stryker-parent/schema/cleanedSchema.json');14const schema = require('stryker/schema/cleanedSchema.json');15const schema = require('stryker-parent/schema/cleanedSchema.json');16const schema = require('stryker/schema/cleanedSchema.json');17const schema = require('stryker-parent/schema/cleanedSchema.json');
Using AI Code Generation
1const schema = require('stryker-parent/schema/cleanedSchema.json');2const schema = require('stryker/schema/cleanedSchema.json');3const schema = require('stryker-parent/schema/cleanedSchema.json');4const schema = require('stryker/schema/cleanedSchema.json');5const schema = require('stryker-parent/schema/cleanedSchema.json');6const schema = require('stryker/schema/cleanedSchema.json');7const schema = require('stryker-parent/schema/cleanedSchema.json');8const schema = require('stryker/schema/cleanedSchema.json');9const schema = require('stryker-parent/schema/cleanedSchema.json');10const schema = require('stryker/schema/cleanedSchema.json');11const schema = require('stryker-parent/schema/cleanedSchema.json');12const schema = require('stryker/schema/cleanedSchema.json');13const schema = require('stryker-parent/schema/cleanedSchema.json');14const schema = require('stryker/schema/cleanedSchema.json');15const schema = require('stryker-parent/schema/cleanedSchema.json');16const schema = require('stryker/schema/cleanedSchema.json');17const schema = require('stryker-parent/schema/cleanedSchema.json');
Using AI Code Generation
1const strykerParent = require('stryker-parent');2const cleanedSchema = strykerParent.cleanedSchema;3const schema = require('./schema.json');4const cleanedSchema = require('stryker-parent').cleanedSchema;5const schema = require('./schema.json');6const { cleanedSchema } = require('stryker-parent');7const schema = require('./schema.json');8const { cleanedSchema } = require('stryker-parent');9const schema = require('./schema.son');10const { cleanedSchema } = require('stryker-parent');11const schema = require('./schema.json');12const { cleanedSchema } = require('stryker-parent');13const schema = require('./schema.json');14const { cleanedSchema } = require('stryker-parent');15const schema = cleanedSchema({});16module.exports = function(config) {17 config.set({18 jest: {19 },20 });21};22module.exports = require('./test');23module.exports = {24 transform: {25 },26 testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',27 globals: {28 'ts-jest': {29 }30 },31};32 at JSON.parse (<anonymous>)33 at Object.exports.parse (C:\Users\shubh\Documents\stryker\stryker-jest\node_modules\stryker\src\utils\objectUtils.js:6:28)34 at Object.exports.readJson (C:\Users\shubh\Documents\stryker\stryker-jest\node_modules\stryker\src\utils\fileUtils.js:29:26)
Using AI Code Generation
1const strykerParent = require('stryker-parent');2const cleanedSchema = strykerParent.cleanedSchema;3const schema = require('./schema.json');4const cleanedSchema = require('stryker-parent').cleanedSchema;5const schema = require('./schema.json');6const { cleanedSchema } = require('stryker-parent');7const schema = require('./schema.json');8const { cleanedSchema } = require('stryker-parent');9const schema = require('./schema.json');10const { cleanedSchema } = require('stryker-parent');11const schema = require('./schema.json');12const { cleanedSchema } = require('stryker-parent');13const schema = require('./schema.json');
Using AI Code Generation
1const {cleanedSchema} = require('stryker-parent');2const schema = require('./schema');3const cleaned = cleanedSchema(schema);4console.log(JSON.stringify(cleaned, null, 2));5module.exports = {6 properties: {7 foo: {8 },9 bar: {10 },11 baz: {12 },13 },14};15{16 "properties": {17 "foo": {18 },19 "bar": {20 },21 "baz": {22 }23 },24}
Using AI Code Generation
1const { cleanedSchema } = require('stryker-parent');2const { schema } = require('stryker-api/config');3const config = require('stryker-api/config');4const schema = cleanedSchema(config.schema);5const { cleanedSchema } = require('stryker-parent');6const { schema } = require('stryker-api/config');7const config = require('stryker-api/config');8const schema = cleanedSchema(config.schema);9const { cleanedSchema } = require('stryker-parent');10const { schema } = require('stryker-api/config');11const config = require('stryker-api/config');12const schema = cleanedSchema(config.schema);13const { cleanedSchema } = require('stryker-parent');14const { schema } = require('stryker-api/config');15const config = require('stryker-api/config');16const schema = cleanedSchema(config.schema);17const { cleanedSchema } = require('stryker-parent');18const { schema } = require('stryker-api/config');19const config = require('stryker-api/config');20const schema = cleanedSchema(config.schema);21const { cleanedSchema } = require('stryker-parent');22const { schema } = require('stryker-api/config');23const config = require('stryker-api/config');24const schema = cleanedSchema(config.schema);25const { cleanedSchema } = require('stryker-parent');26const { schema } = require('stryker-api/config');27const config = require('stryker-api/config');28const schema = cleanedSchema(config.schema);29const { cleanedSchema } = require('stryker-parent');30const { schema } = require('stryker-api/config');31const config = require('stryker-api/config');32const schema = cleanedSchema(config.schema);33const { cleanedSchema } = require('stryker-parent');34const { schema } =
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!