Apollo codegen doesn't return non-zero exit code when errors have been found
Intended outcome:
Running apollo client:codegen ... should exit with a number different than 0, when there are errors.
Actual outcome:
Running apollo client:codegen ... exits by returning 0, even when there are errors. Such as Error: ️️There are multiple definitions for the AccountSelectorInputQuery operation. All operations in a project must have unique names. If generating types, only the types for the first definition found will be generated.. This turns out to be problematic on a pipeline where the next command shouldn't be executed, if there are errors on the previous one.
How to reproduce the issue:
Create two queries with the same name and run the apollo client:codegen ... command. Then run echo $? to get its return value. It is 0 but should be different, since there was an error.
Versions 2.21.3
Problem still exists in 2.30.3.
+1 - I'm working on a CI pipeline using codegen that would benefit greatly from a non-zero exit code. I was really hoping apollo would do this for me instead of building something to interface directly with getValidationErrors). Looking forward to action around this.
A quick solution until this is merged:
You can run the following bash script on a CI environment which will fail if the apollo codegen output contains fail anywhere
# https://stackoverflow.com/a/47560475/5279269
if ( (apollo codegen:generate ... 2>&1) | tee /dev/fd/2) | grep -i 'fail'; then
echo "Found 'fail' in command output, exiting..."
exit 1
fi
definitely not ideal, but works well. An improved version could instead check if there are any lines existing in stderr. Right now this script just checks for the keywords fail in both stdout and stderr for simplicty.