Netflix / falcor

A JavaScript library for efficient data fetching
http://netflix.github.io/falcor
Apache License 2.0
10.48k stars 446 forks source link

Model#call arguments validation #994

Open cameronhunter opened 3 years ago

cameronhunter commented 3 years ago

Context

I was surprised that Model#call's arguments validation (source) changes depending on the number of arguments provided. For example:

// Passes input validation.
model.call(['hello', 'world'], [{ name: 'Falcor' }]);

// Fails input validation with "Invalid argument" error.
model.call(['hello', 'world'], [{ name: 'Falcor' }], undefined);

This difference likely doesn't affect developers invoking call directly, but it would affect developers that have wrapped Model#call for whatever reason. For example:

function call<T = any>(
  functionPath: string | Path,
  args?: any[],
  refPaths?: PathSet,
  thisPaths?: PathSet[],
): ModelResponse<JSONEnvelope<T>> {
  // Fails because optional parameters will always be passed as `undefined`.
  return model.call(functionPath, args, refPaths, thisPaths);
}

Workaround

The workaround is to use a spread to pass the same number of parameters:

function call<T = any>(
  functionPath: string | Path,
  args?: any[],
  refPaths?: PathSet,
  thisPaths?: PathSet[],
): ModelResponse<JSONEnvelope<T>>;
function call(...args: Parameters<Model['call']>) {
  return model.call(...args);
}

Change Request