简体   繁体   English

GraphQL 模式拼接选择集中是否禁止使用列表类型的字段?

[英]Are fields with list types forbidden in GraphQL schema stitching selection sets?

I have an array of entities that look like this:我有一组看起来像这样的实体:

const aEntities = [
    {
        id: 1,
        name: 'Test',
        oneToManyRelation: [
            {
                id: 2
            },
            {
                id: 3
            }
        ],
        oneToOneRelation: {
            id: 1
        }
    }
];

The entities are represented by the type AType .实体由类型AType表示。 I want to make an extension of this type in a separate subschema and prove that it is possible to add fields that derive their values from the contents of oneToOneRelation and oneToManyRelation respectively.我想在一个单独的子模式中扩展这种类型,并证明可以添加分别从oneToOneRelationoneToManyRelation的内容派生其值的字段。

The following schema, implementing a derived field based on oneToOneRelation , works fine:以下架构,实现基于oneToOneRelation的派生字段,工作正常:

const aSchema = makeExecutableSchema({
    resolvers: {
        Query: {
            aEntities: () => aEntities
        }
    },
    schemaTransforms: [stitchingDirectivesValidator],
    typeDefs: gql`
        ${allStitchingDirectivesTypeDefs}

        type AType {
            id: ID!
            name: String!
            oneToOneRelation: AEmbeddedType!
        }

        type AEmbeddedType {
            id: ID!
        }

        type Query {
            aEntities: [AType!]!
        }
    `
});

const bSchema = makeExecutableSchema({
    resolvers: {
        AType: {
            oneToOneId: ({ oneToOneRelation }) => oneToOneRelation.id
        },
        Query: {
            aEntities_fromBSchema: (_, { keys }) => keys,
        }
    },
    schemaTransforms: [stitchingDirectivesValidator],
    typeDefs: gql`
        ${allStitchingDirectivesTypeDefs}

        type AType @key(selectionSet: "{ oneToOneRelation { id } }") {
            oneToOneId: String!
        }

        scalar Key

        type Query {
            aEntities_fromBSchema(keys: [Key!]!): [AType!]! @merge
        }
    `
})

const schema = stitchSchemas({
    subschemaConfigTransforms: [stitchingDirectivesTransformer],
    subschemas: [
        {
            schema: aSchema
        },
        {
            schema: bSchema,
        }
    ]
})

But once I add oneToManyRelation { id } to the selectionSet i run into problems:但是一旦我将oneToManyRelation { id }添加到 selectionSet 中,我就会遇到问题:

const aSchema = makeExecutableSchema({
    resolvers: {
        Query: {
            aEntities: () => aEntities
        }
    },
    schemaTransforms: [stitchingDirectivesValidator],
    typeDefs: gql`
        ${allStitchingDirectivesTypeDefs}

        type AType {
            id: ID!
            name: String!
            oneToManyRelation: [AEmbeddedType!]!
            oneToOneRelation: AEmbeddedType!
        }

        type AEmbeddedType {
            id: ID!
        }

        type Query {
            aEntities: [AType!]!
        }
    `
});

const bSchema = makeExecutableSchema({
    resolvers: {
        AType: {
            oneToManyIds: ({ oneToManyRelation }) => oneToManyRelation.map(({ id }) => id),
            oneToOneId: ({ oneToOneRelation }) => oneToOneRelation.id
        },
        Query: {
            aEntities_fromBSchema: (_, { keys }) => keys,
        }
    },
    schemaTransforms: [stitchingDirectivesValidator],
    typeDefs: gql`
        ${allStitchingDirectivesTypeDefs}

        type AType @key(selectionSet: "{ oneToOneRelation { id }, oneToManyRelation { id } }") {
            oneToOneId: String!
            oneToManyIds: [String!]!
        }

        scalar Key

        type Query {
            aEntities_fromBSchema(keys: [Key!]!): [AType!]! @merge
        }
    `
})

I get the following error:我收到以下错误:

oneToManyRelation.map is not a function

And when I log the keys parameter in the aEntities_fromBSchema resolver it seems that oneToManyRelation haven't been resolved to be an array at all, but rather an (empty) object:当我在aEntities_fromBSchema解析器中记录 keys 参数时,似乎oneToManyRelation根本没有被解析为一个数组,而是一个(空)object:

[
  {
    oneToOneRelation: [Object: null prototype] { id: '1' },
    oneToManyRelation: [Object: null prototype] { id: undefined },
    __typename: 'AType'
  }
]

Is referencing list types in key selection sets simply forbidden as of graphql-tools v 7.0.2 ?graphql-tools v 7.0.2起,是否禁止在键选择集中引用列表类型? It looks like I actually can circumvent the issue by using a subschema merge config defined outside of the SDL (without batching, instead using the args and selectionSet config parameters), but for validation/gateway reasons I'd prefer to have all my subschemas contain all of their type merging instructions as SDL directives.看起来我实际上可以通过使用在 SDL 之外定义的子模式合并配置来规避这个问题(没有批处理,而是使用argsselectionSet配置参数),但是出于验证/网关的原因,我希望我的所有子模式都包含它们的所有类型合并指令都作为 SDL 指令。

Nb. NB。 This is a simplified representation of a real world problem.这是现实世界问题的简化表示。

Nb2. Nb2。 In the real world application one of my subschemas is a remote GraphQL application that I don't control, hence the need for some advanced tailoring in the stitching layer.在现实世界的应用程序中,我的子模式之一是我无法控制的远程 GraphQL 应用程序,因此需要在拼接层中进行一些高级定制。

Edit: Simply adding the following to the merge options on the subschema config seems to solve the problem.编辑:只需将以下内容添加到子模式配置的合并选项中似乎可以解决问题。 Someone know of a good reason why this doesn't seem to be reproducible with SDL directives?有人知道为什么这似乎不能用 SDL 指令重现吗? (Or a good way to do so?) (或者这样做的好方法?)

// AType
{
  argsFromKeys: (keys) => ({ keys }),
  fieldName: 'aEntities_fromBSchema',
  key: ({ oneToOneRelation, oneToManyRelation }) => ({ oneToManyRelation, oneToOneRelation }),
  selectionSet: '{ oneToOneRelation { id }, oneToManyRelation { id } }'
}

You have likely found a bug.您可能发现了一个错误。 Please open an issue on the GitHub repo so we can track it: :)请在 GitHub 存储库上打开一个问题,以便我们跟踪它::)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM