Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
YCR: bootstrap SDUI GraphQL schema
Browse files Browse the repository at this point in the history
The server now knows how to return `SDUIComponent` union including filter of supported components via `supported` arg.

adeira-source-id: 27a09043f0e6688560cf239d4e61c7b629ab6d83
  • Loading branch information
mrtnzlml authored and adeira-github-bot committed Dec 8, 2020
1 parent a14d2fa commit 487ba26
Show file tree
Hide file tree
Showing 22 changed files with 202 additions and 69 deletions.
4 changes: 1 addition & 3 deletions react-native/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
type Query {
mobileEntrypointSections(
id: String! # TODO: limit to only supported pages? (com.yaComiste.Explore)
): [SDUISection!]!
mobileEntrypointSections(id: String!): [SDUISection!]!
}

type SDUISection {
Expand Down
13 changes: 12 additions & 1 deletion rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ Example query:
{
mobileEntrypointSections(key: "com.yaComiste.Explore") {
id
title
__typename
component(supported: ["SDUIScrollViewHorizontalComponent"]) {
__typename
... on SDUICardComponent {
id
}
... on SDUIScrollViewHorizontalComponent {
id
}
}
}
}
```
Expand All @@ -42,6 +51,8 @@ Example query:

- setup CI for Rust
- DB strings and source-code translations
- dataloaders
- DB schema validations

# ArangoDB

Expand Down
2 changes: 1 addition & 1 deletion rust/arangodb/dump/_system/dump.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"database":"_system","lastTickAtDumpStart":"90475","properties":{"id":"1","name":"_system","isSystem":true}}
{"database":"_system","lastTickAtDumpStart":"132102","properties":{"id":"1","name":"_system","isSystem":true}}
2 changes: 1 addition & 1 deletion rust/arangodb/dump/ya-comiste/dump.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"database":"ya-comiste","lastTickAtDumpStart":"90477","properties":{"id":"84144","name":"ya-comiste","isSystem":false}}
{"database":"ya-comiste","lastTickAtDumpStart":"132104","properties":{"id":"84144","name":"ya-comiste","isSystem":false}}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{"type":2300,"data":{"_key":"89538","_id":"entrypoint_sections/89538","_from":"entrypoints/com.yaComiste.Explore","_to":"sections/10771","_rev":"_bff4636---"}}
{"type":2300,"data":{"_key":"89458","_id":"entrypoint_sections/89458","_from":"entrypoints/com.yaComiste.Explore","_to":"sections/9810","_rev":"_bfgGf26---","user":"users/87047"}}
{"type":2300,"data":{"_key":"89458","_id":"entrypoint_sections/89458","_from":"entrypoints/com.yaComiste.Explore","_to":"sections/9810","_rev":"_bgJE1wa---"}}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{"type":2300,"data":{"_key":"9810","_id":"sections/9810","_rev":"_bff1zcW---","section_order":1,"title":"Places nearby","type":"SDUIScrollViewHorizontalComponent"}}
{"type":2300,"data":{"_key":"10771","_id":"sections/10771","_rev":"_bff2ICi---","section_order":2,"title":"Bakeries nearby","type":"SDUIScrollViewHorizontalComponent"}}
{"type":2300,"data":{"_key":"9810","_id":"sections/9810","_rev":"_bgLK4YO---","order":1,"component":{"typename":"SDUIScrollViewHorizontalComponent"}}}
{"type":2300,"data":{"_key":"10771","_id":"sections/10771","_rev":"_bgLL-8W---","order":2,"component":{"typename":"SDUIScrollViewHorizontalComponent"}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"indexes":[],"parameters":{"allowUserKeys":true,"cacheEnabled":false,"cid":"85191","deleted":false,"globallyUniqueId":"hC8B4EB76885F/9751","id":"85191","isDisjoint":false,"isSmart":false,"isSmartChild":false,"isSystem":false,"keyOptions":{"allowUserKeys":true,"type":"traditional","lastValue":11596},"minReplicationFactor":1,"name":"sections","numberOfShards":1,"planId":"9751","replicationFactor":1,"schema":{"message":"","level":"strict","rule":{"type":"object","oneOf":[{"properties":{"section_order":{"type":"number"},"type":{"const":"SDUIScrollViewHorizontalComponent"},"title":{"type":"string"}},"additionalProperties":false,"required":["section_order","type","title"]},{"properties":{"section_order":{"type":"number"},"type":{"const":"SDUICardComponent"}},"additionalProperties":false,"required":["section_order","type"]}]}},"shardKeys":["_key"],"shards":{},"status":3,"tempObjectId":"0","type":2,"version":9,"waitForSync":false,"writeConcern":1}}
{"indexes":[],"parameters":{"allowUserKeys":true,"cacheEnabled":false,"cid":"85191","deleted":false,"globallyUniqueId":"hC8B4EB76885F/9751","id":"85191","isDisjoint":false,"isSmart":false,"isSmartChild":false,"isSystem":false,"keyOptions":{"allowUserKeys":true,"type":"traditional","lastValue":11596},"minReplicationFactor":1,"name":"sections","numberOfShards":1,"planId":"9751","replicationFactor":1,"schema":null,"shardKeys":["_key"],"shards":{},"status":3,"tempObjectId":"0","type":2,"version":9,"waitForSync":false,"writeConcern":1}}
11 changes: 1 addition & 10 deletions rust/crates/sdui/src/entrypoint.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
use graphql::graphql_context::Context;

/// TODO: maybe `MobileEntrypoint` would be a better name so we are future proof?
#[derive(Clone, Debug, serde::Deserialize)]
pub struct Entrypoint {
pub _id: juniper::ID,
pub _id: String,
pub _key: String,
}

#[juniper::graphql_object(context = Context)]
impl Entrypoint {
fn id(&self) -> &str {
&self._id
}
}
1 change: 1 addition & 0 deletions rust/crates/sdui/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// use sdui::errors::ModelError;
/// assert_eq!(format!("{}", ModelError::LogicError("ups".to_string())), "Logic error: ups")
/// ```
#[derive(Debug)]
pub enum ModelError {
// TODO: naming (SDUIError?)
DatabaseError(arangors::ClientError),
Expand Down
6 changes: 6 additions & 0 deletions rust/crates/sdui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ pub mod entrypoint;
pub mod errors;
pub mod model;
pub mod sdui_section;

mod sdui_card_component;
mod sdui_component;
mod sdui_description_component;
mod sdui_jumbotron_component;
mod sdui_scrollview_horizontal_component;
1 change: 1 addition & 0 deletions rust/crates/sdui/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod entrypoints;
pub mod sdui_sections;
71 changes: 71 additions & 0 deletions rust/crates/sdui/src/model/sdui_sections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use crate::errors::ModelError;
use crate::model::entrypoints::get_entrypoint;
use crate::sdui_component::SDUIComponent;
use crate::sdui_section::SDUISection;
use arangodb::connection;

/// Note: this function doesn't return `section.component` on purpose.
pub async fn get_all_sections_for_entrypoint_key(
entrypoint_key: String,
) -> Result<Vec<SDUISection>, ModelError> {
let conn = connection().await;
let db = conn.db("ya-comiste").await.unwrap();

let entrypoint = get_entrypoint(&entrypoint_key).await?;
let aql = arangors::AqlQuery::builder()
.query(
"
FOR section IN 1..1 OUTBOUND @entrypoint_id entrypoint_sections
LET component = section.component
SORT section.order ASC
RETURN {
id: section._id,
}
",
)
.bind_var("entrypoint_id", entrypoint._id.to_string())
.batch_size(1)
.build();

match db.aql_query::<SDUISection>(aql).await {
Ok(r) => Ok(r),
Err(e) => Err(ModelError::DatabaseError(e)),
}
}

pub async fn get_section_component(
section_id: String,
supported: Vec<String>,
) -> Result<SDUIComponent, ModelError> {
let conn = connection().await;
let db = conn.db("ya-comiste").await.unwrap();

// TODO: change the component ID to be unique (how to 🤔)
let aql = arangors::AqlQuery::builder()
.query(
"
LET section = DOCUMENT(@section_id)
LET component = section.component
FILTER component.typename IN @supported_typenames
LIMIT 1
RETURN {
typename: component.typename,
id: CONCAT(section._id, '~', component.typename),
}
",
)
.bind_var("section_id", section_id)
.bind_var("supported_typenames", supported)
.batch_size(1)
.build();

match db.aql_query::<SDUIComponent>(aql).await {
Ok(r) => match r.first() {
Some(first) => Ok(first.to_owned()),
None => Err(ModelError::LogicError(
"no supported component available".to_string(),
)),
},
Err(e) => Err(ModelError::DatabaseError(e)),
}
}
13 changes: 13 additions & 0 deletions rust/crates/sdui/src/sdui_card_component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use graphql::graphql_context::Context;

#[derive(Clone, Debug, serde::Deserialize)]
pub struct SDUICardComponent {
pub id: String,
}

#[juniper::graphql_object(context = Context)]
impl SDUICardComponent {
fn id(&self) -> juniper::ID {
juniper::ID::new(&self.id)
}
}
16 changes: 16 additions & 0 deletions rust/crates/sdui/src/sdui_component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::sdui_card_component::SDUICardComponent;
use crate::sdui_description_component::SDUIDescriptionComponent;
use crate::sdui_jumbotron_component::SDUIJumbotronComponent;
use crate::sdui_scrollview_horizontal_component::SDUIScrollViewHorizontalComponent;
use graphql::graphql_context::Context;
use juniper::GraphQLUnion;

#[derive(Clone, Debug, serde::Deserialize, GraphQLUnion)]
#[graphql(Context = Context)]
#[serde(tag = "typename")]
pub enum SDUIComponent {
SDUICardComponent(SDUICardComponent),
SDUIDescriptionComponent(SDUIDescriptionComponent),
SDUIJumbotronComponent(SDUIJumbotronComponent),
SDUIScrollViewHorizontalComponent(SDUIScrollViewHorizontalComponent),
}
13 changes: 13 additions & 0 deletions rust/crates/sdui/src/sdui_description_component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use graphql::graphql_context::Context;

#[derive(Clone, Debug, serde::Deserialize)]
pub struct SDUIDescriptionComponent {
pub id: String,
}

#[juniper::graphql_object(context = Context)]
impl SDUIDescriptionComponent {
fn id(&self) -> juniper::ID {
juniper::ID::new(&self.id)
}
}
13 changes: 13 additions & 0 deletions rust/crates/sdui/src/sdui_jumbotron_component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use graphql::graphql_context::Context;

#[derive(Clone, Debug, serde::Deserialize)]
pub struct SDUIJumbotronComponent {
pub id: String,
}

#[juniper::graphql_object(context = Context)]
impl SDUIJumbotronComponent {
fn id(&self) -> juniper::ID {
juniper::ID::new(&self.id)
}
}
13 changes: 13 additions & 0 deletions rust/crates/sdui/src/sdui_scrollview_horizontal_component.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use graphql::graphql_context::Context;

#[derive(Clone, Debug, serde::Deserialize)]
pub struct SDUIScrollViewHorizontalComponent {
pub id: String,
}

#[juniper::graphql_object(context = Context)]
impl SDUIScrollViewHorizontalComponent {
fn id(&self) -> juniper::ID {
juniper::ID::new(&self.id)
}
}
19 changes: 12 additions & 7 deletions rust/crates/sdui/src/sdui_section.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::model::sdui_sections::get_section_component;
use crate::sdui_component::SDUIComponent;
use graphql::graphql_context::Context;

#[derive(Clone, Debug, serde::Deserialize)]
pub struct SDUISection {
pub id: juniper::ID,
pub title: String,
pub id: String,
pub component: Option<SDUIComponent>,
}

// impl SDUISection {
Expand All @@ -14,12 +16,15 @@ pub struct SDUISection {

#[juniper::graphql_object(context = Context)]
impl SDUISection {
#[graphql(description = "GraphQL description 🤯")]
fn id(&self) -> &str {
&self.id
fn id(&self) -> juniper::ID {
juniper::ID::new(&self.id)
}

fn title(&self) -> &str {
&self.title
async fn component(&self, supported: Vec<String>) -> Option<SDUIComponent> {
// We might get a component which is not supported by the client yet (defined by `supported`).
match get_section_component(self.id.to_string(), supported).await {
Ok(component) => Some(component),
Err(_) => None,
}
}
}
6 changes: 3 additions & 3 deletions rust/crates/server/src/graphql_schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::sdui_blocks::get_all_sections;
use graphql::graphql_context::Context;
use juniper::{EmptyMutation, EmptySubscription, FieldError, FieldResult, RootNode};
use sdui::errors::ModelError;
use sdui::model::sdui_sections::get_all_sections_for_entrypoint_key;
use sdui::sdui_section::SDUISection;

#[derive(Clone, Copy, Debug)]
Expand All @@ -10,11 +10,11 @@ pub struct Query;
#[juniper::graphql_object(context = Context)]
impl Query {
async fn mobile_entrypoint_sections(key: String) -> FieldResult<Vec<SDUISection>> {
match get_all_sections(key).await {
match get_all_sections_for_entrypoint_key(key).await {
Ok(s) => Ok(s),
// Err(e) => Err(FieldError::from(e)),
Err(e) => match e {
ModelError::DatabaseError(_) => Err(FieldError::from("opaque database error")), // TODO log these errors
ModelError::DatabaseError(e) => Err(FieldError::from(e)), // TODO: hide and log these errors
ModelError::LogicError(e) => Err(FieldError::from(e)),
},
}
Expand Down
1 change: 0 additions & 1 deletion rust/crates/server/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// This declaration will look for a file named `XXX.rs` or `XXX/mod.rs` and will
// insert its contents inside a module named `XXX` under this scope
mod graphql_schema;
mod sdui_blocks;

use crate::graphql_schema::create_graphql_schema;
use graphql::graphql_context::Context;
Expand Down
35 changes: 0 additions & 35 deletions rust/crates/server/src/sdui_blocks.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@
source: crates/server/src/graphql_schema.rs
expression: "super::create_graphql_schema().as_schema_language()"
---
type SDUIScrollViewHorizontalComponent {
id: ID!
}

type SDUIDescriptionComponent {
id: ID!
}

type SDUIJumbotronComponent {
id: ID!
}

type SDUICardComponent {
id: ID!
}

type Query {
mobileEntrypointSections(key: String!): [SDUISection!]!
}

type SDUISection {
"GraphQL description \u129327"
id: String!
title: String!
id: ID!
component(supported: [String!]!): SDUIComponent
}

union SDUIComponent = SDUICardComponent | SDUIDescriptionComponent | SDUIJumbotronComponent | SDUIScrollViewHorizontalComponent

schema {
query: Query
}
Expand Down

0 comments on commit 487ba26

Please sign in to comment.