Skip to content
Snippets Groups Projects
Select Git revision
  • ece028328136af4686c4b98d3fd65eb5e3c126b5
  • dev default protected
  • vainamoinen197-transactiondocument-replace-vec-fields-by-smallvec-2
  • dvermd/200-keypairs-dewif
  • elois/wot
  • jawaka/155-dbex-add-dump-fork-tree-command
  • elois/195-bcdbwriteop
  • elois/deps-crypto
  • elois/gva-monetary-mass
  • elois/191-sled
  • elois/195
  • ji_emme/gva-humantimefield
  • 184-gva-rename-commontime-field-to-blockchaintime
  • ji_emme/182-gva-implement-block-meta-data
  • ji_emme/rml14
  • hugo/151-ws2pv2-sync
  • ji_emme/181-gva-implement-identity-request
  • ji_emme/89-implement-client-api-gva-graphql-verification-api
  • logo
  • test-juniper-from-schema
  • elois/exemple-gva-global-context
  • v0.2.0-a4 protected
  • v0.2.0-a2 protected
  • v0.2.0-a protected
  • v0.1.1-a1 protected
  • documents/v0.10.0-b1 protected
  • crypto/v0.4.0-b1 protected
  • crypto/v0.3.0-b3 protected
  • crypto/v0.3.0-b2 protected
  • crypto/v0.3.0-b1 protected
  • wot/v0.8.0-a0.9 protected
  • wot/v0.8.0-a0.8 protected
  • 0.1.0-a0.1 protected
  • v0.0.1-a0.12 protected
  • v0.0.1-a0.11 protected
  • v0.0.1-a0.10 protected
  • v0.0.1-a0.9 protected
  • v0.0.1-a0.8 protected
  • v0.0.1-a0.7 protected
  • v0.0.1-a0.6 protected
  • v0.0.1-a0.5 protected
41 results

schema.rs

Blame
  • schema.rs 4.24 KiB
    //  Copyright (C) 2017-2019  The AXIOM TEAM Association.
    //
    // This program is free software: you can redistribute it and/or modify
    // it under the terms of the GNU Affero General Public License as
    // published by the Free Software Foundation, either version 3 of the
    // License, or (at your option) any later version.
    //
    // This program is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU Affero General Public License for more details.
    //
    // You should have received a copy of the GNU Affero General Public License
    // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    
    // ! model and resolvers implementation
    
    mod block;
    mod paging;
    
    use self::block::Block;
    use crate::context::Context;
    use dubp_common_doc::BlockNumber;
    use durs_bc_db_reader::{BcDbRo, DbError, DbReadable};
    use juniper::Executor;
    use juniper::FieldResult;
    use juniper_from_schema::graphql_schema_from_file;
    
    // generate schema from schema file
    graphql_schema_from_file!("resources/schema.gql");
    
    pub struct Query;
    
    pub struct Summary {
        software: &'static str,
        version: &'static str,
    }
    
    pub struct Node {
        summary: Summary,
    }
    
    fn db_err_to_juniper_err(e: DbError) -> juniper::FieldError {
        juniper::FieldError::from(format!("Db error: {:?}", e))
    }
    
    impl QueryFields for Query {
        fn field_node(
            &self,
            executor: &Executor<'_, Context>,
            _trail: &QueryTrail<'_, Node, Walked>,
        ) -> FieldResult<Node> {
            Ok(Node {
                summary: Summary {
                    software: executor.context().get_software_name(),
                    version: executor.context().get_software_version(),
                },
            })
        }
        fn field_current(
            &self,
            executor: &Executor<'_, Context>,
            _trail: &QueryTrail<'_, Block, Walked>,
        ) -> FieldResult<Option<Block>> {
            let db: &BcDbRo = &executor.context().get_db();
    
            db.read(|r| {
                if let Some(current_blockstamp) =
                    durs_bc_db_reader::current_meta_datas::get_current_blockstamp_(db, r)?
                {
                    block::get_block(db, r, current_blockstamp.id)
                } else {
                    Ok(None)
                }
            })
            .map_err(db_err_to_juniper_err)
        }
        fn field_block(
            &self,
            executor: &Executor<'_, Context>,
            _trail: &QueryTrail<'_, Block, Walked>,
            number: i32,
        ) -> FieldResult<Option<Block>> {
            let db: &BcDbRo = &executor.context().get_db();
    
            let block_number = if number >= 0 {
                BlockNumber(number as u32)
            } else {
                return Err(juniper::FieldError::from("Block number must be positive."));
            };
    
            db.read(|r| block::get_block(db, r, block_number))
                .map_err(db_err_to_juniper_err)
        }
        fn field_blocks(
            &self,
            executor: &Executor<'_, Context>,
            _trail: &QueryTrail<'_, Block, Walked>,
            paging_opt: Option<Paging>,
        ) -> FieldResult<Vec<Block>> {
            let db: &BcDbRo = &executor.context().get_db();
            db.read(|r| {
                paging::FilledPaging::new(db, r, paging_opt)?
                    .get_range()
                    .filter_map(|n| match block::get_block(db, r, BlockNumber(n)) {
                        Ok(Some(db_block)) => Some(Ok(db_block)),
                        Ok(None) => None,
                        Err(e) => Some(Err(e)),
                    })
                    .collect::<Result<Vec<Block>, DbError>>()
            })
            .map_err(db_err_to_juniper_err)
        }
    }
    
    impl NodeFields for Node {
        fn field_summary(
            &self,
            _executor: &Executor<'_, Context>,
            _trail: &QueryTrail<'_, Summary, Walked>,
        ) -> &Summary {
            &self.summary
        }
    }
    
    impl SummaryFields for Summary {
        fn field_software(&self, _executor: &Executor<'_, Context>) -> String {
            self.software.to_owned()
        }
        fn field_version(&self, _executor: &Executor<'_, Context>) -> String {
            self.version.to_owned()
        }
    }
    
    pub struct Mutation;
    
    impl MutationFields for Mutation {
        fn field_noop(&self, _executor: &Executor<'_, Context>) -> FieldResult<&bool> {
            Ok(&true)
        }
    }
    
    pub fn create_schema() -> Schema {
        Schema::new(Query {}, Mutation {})
    }