maticzav / graphql-middleware

Split up your GraphQL resolvers in middleware functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

applyMiddleware + __resolveReference

alex-oleksiiuk opened this issue · comments

Good evening everybody! I have a federated schema the looks like this:

const resolveItemReference = async ({ id }: Pick<Item, 'id'>) => this.itemRepo.findByPk(id);

const rawSchema = await buildFederatedSchema(
    {
      resolvers: [__dirname + '/../resolvers/**/*.ts'],
      orphanedTypes: [Item],
    },
    {
      Item: { __resolveReference: resolveItemReference },
    });

If I pass rawSchema to the ApolloServer initiation, I can hit this reference resolver from another service via gateway, and it works fine. But when I need to bundle this schema with permissions from graphql-shield wrapping them with applyMiddleware call, I can no longer access this reference resolver:
const schema = applyMiddleware(rawSchema, permissions); - schema is then passed to the new ApolloServer call

The orphanedType Item is still available to the gateway, but when I try to query any of the Item fields via reference resolver, I am getting null for each of them. Also, the reference resolver function is not getting invoked at all.

Please share your thoughts on how this can be fixed. Thanks for your help.

I am having the same issue. @aoleksiiuk-karsun Have you found any workaround?

@shayzun yes, I've managed to make it work - you need to remove reference resolvers from the buildFederatedSchema call, and then add them with addResolversToSchema call. The working setup of the example above looks as follows:

import { addResolversToSchema } from 'apollo-graphql';

const resolveItemReference = async ({ id }: Pick<Item, 'id'>) => this.itemRepo.findByPk(id);

// 1. build federated schema
const rawSchema = await buildFederatedSchema({
      resolvers: [__dirname + '/../resolvers/**/*.ts'],
      orphanedTypes: [Item],
    });

// 2. apply shield permissions
 schema = applyMiddleware(schema, permissions);

// 3. create an object with reference resolvers
 const referenceResolvers = {
    Item: { __resolveReference: resolveItemReference }
  };

// 4. add them to the schema
addResolversToSchema(schema, referenceResolvers);

With this shield'ed queries, mutations and types are still protected, and you have your reference resolvers as part of the schema. Hope this helps. Cheers!

Has anyone gotten this working recently? buildSubgraphSchema doesn't take an orphanedTypes param. Anyone have a solution for the latest libs? I'm on federation 0.33 and apollo-server 3.5

Having same issue @dborstelmann. Did you ever find a fix for this using the newer version of apollo-server and federation?