Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add separate directory for flow
  • Loading branch information
cehan-Chloe committed Mar 20, 2024
commit 52483432222d757f4db06f3de6574083522bd6e6
15 changes: 7 additions & 8 deletions src/codegen.ts → src/genTypeFlow/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import _ from 'lodash';
import prettier from 'prettier';
import { GlobalConfig, getResourcePaths, LanguageOptions } from './config';
import { getLoaderType, getLoadersTypeMap, getResourceTypings } from './genTypeFlow';
import getLoaderImplementation from './implementation';
import { GlobalConfig, getResourcePaths } from '../config';
import { getLoaderType, getLoadersTypeMap, getResourceTypings } from './genType';
import getLoaderImplementation from '../genTypeFlow/implementation';

function getLoaders(language: LanguageOptions, config: object, paths: Array<Array<string>>, current: Array<string>) {
function getLoaders(config: object, paths: Array<Array<string>>, current: Array<string>) {
if (_.isEqual(paths, [[]])) {
return getLoaderImplementation(language, _.get(config, current.join('.')), current);
return getLoaderImplementation(_.get(config, current.join('.')), current);
}

const nextValues = _.uniq(paths.map((p) => p[0]));

const objectProperties: Array<string> = nextValues.map(
(nextVal) =>
`${nextVal}: ${getLoaders(
language,
config,
paths.filter((p) => p[0] === nextVal).map((p) => p.slice(1)),
[...current, nextVal],
Expand Down Expand Up @@ -111,10 +110,10 @@ export default function codegen(
* ===============================
*/

export type LoadersType = ${getLoadersTypeMap(config.typings.language, config.resources, getResourcePaths(config.resources), [])};
export type LoadersType = ${getLoadersTypeMap(config.resources, getResourcePaths(config.resources), [])};

export default function getLoaders(resources: ResourcesType, options?: DataLoaderCodegenOptions): LoadersType {
return ${getLoaders(config.typings.language, config.resources, getResourcePaths(config.resources), [])};
return ${getLoaders(config.resources, getResourcePaths(config.resources), [])};
}
`;

Expand Down
6 changes: 2 additions & 4 deletions src/genTypeFlow.ts → src/genTypeFlow/genType.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import _ from 'lodash';
import assert from './assert';
import { GlobalConfig, ResourceConfig, LanguageOptions } from './config';
import assert from '../assert';
import { GlobalConfig, ResourceConfig, LanguageOption } from '../config';

function errorPrefix(resourcePath: ReadonlyArray<string>): string {
return `[dataloader-codegen :: ${resourcePath.join('.')}]`;
Expand Down Expand Up @@ -136,7 +136,6 @@ export function getLoaderType(resourceConfig: ResourceConfig, resourcePath: Read
}

export function getLoadersTypeMap(
language: LanguageOptions = LanguageOptions.FLOW,
config: object,
paths: ReadonlyArray<ReadonlyArray<string>>,
current: ReadonlyArray<string>,
Expand All @@ -150,7 +149,6 @@ export function getLoadersTypeMap(
const objectProperties: ReadonlyArray<string> = nextValues.map(
(nextVal) =>
`${nextVal}: ${getLoadersTypeMap(
language,
config,
paths.filter((p) => p[0] === nextVal).map((p) => p.slice(1)),
[...current, nextVal],
Expand Down
145 changes: 72 additions & 73 deletions src/implementation.ts → src/genTypeFlow/implementation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ResourceConfig, BatchResourceConfig, NonBatchResourceConfig, LanguageOptions } from './config';
import assert from './assert';
import { getLoaderTypeKey, getLoaderTypeVal } from './genTypeFlow';
import { errorPrefix } from './runtimeHelpers';
import { ResourceConfig, BatchResourceConfig, NonBatchResourceConfig } from '../config';
import assert from '../assert';
import { getLoaderTypeKey, getLoaderTypeVal } from './genType';
import { errorPrefix } from '../runtimeHelpers';

function getLoaderComment(resourceConfig: ResourceConfig, resourcePath: ReadonlyArray<string>): string {
const configComment = JSON.stringify(resourceConfig, null, 2)
Expand Down Expand Up @@ -61,8 +61,8 @@ function callResource(resourceConfig: ResourceConfig, resourcePath: ReadonlyArra
if (!(_response instanceof Error)) {
_response = new Error([
\`${errorPrefix(
resourcePath,
)} Caught an error, but errorHandler did not return an Error object.\`,
resourcePath,
)} Caught an error, but errorHandler did not return an Error object.\`,
\`Instead, got \${typeof _response}: \${util.inspect(_response)}\`,
].join(' '));
}
Expand Down Expand Up @@ -97,29 +97,29 @@ function batchLoaderLogic(resourceConfig: BatchResourceConfig, resourcePath: Rea
const requests = requestIDs.map(id => keys[id]);

${(() => {
const { batchKey, newKey, commaSeparatedBatchKey } = resourceConfig;
const { batchKey, newKey, commaSeparatedBatchKey } = resourceConfig;

let batchKeyParam = `['${batchKey}']: requests.map(k => k['${newKey}'])`;
if (commaSeparatedBatchKey === true) {
batchKeyParam = `${batchKeyParam}.join(',')`;
}
let batchKeyParam = `['${batchKey}']: requests.map(k => k['${newKey}'])`;
if (commaSeparatedBatchKey === true) {
batchKeyParam = `${batchKeyParam}.join(',')`;
}

return `
return `
// For now, we assume that the dataloader key should be the first argument to the resource
// @see https://github.com/Yelp/dataloader-codegen/issues/56
const resourceArgs = [{
..._.omit(requests[0], '${resourceConfig.newKey}'),
${batchKeyParam},
}];
`;
})()}
})()}

let response = await ${callResource(resourceConfig, resourcePath)}(resourceArgs);

if (!(response instanceof Error)) {
${(() => {
if (typeof resourceConfig.nestedPath === 'string') {
return `
if (typeof resourceConfig.nestedPath === 'string') {
return `
/**
* Un-nest the actual data from the resource return value.
*
Expand Down Expand Up @@ -155,16 +155,16 @@ function batchLoaderLogic(resourceConfig: BatchResourceConfig, resourcePath: Rea
),
);
`;
} else {
return '';
}
})()}
} else {
return '';
}
})()}
}

if (!(response instanceof Error)) {
${(() => {
if (resourceConfig.isResponseDictionary === true) {
return `
if (resourceConfig.isResponseDictionary === true) {
return `
if (typeof response !== 'object') {
response = new Error(
[
Expand All @@ -174,8 +174,8 @@ function batchLoaderLogic(resourceConfig: BatchResourceConfig, resourcePath: Rea
);
}
`;
} else {
return `
} else {
return `
if (!Array.isArray(response)) {
response = new Error(
[
Expand All @@ -185,23 +185,23 @@ function batchLoaderLogic(resourceConfig: BatchResourceConfig, resourcePath: Rea
);
}
`;
}
})()}
}
})()}
}

${(() => {
const { reorderResultsByKey, isResponseDictionary, propertyBatchKey } = resourceConfig;
if (
!isResponseDictionary &&
reorderResultsByKey == null &&
/**
* When there's propertyBatchKey and propertyNewKey, the resource might
* contain less number of items that we requested. It's valid, so we
* should skip the check.
*/
!(typeof propertyBatchKey === 'string')
) {
return `
const { reorderResultsByKey, isResponseDictionary, propertyBatchKey } = resourceConfig;
if (
!isResponseDictionary &&
reorderResultsByKey == null &&
/**
* When there's propertyBatchKey and propertyNewKey, the resource might
* contain less number of items that we requested. It's valid, so we
* should skip the check.
*/
!(typeof propertyBatchKey === 'string')
) {
return `
if (!(response instanceof Error)) {
/**
* Check to see the resource contains the same number
Expand All @@ -216,8 +216,8 @@ function batchLoaderLogic(resourceConfig: BatchResourceConfig, resourcePath: Rea
*/
response = new BatchItemNotFoundError([
\`${errorPrefix(
resourcePath,
)} Resource returned \${response.length} items, but we requested \${requests.length} items.\`,
resourcePath,
)} Resource returned \${response.length} items, but we requested \${requests.length} items.\`,
'Add reorderResultsByKey to the config for this resource to be able to handle a partial response.',
].join(' '));

Expand All @@ -228,15 +228,15 @@ function batchLoaderLogic(resourceConfig: BatchResourceConfig, resourcePath: Rea
}
}
`;
} else {
return '';
}
})()}
} else {
return '';
}
})()}

${(() => {
const { newKey, isResponseDictionary } = resourceConfig;
if (isResponseDictionary === true) {
return `
const { newKey, isResponseDictionary } = resourceConfig;
if (isResponseDictionary === true) {
return `
if (!(response instanceof Error)) {
response = resultsDictToList(
response,
Expand All @@ -245,10 +245,10 @@ function batchLoaderLogic(resourceConfig: BatchResourceConfig, resourcePath: Rea
);
}
`;
} else {
return '';
}
})()}
} else {
return '';
}
})()}

/**
* If the resource returns an Error, we'll want to copy and
Expand All @@ -273,46 +273,45 @@ function batchLoaderLogic(resourceConfig: BatchResourceConfig, resourcePath: Rea
* (If we didn't specify that this resource needs
* sorting, then this will be "null" and won't be used.)
*/
const reorderResultsByValue = ${
typeof resourceConfig.reorderResultsByKey === 'string'
? `keys[requestId]['${resourceConfig.newKey}']`
: 'null'
}
const reorderResultsByValue = ${typeof resourceConfig.reorderResultsByKey === 'string'
? `keys[requestId]['${resourceConfig.newKey}']`
: 'null'
}

// Tell flow that "response" is actually an error object.
// (This is so we can pass it as 'cause' to CaughtResourceError)
invariant(response instanceof Error, 'expected response to be an error');

return new CaughtResourceError(
\`${errorPrefix(
resourcePath,
)} Caught error during call to resource. Error: \${response.stack}\`,
resourcePath,
)} Caught error during call to resource. Error: \${response.stack}\`,
response,
reorderResultsByValue
);
});
}

${(() => {
const { reorderResultsByKey, newKey } = resourceConfig;
const { reorderResultsByKey, newKey } = resourceConfig;

if (typeof reorderResultsByKey === 'string') {
return `
if (typeof reorderResultsByKey === 'string') {
return `
response = sortByKeys({
items: response,
keys: requests.map(k => k['${newKey}']),
prop: '${reorderResultsByKey}',
resourcePath: ${JSON.stringify(resourcePath)},
})
`;
} else {
return '';
}
})()}
} else {
return '';
}
})()}
`;
}

function getBatchLoader(language: LanguageOptions, resourceConfig: BatchResourceConfig, resourcePath: ReadonlyArray<string>) {
function getBatchLoader(resourceConfig: BatchResourceConfig, resourcePath: ReadonlyArray<string>) {
assert(
resourceConfig.isBatchResource === true,
`${errorPrefix(resourcePath)} Expected getBatchLoader to be called with a batch resource config`,
Expand Down Expand Up @@ -415,13 +414,13 @@ function getBatchLoader(language: LanguageOptions, resourceConfig: BatchResource
* TODO: Figure out why directly passing `cacheKeyOptions` causes
* flow errors :(
*/ ''
}
}
...cacheKeyOptions
}
)`;
}

function getPropertyBatchLoader(language: LanguageOptions, resourceConfig: BatchResourceConfig, resourcePath: ReadonlyArray<string>) {
function getPropertyBatchLoader(resourceConfig: BatchResourceConfig, resourcePath: ReadonlyArray<string>) {
assert(
resourceConfig.isBatchResource === true,
`${errorPrefix(resourcePath)} Expected getBatchLoader to be called with a batch resource config`,
Expand Down Expand Up @@ -513,13 +512,13 @@ function getPropertyBatchLoader(language: LanguageOptions, resourceConfig: Batch
* TODO: Figure out why directly passing `cacheKeyOptions` causes
* flow errors :(
*/ ''
}
}
...cacheKeyOptions
}
)`;
}

function getNonBatchLoader(language: LanguageOptions, resourceConfig: NonBatchResourceConfig, resourcePath: ReadonlyArray<string>) {
function getNonBatchLoader(resourceConfig: NonBatchResourceConfig, resourcePath: ReadonlyArray<string>) {
assert(
resourceConfig.isBatchResource === false,
`${errorPrefix(resourcePath)} Expected getNonBatchLoader to be called with a non-batch endpoint config`,
Expand Down Expand Up @@ -555,14 +554,14 @@ function getNonBatchLoader(language: LanguageOptions, resourceConfig: NonBatchRe
})`;
}

export default function getLoaderImplementation(language: LanguageOptions = LanguageOptions.FLOW, resourceConfig: ResourceConfig, resourcePath: ReadonlyArray<string>) {
export default function getLoaderImplementation(resourceConfig: ResourceConfig, resourcePath: ReadonlyArray<string>) {
if (resourceConfig.isBatchResource) {
if (typeof resourceConfig.propertyBatchKey === 'string') {
return getPropertyBatchLoader(language, resourceConfig, resourcePath);
return getPropertyBatchLoader(resourceConfig, resourcePath);
} else {
return getBatchLoader(language, resourceConfig, resourcePath);
return getBatchLoader(resourceConfig, resourcePath);
}
} else {
return getNonBatchLoader(language, resourceConfig, resourcePath);
return getNonBatchLoader(resourceConfig, resourcePath);
}
}
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import assert from 'assert';
import fs from 'fs';
import path from 'path';
import yargs from 'yargs';
import codegen from './codegen';
import codegenFlow from './genTypeFlow/codegen';
import { getConfig } from './config';

interface CLIArgs {
Expand All @@ -14,7 +14,7 @@ interface CLIArgs {

function writeLoaders(args: CLIArgs) {
const config = getConfig(args.config);
const output = codegen(config);
const output = codegenFlow(config);

assert(typeof args.config === 'string', 'expected args.config to be set!');
assert(typeof args.output === 'string', 'expected args.output to be set!');
Expand Down