为Drupal9空字段创建自定义GraphQL模式,这些字段是段落引用



拥有一个带有Gatsby和GraphQL的解耦Drupal 9。在Drupal一侧,有一个名为school的节点,还有一个字段(field_components(,它是一个实体引用字段,可以对段落进行无限数量的引用。网站上有50多种段落类型,但该特定字段只接受四(4(种类型。我正在尝试在GraphQL中定义它们的类型。

exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type node__school implements Node {
field_location: String
field_type_r_a: Boolean
relationships: node__schoolRelationships
}

type node__schoolRelationships {
field_components: // this is where i need to define the 4 types 
}
`
createTypes(typeDefs);
};

正如你从上面的例子中看到的,我已经写出了90%的必要模式,但我不知道如何定义for段落类型。我猜每个单独的段落都会被称为paragraph__machine_name(例如段落__carousel(,但由于有四(4(个段落,我不知道如何将它们链接起来(定义所有段落(

有什么想法吗?

来自GraphQL文档:

联合和接口是抽象的GraphQL类型,使模式字段能够返回多个对象类型中的一个。

实际操作(使用段落实体(:

const typeDefs = `
type node__school implements Node {
field_location: String
field_type_r_a: Boolean
relationships: node__schoolRelationships
}

union fieldContentParagraphUnion =
paragraph__foo
| paragraph__bar
| paragraph__bat
type paragraph__foo implements Node {
field_1: String
}
type paragraph__bar implements Node {
field_2: String
}
type paragraph__bat implements Node {
field_3: String
}
type node__schoolRelationships {
field_components: [fieldContentParagraphUnion] @link(from: "field_components___NODE") 
}
`

这个官方的Gatsby插件帮助我解决了与模式相关的问题:Gatsby插件模式快照

将最小架构保存到文件,将@dontInfer指令添加到所有顶级类型,并在引导过程中根据保存的类型定义重新创建架构。如果您打算锁定项目的GraphQL模式,请使用此插件。

最新更新