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

fix(mapSchema): allow custom serialization function #5120

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/utils/src/rewire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ export function rewireTypes(
return new GraphQLInputObjectType(newConfig);
} else if (isEnumType(type)) {
const enumConfig = (type as GraphQLEnumType).toConfig();
return new GraphQLEnumType(enumConfig);
const newGraphQLEnum = new GraphQLEnumType(enumConfig);
if (type.serialize) {
newGraphQLEnum.serialize = type.serialize;
}
return newGraphQLEnum;
} else if (isScalarType(type)) {
if (isSpecifiedScalarType(type)) {
return type;
Expand Down
39 changes: 39 additions & 0 deletions packages/utils/tests/mapSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,45 @@ describe('mapSchema', () => {
expect(result.data?.['version']).toBe(1);
});

test('can add custom serializer to enum', () => {
const schema = buildSchema(/* GraphQL */ `
enum TestEnum {
VALUE1
VALUE2
}
type Query {
version: TestEnum
}
`);

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

expect(newSchema).toBeInstanceOf(GraphQLSchema);
const result = graphqlSync({
schema: newSchema,
source: /* GraphQL */ `
{
version
}
`,
});

expect(result.data?.['version']).toBe(testReturnVal);
});

test('can change the root query name', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
Expand Down