How to merge application resolvers with dynamic resolver in NestJS using ApolloFederationDriver

How to merge application resolvers with dynamic resolver in NestJS using ApolloFederationDriver


0

I am developing an application that requires dynamic schema using NestJS and the Apollo GraphQL module that comes integrated with it. To achieve this, I am creating type definitions and resolvers based on user configuration.

Everything works correctly when I use the ApolloDriver, but when I switch to the ApolloFederationDriver, problems arise. Let me explain, I have the following module:

@Module({
    imports: [
        GraphQLModule.forRoot<ApolloDriverConfig>({
            driver: ApolloDriver,
            typeDefs: createDynamicTypeDefs(),
            resolvers: createDynamicResolvers(),
        }),
    ],
    exports: [DataResolver, DataService],
    providers: [DataResolver, DataService],
})
export class DataModule { }

Everything works correctly, and I can access the definitions and resolvers for all Queries and Mutations. However, when I switch to ApolloFederationDriver, I lose access to the resolvers defined in DataResolver:

@Resolver('Datum')
export class DataResolver {
    constructor(private readonly dataService: DataService) { }

    @Query('data')
    findAll() {
        return this.dataService.findAll();
    }
}

If I remove resolvers from the Apollo configuration, then the resolver defined in DataResolver works again.

I would like to know how I can achieve the same behavior regarding the resolvers I have when using ApolloDriver but using ApolloFederationDriver. What I want is to be able to use the resolvers added by configuration and the resolvers defined in my application while allowing federation of these.

An alternative could be to obtain the resolvers defined in my application and merge them with the dynamic resolvers, so everything would work. But how can I do this?

One option I found was to create resolvers manually that point to the functions in DataResolver, something like this:

const resolvers = {
    Query: {
        data: this.dataResolver.findAll.bind(this.dataResolver),
        datum: (parent, args, contextValue, info) => {
            return this.dataResolver.findOne(args.id);
        },
    }
};

It works, but essentially, I would have to create a wrapper for each resolver, losing the benefits of creating resolvers with their decorators, etc.

Is there any option to combine the resolvers or access them to merge them with the dynamic resolvers?

Thanks

New contributor

Magnus is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 4 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *