Skip to content

Transforming input fields #1894

@MatthiasKunnen

Description

@MatthiasKunnen

I find myself short of an easy way to perform reusable transformation on input fields.

Say there is the following input object:

export const AddressInputType = new GraphQLInputObjectType({
    name: 'AddressInput',
    fields: () => ({
        name: {
            type: new GraphQLNonNull(GraphQLString),
        },
    }),
});

I would like to be able to do the following:

export const trim = (source, info) => source[info.fieldName].trim();
export const toUpperCase = (source, info) => source[info.fieldName].toUpperCase();

export const AddressInputType = new GraphQLInputObjectType({
    name: 'AddressInput',
    fields: () => ({
        name: {
            type: new GraphQLNonNull(GraphQLString),
            transform: [
                trim,
                toUpperCase,
            ],
        },
    }),
});

Returning undefined from a transform function should remove the field from the source object.

When to transform? I believe the transformation should occur after the type is parsed although arguments can be made to apply the transform before parsing. A potential solution is to have a transformBefore and a transformAfter property.

It should be possible to throw an error in the transform function. This should be handled similar to throwing an error in the type parser.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions