@@ -5,6 +5,7 @@ use std::sync::Arc;
5
5
use std:: time:: Duration ;
6
6
7
7
use anyhow:: { anyhow, Error } ;
8
+ use semver:: Version ;
8
9
use store:: Entity ;
9
10
10
11
use crate :: bail;
@@ -774,7 +775,7 @@ impl InputSchema {
774
775
/// A convenience function for creating an `InputSchema` from the string
775
776
/// representation of the subgraph's GraphQL schema `raw` and its
776
777
/// deployment hash `id`. The returned schema is fully validated.
777
- pub fn parse ( raw : & str , id : DeploymentHash ) -> Result < Self , Error > {
778
+ pub fn parse ( spec_version : & Version , raw : & str , id : DeploymentHash ) -> Result < Self , Error > {
778
779
fn agg_mappings ( ts_types : & [ TypeInfo ] ) -> Box < [ AggregationMapping ] > {
779
780
let mut mappings: Vec < _ > = ts_types
780
781
. iter ( )
@@ -798,7 +799,7 @@ impl InputSchema {
798
799
}
799
800
800
801
let schema = Schema :: parse ( raw, id. clone ( ) ) ?;
801
- validations:: validate ( & schema) . map_err ( |errors| {
802
+ validations:: validate ( spec_version , & schema) . map_err ( |errors| {
802
803
anyhow ! (
803
804
"Validation errors in subgraph `{}`:\n {}" ,
804
805
id,
@@ -859,6 +860,13 @@ impl InputSchema {
859
860
} )
860
861
}
861
862
863
+ /// Parse with the latest spec version
864
+ pub fn parse_latest ( raw : & str , id : DeploymentHash ) -> Result < Self , Error > {
865
+ use crate :: data:: subgraph:: LATEST_VERSION ;
866
+
867
+ Self :: parse ( LATEST_VERSION , raw, id)
868
+ }
869
+
862
870
/// Convenience for tests to construct an `InputSchema`
863
871
///
864
872
/// # Panics
@@ -868,7 +876,7 @@ impl InputSchema {
868
876
#[ track_caller]
869
877
pub fn raw ( document : & str , hash : & str ) -> Self {
870
878
let hash = DeploymentHash :: new ( hash) . unwrap ( ) ;
871
- Self :: parse ( document, hash) . unwrap ( )
879
+ Self :: parse_latest ( document, hash) . unwrap ( )
872
880
}
873
881
874
882
pub fn schema ( & self ) -> & Schema {
@@ -1324,6 +1332,7 @@ mod validations {
1324
1332
1325
1333
use inflector:: Inflector ;
1326
1334
use itertools:: Itertools ;
1335
+ use semver:: Version ;
1327
1336
1328
1337
use crate :: {
1329
1338
data:: {
@@ -1343,15 +1352,20 @@ mod validations {
1343
1352
1344
1353
/// Helper struct for validations
1345
1354
struct Schema < ' a > {
1355
+ #[ allow( dead_code) ]
1356
+ spec_version : & ' a Version ,
1346
1357
schema : & ' a BaseSchema ,
1347
1358
subgraph_schema_type : Option < & ' a s:: ObjectType > ,
1348
1359
// All entity types, excluding the subgraph schema type
1349
1360
entity_types : Vec < & ' a s:: ObjectType > ,
1350
1361
aggregations : Vec < & ' a s:: ObjectType > ,
1351
1362
}
1352
1363
1353
- pub ( super ) fn validate ( schema : & BaseSchema ) -> Result < ( ) , Vec < SchemaValidationError > > {
1354
- let schema = Schema :: new ( schema) ;
1364
+ pub ( super ) fn validate (
1365
+ spec_version : & Version ,
1366
+ schema : & BaseSchema ,
1367
+ ) -> Result < ( ) , Vec < SchemaValidationError > > {
1368
+ let schema = Schema :: new ( spec_version, schema) ;
1355
1369
1356
1370
let mut errors: Vec < SchemaValidationError > = [
1357
1371
schema. validate_no_extra_types ( ) ,
@@ -1380,14 +1394,15 @@ mod validations {
1380
1394
}
1381
1395
1382
1396
impl < ' a > Schema < ' a > {
1383
- fn new ( schema : & ' a BaseSchema ) -> Self {
1397
+ fn new ( spec_version : & ' a Version , schema : & ' a BaseSchema ) -> Self {
1384
1398
let subgraph_schema_type = schema. subgraph_schema_object_type ( ) ;
1385
1399
let mut entity_types = schema. document . get_object_type_definitions ( ) ;
1386
1400
entity_types. retain ( |obj_type| obj_type. find_directive ( kw:: ENTITY ) . is_some ( ) ) ;
1387
1401
let mut aggregations = schema. document . get_object_type_definitions ( ) ;
1388
1402
aggregations. retain ( |obj_type| obj_type. find_directive ( kw:: AGGREGATION ) . is_some ( ) ) ;
1389
1403
1390
1404
Schema {
1405
+ spec_version,
1391
1406
schema,
1392
1407
subgraph_schema_type,
1393
1408
entity_types,
@@ -2276,7 +2291,7 @@ mod validations {
2276
2291
mod tests {
2277
2292
use std:: ffi:: OsString ;
2278
2293
2279
- use crate :: prelude:: DeploymentHash ;
2294
+ use crate :: { data :: subgraph :: LATEST_VERSION , prelude:: DeploymentHash } ;
2280
2295
2281
2296
use super :: * ;
2282
2297
@@ -2285,6 +2300,10 @@ mod validations {
2285
2300
BaseSchema :: parse ( schema, hash) . unwrap ( )
2286
2301
}
2287
2302
2303
+ fn validate ( schema : & BaseSchema ) -> Result < ( ) , Vec < SchemaValidationError > > {
2304
+ super :: validate ( LATEST_VERSION , schema)
2305
+ }
2306
+
2288
2307
#[ test]
2289
2308
fn object_types_have_id ( ) {
2290
2309
const NO_ID : & str = "type User @entity { name: String! }" ;
@@ -2369,7 +2388,7 @@ type Account implements Address @entity { id: ID!, txn: Transaction! @derivedFro
2369
2388
. expect ( "Failed to parse raw schema" )
2370
2389
. into_static ( ) ;
2371
2390
let schema = BaseSchema :: new ( DeploymentHash :: new ( "id" ) . unwrap ( ) , document) . unwrap ( ) ;
2372
- let schema = Schema :: new ( & schema) ;
2391
+ let schema = Schema :: new ( LATEST_VERSION , & schema) ;
2373
2392
match schema. validate_derived_from ( ) {
2374
2393
Err ( ref e) => match e {
2375
2394
SchemaValidationError :: InvalidDerivedFrom ( _, _, msg) => {
@@ -2425,7 +2444,7 @@ type _Schema_ { id: ID! }";
2425
2444
let document =
2426
2445
graphql_parser:: parse_schema ( ROOT_SCHEMA ) . expect ( "Failed to parse root schema" ) ;
2427
2446
let schema = BaseSchema :: new ( DeploymentHash :: new ( "id" ) . unwrap ( ) , document) . unwrap ( ) ;
2428
- let schema = Schema :: new ( & schema) ;
2447
+ let schema = Schema :: new ( LATEST_VERSION , & schema) ;
2429
2448
assert_eq ! (
2430
2449
schema. validate_schema_type_has_no_fields( ) . expect_err(
2431
2450
"Expected validation to fail due to fields defined on the reserved type"
@@ -2442,7 +2461,7 @@ type _Schema_ @illegal";
2442
2461
let document =
2443
2462
graphql_parser:: parse_schema ( ROOT_SCHEMA ) . expect ( "Failed to parse root schema" ) ;
2444
2463
let schema = BaseSchema :: new ( DeploymentHash :: new ( "id" ) . unwrap ( ) , document) . unwrap ( ) ;
2445
- let schema = Schema :: new ( & schema) ;
2464
+ let schema = Schema :: new ( LATEST_VERSION , & schema) ;
2446
2465
assert_eq ! (
2447
2466
schema. validate_directives_on_schema_type( ) . expect_err(
2448
2467
"Expected validation to fail due to extra imports defined on the reserved type"
@@ -2467,7 +2486,7 @@ type A @entity {
2467
2486
let document =
2468
2487
graphql_parser:: parse_schema ( ROOT_SCHEMA ) . expect ( "Failed to parse root schema" ) ;
2469
2488
let schema = BaseSchema :: new ( DeploymentHash :: new ( "id" ) . unwrap ( ) , document) . unwrap ( ) ;
2470
- let schema = Schema :: new ( & schema) ;
2489
+ let schema = Schema :: new ( LATEST_VERSION , & schema) ;
2471
2490
assert_eq ! ( schema. validate_fields( ) . len( ) , 0 ) ;
2472
2491
}
2473
2492
@@ -2575,7 +2594,7 @@ type Gravatar @entity {
2575
2594
2576
2595
let document = graphql_parser:: parse_schema ( SCHEMA ) . expect ( "Failed to parse schema" ) ;
2577
2596
let schema = BaseSchema :: new ( DeploymentHash :: new ( "id1" ) . unwrap ( ) , document) . unwrap ( ) ;
2578
- let schema = Schema :: new ( & schema) ;
2597
+ let schema = Schema :: new ( LATEST_VERSION , & schema) ;
2579
2598
assert_eq ! ( schema. validate_fulltext_directives( ) , vec![ ] ) ;
2580
2599
}
2581
2600
@@ -2706,7 +2725,7 @@ mod tests {
2706
2725
2707
2726
fn make_schema ( ) -> InputSchema {
2708
2727
let id = DeploymentHash :: new ( "test" ) . unwrap ( ) ;
2709
- InputSchema :: parse ( SCHEMA , id) . unwrap ( )
2728
+ InputSchema :: parse_latest ( SCHEMA , id) . unwrap ( )
2710
2729
}
2711
2730
2712
2731
#[ test]
@@ -2748,7 +2767,7 @@ mod tests {
2748
2767
"# ;
2749
2768
2750
2769
let id = DeploymentHash :: new ( "test" ) . unwrap ( ) ;
2751
- let schema = InputSchema :: parse ( SCHEMA , id) . unwrap ( ) ;
2770
+ let schema = InputSchema :: parse_latest ( SCHEMA , id) . unwrap ( ) ;
2752
2771
2753
2772
let dog = schema. entity_type ( "Dog" ) . unwrap ( ) ;
2754
2773
let cat = schema. entity_type ( "Cat" ) . unwrap ( ) ;
0 commit comments