Commit a1a557a6 authored by Gerry Gunzenhauser's avatar Gerry Gunzenhauser

implement graphql skeleton

parent e5aa79cd
Pipeline #62005 failed with stages
in 9 minutes and 24 seconds
This diff is collapsed.
[workspace]
members = [
"mikrotik-gen-cli",
"mikrotik-gen-lib",
"mikrotik-gen-cli",
"mikrotik-gen-web",
]
[package]
name = "mikrotik-gen-web"
version = "0.1.0"
authors = ["Gerry <gerry@linuxgl.ch>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
mikrotik-gen-config = { path = "../mikrotik-gen-lib" }
juniper = "0.14.1"
juniper-from-schema = "0.5.0"
juniper_hyper = "0.5.1"
hyper = "^0.12"
pretty_env_logger = "^0.2"
futures="^0.1"
\ No newline at end of file
use juniper;
use juniper_from_schema::{graphql_schema_from_file, QueryTrail as QT};
graphql_schema_from_file!("src/schema.graphqls");
#[derive(Debug,Clone)]
pub struct Context;
impl juniper::Context for Context {}
pub struct Query;
impl QueryFields for Query {
fn field_list_locations<'a>(
&self,
executor: &juniper::Executor<'a, Context>,
trail: &QT<'a, Location, juniper_from_schema::Walked>,
) -> std::result::Result<&Option<Vec<Option<Location>>>, juniper::FieldError> {
Ok(&Option::None)
}
fn field_get_location<'a>(
&self,
executor: &juniper::Executor<'a, Context>,
trail: &QT<'a, Location, juniper_from_schema::Walked>,
id: Option<juniper::ID>,
) -> std::result::Result<&Option<Location>, juniper::FieldError> {
Ok(&Option::None)
}
fn field_get_device<'a>(
&self,
executor: &juniper::Executor<'a, Context>,
trail: &QT<'a, Device, juniper_from_schema::Walked>,
location_id: Option<juniper::ID>,
device_id: Option<juniper::ID>,
) -> std::result::Result<&Option<Device>, juniper::FieldError> {
Ok(&Option::None)
}
}
pub struct Device;
impl DeviceFields for Device {
fn field_id<'a>(
&self,
executor: &juniper::Executor<'a, Context>,
) -> std::result::Result<&juniper::ID, juniper::FieldError> {
unimplemented!();
}
fn field_name<'a>(
&self,
executor: &juniper::Executor<'a, Context>,
) -> std::result::Result<&String, juniper::FieldError> {
unimplemented!();
}
fn field_generated_configuration<'a>(
&self,
executor: &juniper::Executor<'a, Context>,
) -> std::result::Result<&Option<String>, juniper::FieldError> {
unimplemented!();
}
}
pub struct Location;
impl LocationFields for Location {
fn field_id<'a>(
&self,
executor: &juniper::Executor<'a, Context>,
) -> std::result::Result<&juniper::ID, juniper::FieldError> {
unimplemented!();
}
fn field_name<'a>(
&self,
executor: &juniper::Executor<'a, Context>,
) -> std::result::Result<&String, juniper::FieldError> {
unimplemented!();
}
fn field_devices<'a>(
&self,
executor: &juniper::Executor<'a, Context>,
trail: &QT<'a, Device, juniper_from_schema::Walked>,
) -> std::result::Result<&Option<Vec<Option<Device>>>, juniper::FieldError> {
unimplemented!();
}
}
extern crate futures;
extern crate hyper;
extern crate juniper;
#[macro_use]
extern crate juniper_from_schema;
extern crate juniper_hyper;
extern crate pretty_env_logger;
use std::sync::Arc;
use futures::future;
use hyper::{
Body,
Method,
Response, rt::{self, Future}, Server, service::service_fn, StatusCode,
};
use juniper::{
EmptyMutation, RootNode,
};
use crate::graphql::Context;
use crate::graphql::Query;
mod graphql;
fn main() {
pretty_env_logger::init();
let addr = ([127, 0, 0, 1], 3000).into();
let root_node = Arc::new(RootNode::new(Query, EmptyMutation::<Context>::new()));
let new_service = move || {
let root_node = root_node.clone();
let ctx = Arc::new(Context{});
service_fn(move |req| -> Box<dyn Future<Item = _, Error = _> + Send> {
let root_node = root_node.clone();
let ctx = ctx.clone();
match (req.method(), req.uri().path()) {
(&Method::GET, "/") => Box::new(juniper_hyper::graphiql("/graphql")),
(&Method::GET, "/graphql") => Box::new(juniper_hyper::graphql(root_node, ctx, req)),
(&Method::POST, "/graphql") => {
Box::new(juniper_hyper::graphql(root_node, ctx, req))
}
_ => {
let mut response = Response::new(Body::empty());
*response.status_mut() = StatusCode::NOT_FOUND;
Box::new(future::ok(response))
}
}
})
};
let server = Server::bind(&addr)
.serve(new_service)
.map_err(|e| eprintln!("server error: {}", e));
println!("Listening on http://{}", addr);
rt::run(server);
}
\ No newline at end of file
type Query{
listLocations: [Location]
getLocation(id: ID): Location
getDevice(locationId: ID, deviceId: ID): Device
}
type Location{
id: ID!
name: String!
devices: [Device]
}
type Device{
id: ID!
name: String!
generatedConfiguration: String
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment