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

mapSchema doesn't apply custom serialisation function to the schema #5119

Closed
4 tasks done
rupert648 opened this issue Mar 16, 2023 · 2 comments
Closed
4 tasks done

mapSchema doesn't apply custom serialisation function to the schema #5119

rupert648 opened this issue Mar 16, 2023 · 2 comments

Comments

@rupert648
Copy link

Issue workflow progress

Progress of the issue based on the Contributor Workflow

  • 1. The issue provides a reproduction available on Github, Stackblitz or CodeSandbox
  • 2. A failing test has been provided
  • 3. A local solution has been provided
  • 4. A pull request is pending review

Describe the bug
When adding a custom serialization function to GraphQLEnumType, this serialization function is overwritten by the default serialization function, meaning it is not currently possible to apply a custom serialization function.

To Reproduce
A repo showcasing this bug can be found here https://github.com/rupert648/graphql-tools-serialization-bug-demo
To run the code in this repo, run yarn && yarn start. Otherwise follow the following instructions.

Create a schema with an enum

const schema = buildSchema(/* GraphQL */ `
      enum TestEnum {
        VALUE1
        VALUE2
      }
      type Query {
        version: TestEnum
      }
    `);

Use mapSchema on MapperKind.ENUM_TYPE, and overwrite the GraphQLEnumType serialization function. Alongside this add a resolver for the query

const newSchema = mapSchema(schema, {
      [MapperKind.ENUM_TYPE]: enumType => {
        enumType.serialize = function newSerialize(_outputValue: unknown): string {
          return "new value";
        };
        return enumType;
      },
     [MapperKind.QUERY]: type => {
        const queryConfig = type.toConfig();
        queryConfig.fields['version'].resolve = () => 'VALUE1';
        return new GraphQLObjectType(queryConfig);
      },
});

Run the query against the schema

const result = graphqlSync({
      schema: newSchema,
      source: /* GraphQL */ `
        {
          version
        }
      `,
    });
console.log(result.data) // { value: "VALUE1" }

the expected output is { value: "new value }`

Expected behavior

I expect the custom serialisation function specified to be called on the enum after the resolver has finished.

Environment:

  • OS: macOS Venture 13.2.1
  • @graphql-tools/schema : "9.0.13":
  • NodeJS: v16.17.0

Additional context

We want to use this to map unknown values returned from datasources to 'UNKNOWN' rather than throwing an error. This would provide a way of doing this globally without having to manually do this at every resolver.

@ardatan
Copy link
Owner

ardatan commented Mar 29, 2023

Thanks for the PR but I don't think we should hijack into graphql-js classes in graphql-tools.
Enum types are not intended to have a custom serialization methods like scalars. Enum's serialization method should be all about internal to external values in my opinion. This looks a bit hack to me.
You can also see here the configuration for enum types doesn't allow to pass a custom serialization method.
https://graphql.org/graphql-js/type/#graphqlenumtype
So this is not an issue with graphql-tools.

@ardatan ardatan closed this as completed Mar 29, 2023
@rupert648
Copy link
Author

Thanks for the feedback, coming back to this issue I would agree with your response. We've since reassessed why we wanted this functionality in the first place and come to the realisation that our use of enums might have been innappropriate in this case. Using a string or a custom scalar would definitely be the correct approach.

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

No branches or pull requests

2 participants