apollographql/federation

subgraph-js: directive can't be used on custom scalar definition

velias opened this issue · 0 comments

velias commented

We need to add @tag directive on custom scalar in our subgraph schema to control contracts, unfortunately it disappears during the subgraph schema generation (we call buildSubgraphSchema() with typeDefs and resolvers provided). It happens only if resolver for that custom scalar type is also added. Without the resolver directive is present in the final schema.

Whole astNode looks to be removed from the custom scalar type during schema build.
I trace problem down to this code in the addResolversToSchema method.

for (const fn in fieldConfigs) {

I'm not fully sure what is this code doing/copiing, but what helped me is to prevent astNode to be copied if it is empty:

if (isScalarType(type)) {
    for (const fn in fieldConfigs) {
        // CUSTOMIZATION: skip astNode copying if empty to keep directives etc on the scalar type definition
        if (fn != "astNode" || (fieldConfigs as any)[fn])
            (type as any)[fn] = (fieldConfigs as any)[fn];
    }
}