Skip to content

Commit

Permalink
graphql: Add subgraphError argument only when feature is present
Browse files Browse the repository at this point in the history
We saw an issue in a graphql client that is avoided by doing this.
and it is generally more correct to only include it if the subgraph opts in.
This requires storing the `features` field in the metadata, which we should be doing anyways.
  • Loading branch information
leoyvens committed Dec 12, 2020
1 parent ce52896 commit 47a4a10
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 85 deletions.
19 changes: 19 additions & 0 deletions graph/src/data/subgraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,3 +1305,22 @@ impl DeploymentState {
pub enum SubgraphFeature {
nonFatalErrors,
}

impl std::fmt::Display for SubgraphFeature {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SubgraphFeature::nonFatalErrors => write!(f, "nonFatalErrors"),
}
}
}

impl FromStr for SubgraphFeature {
type Err = anyhow::Error;

fn from_str(s: &str) -> anyhow::Result<Self> {
match s {
"nonFatalErrors" => Ok(SubgraphFeature::nonFatalErrors),
_ => Err(anyhow::anyhow!("invalid subgraph feature {}", s)),
}
}
}
26 changes: 19 additions & 7 deletions graph/src/data/subgraph/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ pub struct SubgraphManifestEntity {
spec_version: String,
description: Option<String>,
repository: Option<String>,
features: Vec<String>,
schema: String,
data_sources: Vec<EthereumContractDataSourceEntity>,
templates: Vec<EthereumContractDataSourceTemplateEntity>,
Expand All @@ -454,17 +455,26 @@ impl SubgraphManifestEntity {
}

fn write_operations(self, id: &str) -> Vec<MetadataOperation> {
let SubgraphManifestEntity {
spec_version,
description,
repository,
features,
schema,
data_sources,
templates,
} = self;

let mut ops = vec![];

let mut data_source_ids: Vec<Value> = vec![];
for (i, data_source) in self.data_sources.into_iter().enumerate() {
for (i, data_source) in data_sources.into_iter().enumerate() {
let data_source_id = format!("{}-data-source-{}", id, i);
ops.extend(data_source.write_operations(&data_source_id));
data_source_ids.push(data_source_id.into());
}

let template_ids: Vec<Value> = self
.templates
let template_ids: Vec<Value> = templates
.into_iter()
.enumerate()
.map(|(i, template)| {
Expand All @@ -476,10 +486,11 @@ impl SubgraphManifestEntity {

let entity = entity! {
id: id,
specVersion: self.spec_version,
description: self.description,
repository: self.repository,
schema: self.schema,
specVersion: spec_version,
description: description,
repository: repository,
features: features,
schema: schema,
dataSources: data_source_ids,
templates: template_ids,
};
Expand All @@ -496,6 +507,7 @@ impl<'a> From<&'a super::SubgraphManifest> for SubgraphManifestEntity {
spec_version: manifest.spec_version.clone(),
description: manifest.description.clone(),
repository: manifest.repository.clone(),
features: manifest.features.iter().map(|f| f.to_string()).collect(),
schema: manifest.schema.document.clone().to_string(),
data_sources: manifest.data_sources.iter().map(Into::into).collect(),
templates: manifest
Expand Down
7 changes: 5 additions & 2 deletions graphql/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use graphql_parser::parse_schema;
use std::env;
use std::fs;
use std::process::exit;
use std::{collections::BTreeSet, env};

use graph_graphql::schema::api::api_schema;

Expand Down Expand Up @@ -31,7 +31,10 @@ pub fn main() {
};
let schema = ensure(fs::read_to_string(schema), "Can not read schema file");
let schema = ensure(parse_schema(&schema), "Failed to parse schema");
let schema = ensure(api_schema(&schema), "Failed to convert to API schema");
let schema = ensure(
api_schema(&schema, &BTreeSet::new()),
"Failed to convert to API schema",
);

println!("{}", schema);
}
Loading

0 comments on commit 47a4a10

Please sign in to comment.