[REQUEST] Update types to support an array of promises as the resolved value for batchLoadFn

Abierto
#247 0 comentarios 1 reacción 0 asignados Ver en GitHub

@chasingmaxwell ya está trabajando en esto.

Desde el 3/8/2020.

  • #248 de @chasingmaxwell — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
35/100
Tipo de issue
Nueva funcionalidad
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
javascript

Línea de trabajo

Comienza por la definición del tipo BatchLoadFn y las declaraciones de tipo de DataLoader. Compara el tipo de retorno declarado con el ejemplo de un array de promesas y, después, verifica que el ejemplo de comprobación de tipos acepta elementos Promise al tiempo que conserva los casos V y Error.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

enhancement

What problem are you trying to solve?

The DataLoader class already functionally handles the case where you resolve with an array of promises rather than an array of values. This is nice because it allows individual items in the batch to resolve separately and prevents the case where one long-running item in the batch holds up the rest of the items which were resolved quickly.

The problem is that the type definition for the batchLoadFn does not allow for individual items in the resolved array to be promises containing V. They must either be V or Error.

Here's an example.

const dataloader = new DataLoader<TheKey, TheValue>(
  // This will cause a flow error because TheValue does not equal Promise<TheValue>.
  async (keys: $ReadOnlyArray<TheKey>): Promise<Array<Promise<TheValue>>> => {
    return keys.map(key =>
      doSomethingTimeConsuming(key)
        .then(doMoreTimeConsumingThings)
        // Ensure errors are cached.
        .catch(e => e)
    );
  }
);

Describe the solution you'd like

The type BatchLoadFn could be updated to return Promise<$ReadOnlyArray<V | Promise<V> | Error>>.

Describe alternatives you've considered

I could just specify Promise<TheValue> as the return value for the dataloader, but then I have to do silly things like this to keep flow happy:

const theValue = await await dataloader.load(theKey);

Additional context

I'm working on a pull request now to address this if the maintainers decide this is a good change.

Lenguaje dominante
JavaScript
Estrellas
13.4k
Forks
522
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de graphql/dataloader

Todos los issues de graphql/dataloader

Issues similares

Más issues de JavaScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.