我有一个类似下面的查询
query MyQuery($id: ID!) {
something(id: $id) {
details {
id
name
}
}
}
和类型。图中有
type something {
details: // this should either type details1 or type details2
}
type details1 {
id: string
name: string
description: string
}
type details2 {
id: string
name: string
description: string
other: string!
}
所以类型details可以是类型details1或details2
如何在type something for details字段中指定。
有人能帮我一下吗?谢谢。编辑:
我尝试过下面的操作
type something {
details : details1 | details2;
}
但是这行不通
我能做的就是创建另一个类型type details3 {
id: string
name: string
description: string
other: string
}
是details2和details3的组合
,像
一样使用type something {
details: details3
}
但不想这样做。
使用union
type something {
details : details;
}
union details = details1 | details2
我想你在定义类型时错过了=
符号,我刚刚试过这个,它工作:
type details1 = {
id: string
name: string
description: string
}
type details2 = {
id: string
name: string
description: string
other: string
}
type something = {
details: details1 | details2;
}
const test: something = {
details: {
id: 'abc',
name: 'abc',
description: 'abc',
}
}
const test2: something = {
details: {
id: 'abc',
name: 'abc',
description: 'abc',
other: 'abc'
}
}