[英]Graphql: completely change field type in schema
I need to completely change field type. 我需要完全更改字段类型。
Situation: I have a gateway that combines multiple services. 情况:我的网关结合了多种服务。 It fetches their schemas with introspection, transforms them by adding namespaces to types (with apollo graphql-tools) and putting them in nested queries (eg Query: { ServiceName: { serviceQuery } }
) by building a new schema with ServiceNameQuery
types and setting delegate
resolvers. 它通过内省获取其架构,通过将名称空间添加到类型(使用apollo graphql-tools)并将其置于嵌套查询(例如Query: { ServiceName: { serviceQuery } }
)中,通过使用ServiceNameQuery
类型和设置delegate
来构建新架构,从而对其进行转换。解析器。 After that I need to create cross-service links. 之后,我需要创建跨服务链接。
I have ServiceA
that has a type with property related
, that contains ID from TargetType
from ServiceB
. 我有一个具有related
属性的类型的ServiceA
,其中包含来自ServiceB
TargetType
ID。 I need to change type of property related
from ID
to TargetType
and add a resolver, that will fetch data from ServiceB
. 我需要将related
ID
related
的属性类型更改为TargetType
并添加一个解析器,该解析器将从ServiceB
获取数据。
Here is a real-life example: 这是一个真实的例子:
To do that I run visitSchema
(from graphql-tools
transforms) over merged schema, find the field I need, fetch TargetType
from schema and change the field type and set a resolver like this: 为此,我在合并的架构上运行visitSchema
(来自graphql-tools
转换),找到我需要的字段,从架构中获取TargetType
并更改字段类型并设置一个解析器,如下所示:
const targetType = schema.getType('TargetType');
field.type = targetType;
field.astNode.type = targetType.astNode;
field.resolve = (parent, args, ctx, info) => {
console.log(parent);
return null;
};
When I run console.log the type over resulting schema before passing it to graphql server — I see that It's type and astNode did change and they are proper. 当我运行console.log时,将类型传递给graphql服务器之前,将其覆盖在生成的模式上-我看到它的类型和astNode确实发生了变化并且它们是正确的。
When I run introspection — I see that type of field is proper. 当我进行内省时,我发现该类型的字段是正确的。
But when I run a query like query { ServiceA { serviceQuery { id related { id } } } }
I get error on request parse stage: 但是,当我运行类似query { ServiceA { serviceQuery { id related { id } } } }
的查询时,在请求解析阶段出现错误:
Error: Field "related" must not have a selection since type "ID" has no subfields.
GraphQL request (5:19)
4: id
5: related {
^
6: id
at asErrorInstance (/.../node_modules/graphql/execution/execute.js:555:43)
at process._tickCallback (internal/process/next_tick.js:68:7)
It event doesn't try to call serviceQuery
resolver. 它不会尝试调用serviceQuery
解析器。
Where does it get the ID type of this field, when I changed the type definition? 当我更改类型定义时,它在哪里获得此字段的ID类型? Where more I need to change it's type? 我还需要在哪里更改它的类型?
UPD Even if I create new type like this: UPD即使我创建像这样的新类型:
schema = return visitSchema(schema, {
[VisitSchemaKind.OBJECT_TYPE](type: GraphQLObjectType) {
if (type.getFields().relatedNews) {
// console.log(type.getFields());
return new GraphQLObjectType({
name: type.name,
fields: {
...fromPairs(Object.values(type.getFields()).map(({name, type, args, resolve, description}) => [name, {type, args: {}, resolve, description}])),
relatedNews: {
type: schema.getType('NewsArticle'),
resolve(obj) {
console.log('foo');
}
}
}
});
}
return type;
},
});
I keep getting this error and resolver is not triggered. 我不断收到此错误,并且未触发解析器。 If I in the same manner add a new field with different name — that works. 如果我以相同的方式添加一个具有不同名称的新字段,则可以。 But if name is the same — error 但是,如果名称相同-错误
Ok, I've figured out. 好的,我知道了。 Stitched schema keeps track of underlying stitched schemas. 缝合模式会跟踪基础缝合模式。 Changing type at top-level schema doesn't affect it so leads to internal type conflict. 在顶级架构上更改类型不会对其造成影响,因此会导致内部类型冲突。 So type should be changed BEFORE merging 因此,在合并之前应更改类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.