Skip to content

Commit

Permalink
refactor(native-app): generate graphql schema from a separate bin
Browse files Browse the repository at this point in the history
  • Loading branch information
meskill committed Sep 25, 2022
1 parent df51077 commit b0df1ef
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 12 deletions.
14 changes: 14 additions & 0 deletions native-app/src/bin/generate-graphql-schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use mystic_light_browser_cinema::{graphql, sdk};
use std::error;
use std::fs;

fn main() -> Result<(), Box<dyn error::Error>> {
let sdk = sdk::create_sdk()?;
let schema = graphql::create_qraphql_schema(sdk);

if let Err(error) = fs::write("./schema.graphql", schema.sdl()) {
tracing::warn!("{}", error);
}

Ok(())
}
12 changes: 2 additions & 10 deletions native-app/src/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::fs;

use async_graphql::{extensions::Tracing, EmptySubscription, Schema};
use mystic_light_sdk::{
build_graphql_schema, MysticLightGraphqlMutation, MysticLightGraphqlQuery, MysticLightSDK,
Expand All @@ -11,13 +9,7 @@ pub type MysticLightSchema =
pub fn create_qraphql_schema(sdk: MysticLightSDK) -> MysticLightSchema {
let (query, mutation) = build_graphql_schema(sdk);

let schema = Schema::build(query, mutation, EmptySubscription)
Schema::build(query, mutation, EmptySubscription)
.extension(Tracing)
.finish();

if let Err(error) = fs::write("./schema.graphql", schema.sdl()) {
tracing::warn!("{}", error);
}

schema
.finish()
}
4 changes: 2 additions & 2 deletions native-app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod app;
mod config;
pub mod constants;
mod graphql;
pub mod graphql;
pub mod log;
mod props;
mod sdk;
pub mod sdk;
pub mod service;

use std::net::{IpAddr, Ipv4Addr, SocketAddr};
Expand Down
153 changes: 153 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@

"""
Represent RGB color
"""
type Color {
red: Int!
green: Int!
blue: Int!
}

"""
Represent RGB color
"""
input ColorInput {
red: Int!
green: Int!
blue: Int!
}

"""
Represents single hardware MysticLight Device
"""
type Device {
name: String!
"""
returns device's leds
"""
leds(filter: DeviceLedFilter! = {names: null}): [DeviceLed!]!
}

"""
used for filtering devices.
Currently, supports only filtering by name
"""
input DeviceFilter {
names: [String!]
}

"""
Represents single led of the device
"""
type DeviceLed {
name: String!
supportedStyles: [String!]!
maxBright: Int!
maxSpeed: Int!
state: DeviceLedState!
}

"""
used for filtering device's leds.
Currently, supports only filtering by name
"""
input DeviceLedFilter {
names: [String!]
}

"""
Mutation wrapper for a device led
"""
type DeviceLedMutation {
"""
updates state for the device led
"""
setState(state: DeviceLedStateInput!): Boolean!
}

"""
Represents state of the single led
"""
type DeviceLedState {
"""
current style of the led
"""
style: String!
"""
current color of the led (some of the styles do not support this, so there will be fake data in this case)
"""
color: Color!
"""
current brightness of the led (some of the styles do not support this, so there will be fake data in this case)
"""
bright: Int!
"""
current speed of the led (some of the styles do not support this, so there will be fake data in this case)
"""
speed: Int!
}

"""
Represents state of the single led, but with optional fields
"""
input DeviceLedStateInput {
"""
current style of the led
"""
style: String
"""
current color of the led (some of the styles do not support this, so there will be fake data in this case)
"""
color: ColorInput
"""
current brightness of the led (some of the styles do not support this, so there will be fake data in this case)
"""
bright: Int
"""
current speed of the led (some of the styles do not support this, so there will be fake data in this case)
"""
speed: Int
}

"""
Mutation wrapper for a device
"""
type DeviceMutation {
"""
returns device's leds wrapped in mutation wrapper
"""
leds(filter: DeviceLedFilter! = {names: null}): [DeviceLedMutation!]!
}




"""
Graphql mutation for MysticLightSDK
"""
type MysticLightGraphqlMutation {
"""
returns Mystic Light devices wrapped in mutation wrapper
"""
devices(filter: DeviceFilter! = {names: null}): [DeviceMutation!]!
"""
Full reload of Mystic Light SDK to get most-fresh hardware data
"""
reload: Boolean!
}

"""
Graphql query for MysticLightSDK
"""
type MysticLightGraphqlQuery {
"""
returns Mystic Light devices
"""
devices(filter: DeviceFilter! = {names: null}): [Device!]!
}


schema {
query: MysticLightGraphqlQuery
mutation: MysticLightGraphqlMutation
}

0 comments on commit b0df1ef

Please sign in to comment.