我有一组看起来像这样的实体:
const aEntities = [
{
id: 1,
name: 'Test',
oneToManyRelation: [
{
id: 2
},
{
id: 3
}
],
oneToOneRelation: {
id: 1
}
}
];
实体由 type 表示AType
。我想在一个单独的子模式中扩展这种类型,并证明可以添加分别从 和 的内容派生其值的oneToOneRelation
字段oneToManyRelation
。
以下架构实现了基于 的派生字段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,
}
]
})
但是一旦我添加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
}
`
})
我收到以下错误:
oneToManyRelation.map is not a function
当我在aEntities_fromBSchema
解析器中记录 keys 参数时,似乎oneToManyRelation
根本没有被解析为数组,而是一个(空)对象:
[
{
oneToOneRelation: [Object: null prototype] { id: '1' },
oneToManyRelation: [Object: null prototype] { id: undefined },
__typename: 'AType'
}
]
graphql-tools
从v开始,是否禁止在键选择集中引用列表类型7.0.2
?看起来我实际上可以通过使用在 SDL 之外定义的子模式合并配置来规避这个问题(没有批处理,而是使用args
和selectionSet
配置参数),但是出于验证/网关的原因,我希望我的所有子模式都包含所有它们的类型合并指令作为 SDL 指令。
NB。这是现实世界问题的简化表示。
Nb2。在现实世界的应用程序中,我的子模式之一是我无法控制的远程 GraphQL 应用程序,因此需要在拼接层中进行一些高级定制。
编辑:只需将以下内容添加到子模式配置的合并选项中似乎可以解决问题。有人知道为什么这似乎不能用 SDL 指令重现吗?(或者这样做的好方法?)
// AType
{
argsFromKeys: (keys) => ({ keys }),
fieldName: 'aEntities_fromBSchema',
key: ({ oneToOneRelation, oneToManyRelation }) => ({ oneToManyRelation, oneToOneRelation }),
selectionSet: '{ oneToOneRelation { id }, oneToManyRelation { id } }'
}