服务器驱动UI模式设计
使用枚举、合约和接口实现SDUI
这些模式提供了服务器驱动UI(SDUI)的示例以及如何构建 图's方案结构以编码和表示UI元素。然后客户端可以使用平台特定的渲染引擎来解释该方案并生成UI组件。
💡 提示
有关SDUI一般信息的更多信息,请参阅服务器驱动UI基础知识。
基于设备的枚举SDUI
不同的设备(例如,移动,网页,Roku,Apple TV)可能需要不同的UI布局或组件。您可以使用枚举来声明设备类型列表,以获取哪个UI类型。
enum UIDeviceType {MOBILEWEBROKUAPPLETV}interface UIApp {}type WebApp implements UIApp {}type IOSApp implements UIApp {}type AndroidApp implements UIApp {}type AppleTVApp implements UIApp {}type RokuApp implements UIApp {}type Query {app(type: UIDeviceType! = WEB): UIApp!}
在以下示例中,UIDeviceType
枚举允许您指定要获取 UIApp
的设备类型。
基于设备的 SDUI 与合约
您可以使用类似基于设备的 SDUI 与 合约 从您的模式中移除与目标平台无关的 UI 组件。
type UIApp {spotlight: UISpotlightComponent @tag(name: "DESKTOP")dashboard: UIDashboardComponentmobileMenu: UIMenuComponent @tag(name: "MOBILE")menu: UIMenuComponent @tag(name: "WEB")}type Query {app: UIApp!}
💡 提示
更多关于 合约使用模式 的信息。
SDUI 网页仪表板
以下示例展示了一个静态结构的模式,客户端可以使用它来创建网页仪表板 UI。
type AppLogo {url: Stringalt: String}type AppLink {icon: Stringlabel: Stringpath: String}type AppUserMenu {user: User}type AppNavbar {logo: AppLogoitems: [AppLink]user: AppUserMenu}type AppMobileMenu {items: [AppLink]user: AppUserMenu}type AppHomePage {recommended: [AppLink]}type WebApp {navbar: AppNavbarmenu: AppMobileMenuhome: AppHomePagesettings: AppSettingsPageprofile: AppProfilePage}type Query {app: WebApp!}
使用接口的 SDUI 网页仪表板
以下示例模式也是针对网页仪表板 UI 的,但它使用 接口 来构造动态响应 UI。通过使用接口,子图 “体验” 可以动态返回不同的 UI 组件。
interface UIComponent {id: ID}type UILogo implements UIComponent {id: IDurl: Stringalt: String}interface UINavbarItem implements UIComponent {id: IDlabel: Stringpath: Stringicon: String}interface UINavbar implements UIComponent {id: IDlogo: UILogoitems: [UINavbarItem]}interface UIMenuItem implements UIComponent {id: IDlabel: Stringpath: Stringicon: String}interface UIMenu implements UIComponent {id: IDlogo: UILogoitems: [UIMenuItem]}interface UISidebar implements UIComponent {id: IDtitle: Stringcontent: [UIComponent]}interface UILayout implements UIComponent {id: IDcontent: [UIComponent]}interface UIScreen implements UIComponent {id: IDcurrent: UIPagenavbar: UINavbarmenu: UIMenumain: UILayoutsidebar: UISidebar}enum UIPage {HOMEDASHBOARDSETTINGS}type Query {app(page: UIPage!): UIScreen!}