Alberto Valero
Alberto Valero

Reputation: 440

Inherit resolvers from graphql interface with Apollo Server v3

I would like to inherit resolvers from a graphql interface.

Consider this schema.

const typeDefs = gql`
  interface Name {
    name: String!
    surname: String!
  }

  type Person implements Name {
    _id: ID!
    name: String!
    surname: String!
  }

  type Query {
    getPerson: Person!
  }
}

And these resolvers:

const queryResolver = {
  Name: {
    name: () => "John",
    surname: () => "Doe"
  },

  Query: {
    getPerson: async (parent, args, context, info) => {
      return {
        _id: "1",
      };
    },
  }
}

This is my server

const { ApolloServer } = require("apollo-server");

const typeDefs = require("./types");
const queryResolvers = require("./resolvers/query");

const resolvers = {
  Query: queryResolvers.Query,
  Name: queryResolvers.Name,
};
try {
  const server = new ApolloServer({
    typeDefs,
    resolvers,
  });
  server.listen().then(({ url }) => {
    console.log(`Apollo server listening on ${url}`);
  });
} catch (e) {
  console.error(e);
}

I would like that when querying the server

query Query {
  getPerson {
    name
    surname
  }
}

I get John Doe, as I would expect that Person inherits the resolvers from Name.

On ApolloServer v.2 I get this functionality implemented through inheritResolversFromInterfaces https://www.apollographql.com/docs/apollo-server/v2/api/graphql-tools/

I have not been able to find and equivalent on ApolloServer v3.0

Upvotes: 1

Views: 333

Answers (1)

Xmasjos
Xmasjos

Reputation: 21

The only option I could find is by creating the schema by using makeExecutableSchema from the @graphql-tools/schema, and then passing that schema to the ApolloServer constructor:

const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
    inheritResolversFromInterfaces: true,
});

const server = new ApolloServer({
    ...otherApolloArguments,
    schema,
});

Upvotes: 0

Related Questions