是否可以在GraphQL中实现多个接口?
是否可以指定在GraphQL模式中实现多个接口的类型?如果是,这将如何实现?是的。如报告所述: 对象类型可以声明它实现一个或多个唯一接口 请记住,结果对象必须是“它实现的所有接口的超集”——它必须实现每个接口拥有的所有字段,并且这些字段不能冲突。例如,如果接口A和接口B都有一个名为是否可以在GraphQL中实现多个接口?,graphql,Graphql,是否可以指定在GraphQL模式中实现多个接口的类型?如果是,这将如何实现?是的。如报告所述: 对象类型可以声明它实现一个或多个唯一接口 请记住,结果对象必须是“它实现的所有接口的超集”——它必须实现每个接口拥有的所有字段,并且这些字段不能冲突。例如,如果接口A和接口B都有一个名为something的字段,则该字段的类型对于两个接口都必须相同,以便对象类型实现两个接口 这里有一个简单的例子,你可以和它一起玩 注意:正如其他人指出的,不再支持使用逗号分隔接口——请改用和(与号) const{Apo
something
的字段,则该字段的类型对于两个接口都必须相同,以便对象类型实现两个接口
这里有一个简单的例子,你可以和它一起玩
注意:正如其他人指出的,不再支持使用逗号分隔接口——请改用和(与号)
const{ApolloServer,gql}=require(“阿波罗服务器”);
常量typeDefs=gql`
类型查询{
动物:动物!
小鸟:小鸟!
}
接口鸟{
翼展:Int!
}
界面动物{
速度:Int!
}
燕子类动物和鸟类{
翼展:Int!
速度:Int!
}
`;
常量解析程序={
查询:{
someAnimal:(根、参数、上下文)=>{
返回{翼展7,速度24};
},
someBird:(根、参数、上下文)=>{
返回{翼展:6,速度:25};
}
},
鸟:{
__resolveType:()=>“吞咽”
},
动物:{
__resolveType:()=>“吞咽”
}
};
const server=新服务器({
typeDefs,
解析器
});
server.listen().then({url})=>{
log(`似乎用逗号分隔接口不再起作用了。我不得不用“&”来让它起作用(Apollo),看到这个答案了吗
我认为是的,这应该是可能的,因为规范中描述了
这是一个例子
根据规范的最新版本():“类型也可以实现多个接口。例如,Business
实现[下面]示例中的NamedEntity
和ValuedEntity
接口。”type Business根据规范的最新版本实现NamedEntity和ValuedEntity{
:“类型也可以实现多个接口。例如,Business
实现[下面]示例中的NamedEntity
和ValuedEntity
接口。”type Business根据规范的最新版本实现NamedEntity和ValuedEntity{
:”类型也可以实现多个接口。例如,Business
实现[下面]示例中的NamedEntity
和ValuedEntity
接口。“type Business实现NamedEntity和ValuedEntity{
const { ApolloServer, gql } = require("apollo-server");
const typeDefs = gql`
type Query {
someAnimal: Animal!
someBird: Bird!
}
interface Bird {
wingspan: Int!
}
interface Animal {
speed: Int!
}
type Swallow implements Animal & Bird {
wingspan: Int!
speed: Int!
}
`;
const resolvers = {
Query: {
someAnimal: (root, args, context) => {
return { wingspan: 7, speed: 24 };
},
someBird: (root, args, context) => {
return { wingspan: 6, speed: 25 };
}
},
Bird: {
__resolveType: () => "Swallow"
},
Animal: {
__resolveType: () => "Swallow"
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
server.listen().then(({ url }) => {
console.log(`It seems comma separating the interfaces doesn't work anymore. I had to use "&" instead to make it work (Apollo), see this answer https://stackoverflow.com/a/49521662/1959584
type Something implements First & Second
interface NamedEntity {
name: String
}
interface ValuedEntity {
value: Int
}
type Person implements NamedEntity {
name: String
age: Int
}
type Business implements NamedEntity & ValuedEntity {
name: String
value: Int
employeeCount: Int
}