Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a graph-node-napi crate #5072

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 98 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"core",
"chain/*",
"graphql",
"napi",
"node",
"runtime/*",
"server/*",
Expand Down Expand Up @@ -38,4 +39,3 @@ incremental = false
lto = true
opt-level = 's'
strip = "debuginfo"

15 changes: 14 additions & 1 deletion graph/src/schema/input_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use crate::schema::api::api_schema;
use crate::util::intern::{Atom, AtomPool};

use super::fulltext::FulltextDefinition;
use super::{ApiSchema, AsEntityTypeName, EntityType, Schema, SCHEMA_TYPE_NAME};
use super::{
ApiSchema, AsEntityTypeName, EntityType, Schema, SchemaValidationError, SCHEMA_TYPE_NAME,
};

/// The name of the PoI entity type
pub(crate) const POI_OBJECT: &str = "Poi$";
Expand Down Expand Up @@ -465,6 +467,17 @@ impl InputSchema {
})
}

pub fn validate(raw: &str, id: DeploymentHash) -> Vec<SchemaValidationError> {
let schema = match Schema::parse(raw, id.clone()) {
Ok(schema) => schema,
Err(err) => return vec![SchemaValidationError::InvalidSchema(err.to_string())],
};
match validations::validate(&schema) {
Ok(_) => vec![],
Err(errors) => errors,
}
}

/// Convenience for tests to construct an `InputSchema`
///
/// # Panics
Expand Down
2 changes: 2 additions & 0 deletions graph/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ impl fmt::Display for Strings {

#[derive(Debug, Error, PartialEq, Eq)]
pub enum SchemaValidationError {
#[error("Invalid schema: {0}")]
InvalidSchema(String),
#[error("Interface `{0}` not defined")]
InterfaceUndefined(String),

Expand Down
11 changes: 11 additions & 0 deletions napi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "graph-node-napi"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
graph = { path = "../graph" }
napi = "2.14.1"
napi-derive = "2.14.4"
22 changes: 22 additions & 0 deletions napi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use napi::bindgen_prelude as n;
use napi_derive::napi;

use graph::{data::subgraph::DeploymentHash, schema::InputSchema};

#[napi]
pub fn validate_schema(schema: String, id: String) -> n::Result<Vec<String>> {
let id = match DeploymentHash::new(id) {
Ok(id) => id,
Err(e) => {
return Err(n::Error::new(
n::Status::InvalidArg,
format!("Invalid deployment hash {e}"),
))
}
};
let errs = InputSchema::validate(&schema, id)
.into_iter()
.map(|e| e.to_string())
.collect();
Ok(errs)
}
Loading