繁体   English   中英

从 graphql 接口继承解析器与 Apollo Server v3

[英]Inherit resolvers from graphql interface with Apollo Server v3

我想从 graphql 接口继承解析器。

考虑这个模式。

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

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

  type Query {
    getPerson: Person!
  }
}

这些解析器:

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

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

这是我的服务器

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);
}

我希望在查询服务器时

query Query {
  getPerson {
    name
    surname
  }
}

我得到了 John Doe,因为我希望PersonName继承解析器。

在 ApolloServer v.2 上,我通过inheritResolversFromInterfaces实现了此功能https://www.apollographql.com/docs/apollo-server/v2/api/graphql-tools/

我无法在 ApolloServer v3.0 上找到和等效项

我能找到的唯一选择是使用 @ graphql @graphql-tools/schema中的 makeExecutableSchema 创建模式,然后将该模式传递给 ApolloServer 构造函数:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM