Skip to content

Throw SerializationError over client safe Error when failing to serialize leaf types #903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 3, 2021

Conversation

spawnia
Copy link
Collaborator

@spawnia spawnia commented Jul 29, 2021

Resolves #570

@spawnia spawnia requested review from vladar and simPod July 29, 2021 15:37
@codecov
Copy link

codecov bot commented Jul 29, 2021

Codecov Report

Merging #903 (8a67f41) into master (d8ccdfe) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #903      +/-   ##
==========================================
- Coverage   94.11%   94.10%   -0.01%     
==========================================
  Files         117      117              
  Lines        9679     9675       -4     
==========================================
- Hits         9109     9105       -4     
  Misses        570      570              
Impacted Files Coverage Δ
src/Executor/Promise/Adapter/AmpPromiseAdapter.php 84.21% <ø> (ø)
...c/Executor/Promise/Adapter/ReactPromiseAdapter.php 0.00% <ø> (ø)
...rc/Executor/Promise/Adapter/SyncPromiseAdapter.php 91.93% <ø> (ø)
src/Type/Definition/CustomScalarType.php 91.30% <ø> (ø)
src/Type/Definition/TypeWithFields.php 100.00% <ø> (ø)
src/Type/Definition/BooleanType.php 100.00% <100.00%> (ø)
src/Type/Definition/EnumType.php 81.57% <100.00%> (ø)
src/Type/Definition/FloatType.php 100.00% <100.00%> (ø)
src/Type/Definition/IDType.php 100.00% <100.00%> (ø)
src/Type/Definition/IntType.php 100.00% <100.00%> (ø)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d8ccdfe...8a67f41. Read the comment docs.

@spawnia spawnia changed the title Throw SerializationError over client safe Error when failing to serialize scalars Throw SerializationError over client safe Error when failing to serialize leaf values Jul 30, 2021
@spawnia spawnia changed the title Throw SerializationError over client safe Error when failing to serialize leaf values Throw SerializationError over client safe Error when failing to serialize leaf types Jul 30, 2021
@spawnia
Copy link
Collaborator Author

spawnia commented Jul 30, 2021

I let the SerializationError thrown in Enum serialization bubble up, given what the spec says at http://spec.graphql.org/draft/#sec-Enums.Result-Coercion

GraphQL services must return one of the defined set of possible values. If a reasonable coercion is not possible they must raise a field error.

'String cannot represent value: ' . Utils::printSafe($value)
);
}

return (string) $value;
}

/**
* @param mixed $value
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep those mixed so they can be converted to (mixed $value) on php 8 automatically.

With this change we wouldn't be able to distinguish intentional mixed versus forgotten type. Also, consumers can @inheritDoc

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parent method still has the explicit mixed annotation.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the thing with php is to specify types even in implementations but that's related to previous discussion on disabling missing type checks.
We can have @inheritdoc here so it explicitly says we want mixed here.

With this change it's ambiguous: is it forgotten or inherited? What if parent type narrows, should it be narrowed here as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good tooling I use assumes the parent PHPDoc is inherited, unless specifically overwritten. Perhaps we can document and reify that assumption as part of the contribution guidelines?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do as you think is the best but personally I don't like trading automated checks for a piece of text few people read 🤷🏾‍♀️

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fairly confident we will keep this project moving towards stronger types without duplicating code.

spawnia added 2 commits August 2, 2021 10:57
# Conflicts:
#	CHANGELOG.md
#	src/Type/Definition/BooleanType.php
#	src/Type/Definition/FloatType.php
#	src/Type/Definition/IDType.php
#	src/Type/Definition/IntType.php
#	src/Type/Definition/StringType.php
#	tests/Executor/TestClasses/ComplexScalar.php
@spawnia spawnia merged commit 5a720a8 into master Aug 3, 2021
@spawnia spawnia deleted the scalar-serialization-error branch August 3, 2021 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scalar types should throw exception different from the base Exception
2 participants