简体   繁体   English

Graphql:完全更改架构中的字段类型

[英]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.

相关问题 GraphQL-有条件地确定架构中字段的类型 - GraphQL- conditionally determine the type of a field in a schema 如何在 GraphQL 模式中使用“十进制”作为字段类型? - How to use 'Decimal' as a field type in GraphQL schema? 在 GraphQL 模式中创建类型时是否可以重命名字段? - Is it possible to rename a field when creating a type within a GraphQL schema? GraphQL 模式 - 是否可以在自定义类型字段上设置约束 - GraphQL schema - is it possible to set constraints on a custom type field GraphQL / Relay架构无法在“CreateLinkPayload”类型上查询字段“store” - GraphQL/Relay Schema Cannot query field “store” on type “CreateLinkPayload” 如何在 GraphQL 模式指令(Node.js、graphql-tools)中获取字段类型 - How to get the field type in a GraphQL schema directive (Node.js, graphql-tools) 使用 Hasura graphql 模式时如何自定义 graphql-code-generator 生成字段的类型 - How to customize the type for a graphql-code-generator produced field when consuming a Hasura graphql schema graphql 中未知对象的模式类型 - schema type for an unknown object in graphql 定义GraphQL模式时,获取字段类型必须为Output Type错误 - Getting field type must be Output Type error when defining a GraphQL schema GraphQL模式测试:从模式到对象类型 - GraphQL Schema Testing: From Schema to Object Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM