问题描述
这是一个非常基本的问题,但是如何调用扩展类型或接口?
所有文档都指向使用扩展类型Person
来添加基于Person的字段.
我希望它能像这样
Employee扩展类型Person {薪水:诚信!}
但是文档显示它是这样的:
扩展类型Person {薪水:诚信!}
那么,我该如何查询员工薪水?如果有Person的多个扩展名,例如员工和房客?我想我可能会受到传统思维的束缚,但我希望扩展名会带来一些命名和可查询的内容.
extend
关键字有效地用于修改架构中的现有类型.这是最常见的两种情况:
1.连接表示单个模式的多个字符串.您可以将模式分解为多个文件,按域划分.然后,您可以执行以下操作:
#base.graphql输入查询{查看者:用户}#user.graphql扩展类型查询{用户:[用户!]!}#post.graphql扩展类型查询{发布:[发布!]!}
这将产生与以下内容相同的架构:
类型查询{查看者:用户用户:[用户!]!发布:[发布!]!}
2.从基础架构扩展..您可能在某些基础架构之上构建了多个架构.或者您可能是将远程模式缝合在一起.在这些情况下,我们经常想要添加特定于我们新模式的字段,这些字段在基本类型上不存在.这也可以用于实现基本架构中缺少的指令:
扩展类型SomeType @customDirective
extend
关键字只能修改现有类型;它不是继承的手段.实际上, GraphQL不支持类型继承.接口提供了对现有类型的抽象级别,但是实现接口的类型不会从该接口继承任何字段.除非您使用 graphql-s2s 之类的库,否则无法做到这一点..>
This is a very basic question but how do you call an extended type or interface?
All the documentations points to using extend type Person
to add fields based on Person.
I would expect it to work like this
Employee extend type Person {
salary: Int!
}
But the documentation suggests it's like this:
extend type Person{
salary: Int!
}
So, how do I query for an Employee salary? What if there are multiple extensions of Person, e.g. Employee and Renter? I think I might be hampered by traditional thinking but I would expect the extension to result in something named and queryable.
The extend
keyword is effectively used to modify an existing type within a schema. This is most commonly used in two scenarios:
1. Concatenating multiple strings that represent a single schema. You can have your schema broken up across multiple files, divided by domain. You can then do something like:
#base.graphql
type Query {
viewer: User
}
# user.graphql
extend type Query {
users: [User!]!
}
# post.graphql
extend type Query {
post: [Post!]!
}
This results in a schema that's effectively the same as:
type Query {
viewer: User
users: [User!]!
post: [Post!]!
}
2. Extending from a base schema. You might have multiple schemas that build on top of some base schema. Or you might be stitching together remote schemas. In these scenarios, we often want to add fields specific to our new schema that don't exist on the base types. This can be used to implement directives that are missing from the base schema as well:
extend type SomeType @customDirective
The extend
keyword can only modify existing types; it is not a vehicle for inheritance. In fact, GraphQL does not support type inheritance. Interfaces provide a level of abstraction over existing types, but types that implement an interface do not inherit any fields from that interface. There's no way to do that, unless you use some library like graphql-s2s.
这篇关于Graphql数据建模:扩展类型和接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!