laravel - 如何在 Laravel 灯塔中查询第三方包特征
问题描述
我阅读了 Laravel Lighthouse 文档并在网上搜索,但没有找到如何在 Laravel 的第三方包中查询特征。我正在使用qirolab/laravel-reactions包,它具有reactSummary ()特性。我在问如何在灯塔查询中添加这种关系?
type Post {
id: ID!
title: String!
excerpt: String!
image_url: String!
slug: String!
source: Source! @belongsTo
reactionSummary: ???????
created_at: DateTime!
updated_at: DateTime!
}
除了解决我的问题之外,我的问题还有一个目的,了解 lighthouse 如何使用包或如何将第三方包与 lighthouse 集成?
解决方案
您需要在 graphql 中定义反应模型的模式,在后模式中您需要定义反应类型的数组。
根据模型(https://github.com/qirolab/laravel-reactions/blob/master/src/Models/Reaction.php),反应graphql模式看起来像这样:
type Reaction {
reactBy: User! @belongsTo
type: String
reactable: Reactable! @morphTo
}
您的帖子架构将更改为
type Post {
id: ID!
title: String!
excerpt: String!
image_url: String!
slug: String!
source: Source! @belongsTo
reactionSummary: [Reaction]
created_at: DateTime!
updated_at: DateTime!
}
正如我从迁移文件https://github.com/qirolab/laravel-reactions/blob/master/migrations/2018_07_10_000000_create_reactions_table.php中看到的那样。该反应具有多晶型关系。
这意味着返回类型可能会根据reactable_type
字段中设置的模型类型而有所不同。因此,您需要定义自己的 Union 类型。
联合是一种简单枚举其他对象类型的抽象类型。它们类似于接口,因为它们可以返回不同的类型,但它们不能定义字段。
来源:https ://lighthouse-php.com/5/the-basics/types.html#union
另请参阅多态关系和联合部分:https ://lighthouse-php.com/5/eloquent/polymorphic-relationships.html#one-to-one
我希望这能为您提供有关如何进行的方向。
推荐阅读
- spring - Spring Boot 中的过滤器初始化
- php - Laravel DB 失去连接(大概)
- python - 无法使用 webdriver python selenium 为组合框设置下拉值
- abap - 全局对象类型中缺少CLASS语句的添加“PUBLIC”
- java - Java 8 流分组依据。如果只有一个分组值对象,如何设置字段?
- java - Jpa Repository save() 不更新现有数据
- php - Laravel 文件上传到 GCP 存储 - .json 文件另存为 .txt
- eclipse - 在“启动 GlassFish 5 [domain1]”期间发生内部错误。找不到基本名称 sun.util.logging.resources.logging 的捆绑包,语言环境 en_DE
- command-line-interface - 使用 Hyperledger Fabric CLI 进行生产?
- javascript - ReactJS 路由器没有导航到指定的组件