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

Introduced support of custom scalar types for GraphQL schema #34651

Closed

Conversation

akaplya
Copy link
Contributor

@akaplya akaplya commented Nov 16, 2021

Support for GraphQL custom scalars

This PR adds support for custom scalar types to GraphQL.

https://graphql.org/learn/schema/#scalar-types
https://webonyx.github.io/graphql-php/type-definitions/scalars/#writing-custom-scalar-types

After PR is merged a developer will be able to define custom scalar types and implementations by using Magento schema.

For example, the following declaration enables Json scalar type

scalar Json
@implementation(class: "Magento\\MyModule\\Model\\JsonScalarType")
@doc(description:"Json type")
{}

Scalar type implementation should implement new Magento\Framework\GraphQl\Schema\Type\Scalar\CustomScalarInterface

Something like:

class JsonScalarType implements \Magento\Framework\GraphQl\Schema\Type\Scalar\CustomScalarInterface
{

    public function serialize($value)
    {
        return json_decode($value);
    }

    public function parseValue($value)
    {
        return json_encode($value);
    }

    public function parseLiteral($valueNode, ?array $variables = null)
    {
        return $valueNode->value;
    }
}

For example, a query that accepts JSON as input and returns JSON as output and has a resolver that returns you an argument that you just passed then you will see a picture similar to this:

Screenshot from 2021-11-16 16-41-42

This feature could be widely useful to express values that may have validation, like HTML, emails, URLs, dates, etc., and for more advanced cases like EAV attributes, where value can be expressed as a single scalar or array of scalars.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Resolved issues:

  1. resolves [Issue] Introduced support of custom scalar types for GraphQL schema #36877: Introduced support of custom scalar types for GraphQL schema

@m2-assistant
Copy link

m2-assistant bot commented Nov 16, 2021

Hi @akaplya. Thank you for your contribution
Here are some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here

ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.

For more details, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@akaplya
Copy link
Contributor Author

akaplya commented Nov 16, 2021

@mageno run all tests

@sidolov sidolov added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Nov 18, 2021
@akaplya akaplya closed this Nov 23, 2021
@akaplya akaplya reopened this Nov 23, 2021
@convenient
Copy link
Contributor

Just spotted this and this additional type will need to be considered when #31879 is reviewed.

@engcom-Charlie engcom-Charlie self-assigned this Nov 9, 2023
@engcom-Charlie
Copy link
Contributor

Hi @akaplya,

Thank you for your contribution!

Can you please resolve the conflicts in order to proceed further.

Thank you!

@engcom-Charlie
Copy link
Contributor

engcom-Charlie commented Nov 10, 2023

I have resolved the conflict and merged magento 2.4-develop

@engcom-Charlie
Copy link
Contributor

@magento run all tests

Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Charlie
Copy link
Contributor

@magento run WebAPI Tests

Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Charlie
Copy link
Contributor

@magento run all tests

Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Charlie
Copy link
Contributor

@magento run WebAPI Tests, Integration Tests, Static Tests

Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@m2-community-project m2-community-project bot added Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. labels Mar 8, 2024
@engcom-Bravo
Copy link
Contributor

Hello,

Internal team has started to work on it.

Thanks.

@engcom-Charlie
Copy link
Contributor

As per this comment, moving it to On Hold.

@Priyakshic Priyakshic added the Project: Community Picked PRs upvoted by the community label Nov 13, 2024
@engcom-Hotel
Copy link
Contributor

Moving this PR On Hold as per this #34651 (comment)

@engcom-Charlie
Copy link
Contributor

It seems that, this PR got merged in 0574ac2. Hence closing this now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: GraphQL GraphQL Component: GraphQlSchemaStitching Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Project: Community Picked PRs upvoted by the community Release Line: 2.4
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Issue] Introduced support of custom scalar types for GraphQL schema