graphql-tools icon indicating copy to clipboard operation
graphql-tools copied to clipboard

transformSchema Map Enum Fields

Open teebszet opened this issue 5 years ago • 5 comments

Is it currently possible to use transformSchema to map enum field names?

I was hoping that RenameObjectFields was generic to allow this, but I doesn't seem to be.

Alternatively, I looked at implementing the Transform myself, but am now wondering if there is a simpler way to map enum field names.

My use case is I'm trying to translate enums in a kind of i18n way.

teebszet avatar Jun 11 '20 05:06 teebszet

Very possible, but requires a new transform that maps values in schema and translates in request and result...

PRs welcome

yaacovCR avatar Jun 11 '20 11:06 yaacovCR

It is potentially tricky to modify enum values as when transforming results you do not have actual access to field type, because of aliasing

When transforming the request, the request can be visited with a schema using upstream graphql-js visit function, but there is no visitResult, because the power of GraphQL is that it lets you change the result shape.

It would be interesting if you could ask GraphQL to give you an annotated result with the types but that is currently not spec, That would also help deal with overlapping fragments I think, but currently those are not allowed for the same reason, from what I recall.

Anyway, we now support a individual transform context object that can be used to save which paths are modified when transforming the request and can be read by the transform result function.

another approach is to encode the field types within the field names when transforming the request, That is basically how WrapFields/HoistField works

yaacovCR avatar Jun 11 '20 12:06 yaacovCR

Would be nice to just generally have a function that takes a document and result and visits the data and errors with the type info.

yaacovCR avatar Jun 11 '20 16:06 yaacovCR

This should be easily addressable with visitResult

One gotcha with type merging might be if enum type ast not recomputed from internal value asts, see https://github.com/ardatan/graphql-tools/commit/1dd9ae85a8fcb0418996c0931aac6c38c249d73b for similar fixes for fields

yaacovCR avatar Jul 13 '20 15:07 yaacovCR

Released in 6.0.14. Needs docs.

yaacovCR avatar Jul 17 '20 11:07 yaacovCR