graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

[graphql-tools/load] `loadDocuments` different error strategies

Open n1ru4l opened this issue 3 years ago • 0 comments

Is your feature request related to a problem? Please describe.

Currently, if one of the GraphQL documents loaded via loadDocuments is invalid (e.g. raises a parse error), a single GraphQLError instance is thrown.

However, it might be useful to sometimes have:

  • access to all the failed parsing errors
  • access to the successfully parsed documents and the failed parsed documents

Describe the solution you'd like

Introduce different "load error behaviors" that alter the error handling. This could be done by a property that is passed to the loadDocuments function.

Raise first error as it happens (current behaviour)

const documents = await loadDocuments(['packages/**/*.graphql', 'packages/**/*.ts(x)'], {
  loaders: [new GraphQLFileLoader(), new CodeFileLoader()],
  errorBehaviour: 'legacy' // !!! better name recommendations welcome!!!
});

Raise all errors as AggregateError

const documents = await loadDocuments(['packages/**/*.graphql', 'packages/**/*.ts(x)'], {
  loaders: [new GraphQLFileLoader(), new CodeFileLoader()],
  errorBehaviour: 'aggregate'
});

Do not raise an error at all, but instead include error in the return result

const documents = await loadDocuments(['packages/**/*.graphql', 'packages/**/*.ts(x)'], {
  loaders: [new GraphQLFileLoader(), new CodeFileLoader()],
  errorBehaviour: 'ignore'
});

documents[0].document // this is null 
documents[0].error // this is a GraphQLError

Describe alternatives you've considered

So far there is no alternative solution except re-implementing the function from scratch for each use case.

n1ru4l avatar Aug 31 '22 10:08 n1ru4l