Skip to content
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

Preserve extended methods from class-based types in SchemaExtender::extend() #934

Merged
merged 13 commits into from
Sep 10, 2021

Conversation

abdullahseba
Copy link
Contributor

@abdullahseba abdullahseba commented Sep 8, 2021

Resolves #933

@spawnia
Copy link
Collaborator

spawnia commented Sep 8, 2021

It seems like extending class-based scalar types does not work at all as of now, given the implementation of SchemaExtender:

protected static function extendScalarType(ScalarType $type): CustomScalarType
{
return new CustomScalarType([
'name' => $type->name,
'description' => $type->description,
'astNode' => $type->astNode,
'serialize' => $type->config['serialize'] ?? null,
'parseValue' => $type->config['parseValue'] ?? null,
'parseLiteral' => $type->config['parseLiteral'] ?? null,
'extensionASTNodes' => static::getExtensionASTNodes($type),
]);
}

The $config values do not exist in a class. How else could we extend it then? Perhaps using closures to reference the orginal scalar's methods?

'serialize' => static fn ($value) => $type->serialize($value),

@abdullahseba
Copy link
Contributor Author

I've implemented a fix using closures as you suggested and it doesn't appear to break any tests. But at the same time, the testing for scalars is probably not compleate? Only serialize() is really tested.

@codecov
Copy link

codecov bot commented Sep 9, 2021

Codecov Report

Merging #934 (472d0f6) into master (2abac8c) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #934      +/-   ##
============================================
+ Coverage     94.16%   94.18%   +0.01%     
  Complexity       50       50              
============================================
  Files           117      117              
  Lines          9685     9659      -26     
============================================
- Hits           9120     9097      -23     
+ Misses          565      562       -3     
Impacted Files Coverage Δ
src/Type/Definition/InterfaceType.php 97.82% <ø> (ø)
src/Type/Definition/UnionType.php 95.45% <ø> (ø)
src/Type/Definition/ObjectType.php 98.11% <100.00%> (ø)
src/Utils/SchemaExtender.php 99.02% <100.00%> (+0.52%) ⬆️
src/Type/Definition/CustomScalarType.php 95.65% <0.00%> (+4.34%) ⬆️

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 2abac8c...472d0f6. Read the comment docs.

@spawnia
Copy link
Collaborator

spawnia commented Sep 9, 2021

I've implemented a fix using closures as you suggested and it doesn't appear to break any tests. But at the same time, the testing for scalars is probably not compleate? Only serialize() is really tested.

I cut down the test case to be as minimal as possible and cover all three methods.

@abdullahseba abdullahseba changed the title Add class-defined scalar test case for SchemaExtenderTest Add class-defined scalar test case for and fix #933 Sep 9, 2021
@abdullahseba abdullahseba changed the title Add class-defined scalar test case for and fix #933 Add class-defined scalar test case and fix #933 Sep 9, 2021
@spawnia spawnia changed the title Add class-defined scalar test case and fix #933 Preserve extended methods from class-based types in SchemaExtender::extend() Sep 9, 2021
@spawnia spawnia requested a review from simPod September 9, 2021 08:46
Copy link
Collaborator

@simPod simPod left a comment

Choose a reason for hiding this comment

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

no objections, some nits

👍🏾

src/Type/Definition/ObjectType.php Outdated Show resolved Hide resolved
src/Utils/SchemaExtender.php Outdated Show resolved Hide resolved
}
}
}

return $interfaces;
}

protected static function extendType($typeDef)
/**
* @param ListOfType|NonNull|(Type &NamedType) $typeDef
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* @param ListOfType|NonNull|(Type &NamedType) $typeDef
* @param ListOfType|NonNull|(Type&NamedType) $typeDef

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would prefer that, PHP_CS keeps messing it up. I don't like that tool, php-cs-fixer does a much better job in my experience.

src/Utils/SchemaExtender.php Outdated Show resolved Hide resolved
src/Utils/SchemaExtender.php Outdated Show resolved Hide resolved
src/Utils/SchemaExtender.php Outdated Show resolved Hide resolved
tests/Utils/SomeInterfaceClassType.php Outdated Show resolved Hide resolved
@spawnia spawnia merged commit b85f8a0 into webonyx:master Sep 10, 2021
@abdullahseba abdullahseba mentioned this pull request Jan 20, 2022
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.

SchemaExtender Not Loading Custom Scalar Types Correctly
3 participants