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

Implement GraphQL custom scalars #1572

Closed
Torres-ssf opened this issue Dec 20, 2023 · 3 comments
Closed

Implement GraphQL custom scalars #1572

Torres-ssf opened this issue Dec 20, 2023 · 3 comments
Assignees
Labels
feat Issue is a feature

Comments

@Torres-ssf
Copy link
Contributor

Torres-ssf commented Dec 20, 2023

Implement GraphQL custom scalars for Efficient Data Type Conversion.

We need to make use of GraphQL custom scalars to automatically handle simple data parsing.

For instance, consider the parsing from a String to BN.

Currently, all BN data retrieved via GraphQL is in string format, always requiring a manual conversion to BN.

Implementing custom scalars will eliminate the need for these repetitive manual conversions. Below is an illustrative example:

Warning

This code is provided as an example and should not be considered the final solution.

// Implementation of the BigNumber scalar
const bigNumberScalar = new GraphQLScalarType({
  name: 'BN',
  description: 'A custom scalar type for BN',
  serialize(value) {
    // Converts BN to a string for the response
    return value.toString();
  },
  parseValue(value) {
    // Transforms an incoming string into BN
    return bn(value);
  },
  parseLiteral(ast) {
    if (ast.kind === 'StringValue') {
      return bn(ast.value);
    }
    return null;
  }
});

// Definition of resolvers
const resolvers = {
  BN: bigNumberScalar,
  // Additional resolvers can be defined here
};

Note

@Torres-ssf Torres-ssf added the feat Issue is a feature label Dec 20, 2023
@arboleya arboleya changed the title Using GraphQL Custom Scalars for Efficient Data Type Conversion Implement GraphQL custom scalars Dec 20, 2023
@arboleya arboleya added this to the 2 - Beetle milestone Jan 8, 2024
@arboleya arboleya modified the milestones: 2 - Beetle, 3 - Wasp, 4 - Cricket Jan 31, 2024
@arboleya
Copy link
Member

arboleya commented Feb 1, 2024

@Dhaiwat10 @Torres-ssf Do you know if there is any overlapping between this issue and #1570?

@Torres-ssf
Copy link
Contributor Author

@arboleya There is no overlapping between these 2

1 - I will close and create another issue in favor of this one today. I've misinterpreted how the problem presented by the FE feedback could be solved on our side. It seems that implementing a custom scalar can only be done on the server side. I will write the specs for the new issue later today.

2 - #1570 is not related to this one. However, It needs some investigation to validate if graphQL resolvers can be used on a node client-side application.

@Torres-ssf
Copy link
Contributor Author

Closing this issue since it is not possible to define Custom Scalars on a GraphQL client-side Node application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Issue is a feature
Projects
None yet
Development

No branches or pull requests

2 participants