Skip to content

Commit

Permalink
graphql: Allow gt/lt comparisons with Bytes fields
Browse files Browse the repository at this point in the history
Fixes #4282
  • Loading branch information
lutter committed Jan 5, 2023
1 parent 5c53812 commit 4bf21f3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fields of type `Bytes` can now use less than and greater than filters [#4285](https://github.com/graphprotocol/graph-node/pull/4285)

#### Upgrade notes

- This release includes a **determinism fix** that should affect very few subgraphs on the network (currently only two). There was an issue that if a subgraph manifest had one data source with no contract address, listening to the same events or calls of another data source that has a specified address, the handlers for those would be called twice. With the fix, this will happen no more, the handler will be called just once like it should.
Expand Down
13 changes: 12 additions & 1 deletion graphql/src/schema/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,18 @@ fn field_scalar_filter_input_values(
match field_type.name.as_ref() {
"BigInt" => vec!["", "not", "gt", "lt", "gte", "lte", "in", "not_in"],
"Boolean" => vec!["", "not", "in", "not_in"],
"Bytes" => vec!["", "not", "in", "not_in", "contains", "not_contains"],
"Bytes" => vec![
"",
"not",
"gt",
"lt",
"gte",
"lte",
"in",
"not_in",
"contains",
"not_contains",
],
"BigDecimal" => vec!["", "not", "gt", "lt", "gte", "lte", "in", "not_in"],
"ID" => vec!["", "not", "gt", "lt", "gte", "lte", "in", "not_in"],
"Int" => vec!["", "not", "gt", "lt", "gte", "lte", "in", "not_in"],
Expand Down
49 changes: 45 additions & 4 deletions graphql/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn test_schema(id: DeploymentHash, id_type: IdType) -> Schema {
type Song @entity {
id: @ID@!
sid: String!
title: String!
writtenBy: Musician!
publisher: Publisher!
Expand Down Expand Up @@ -289,10 +290,10 @@ async fn insert_test_entities(
entity! { __typename: "Publisher", id: "0xb1" },
entity! { __typename: "Band", id: "b1", name: "The Musicians", originalSongs: vec![s[1], s[2]] },
entity! { __typename: "Band", id: "b2", name: "The Amateurs", originalSongs: vec![s[1], s[3], s[4]] },
entity! { __typename: "Song", id: s[1], title: "Cheesy Tune", publisher: "0xb1", writtenBy: "m1", media: vec![md[1], md[2]] },
entity! { __typename: "Song", id: s[2], title: "Rock Tune", publisher: "0xb1", writtenBy: "m2", media: vec![md[3], md[4]] },
entity! { __typename: "Song", id: s[3], title: "Pop Tune", publisher: "0xb1", writtenBy: "m1", media: vec![md[5]] },
entity! { __typename: "Song", id: s[4], title: "Folk Tune", publisher: "0xb1", writtenBy: "m3", media: vec![md[6]] },
entity! { __typename: "Song", id: s[1], sid: "s1", title: "Cheesy Tune", publisher: "0xb1", writtenBy: "m1", media: vec![md[1], md[2]] },
entity! { __typename: "Song", id: s[2], sid: "s2", title: "Rock Tune", publisher: "0xb1", writtenBy: "m2", media: vec![md[3], md[4]] },
entity! { __typename: "Song", id: s[3], sid: "s3", title: "Pop Tune", publisher: "0xb1", writtenBy: "m1", media: vec![md[5]] },
entity! { __typename: "Song", id: s[4], sid: "s4", title: "Folk Tune", publisher: "0xb1", writtenBy: "m3", media: vec![md[6]] },
entity! { __typename: "SongStat", id: s[1], played: 10 },
entity! { __typename: "SongStat", id: s[2], played: 15 },
entity! { __typename: "BandReview", id: "r1", body: "Bad musicians", band: "b1", author: "u1" },
Expand Down Expand Up @@ -403,6 +404,16 @@ impl From<&str> for QueryArgs {
}
}

impl From<String> for QueryArgs {
fn from(query: String) -> Self {
QueryArgs {
query,
variables: None,
max_complexity: None,
}
}
}

impl From<(&str, r::Value)> for QueryArgs {
fn from((query, vars): (&str, r::Value)) -> Self {
let vars = match vars {
Expand Down Expand Up @@ -2286,3 +2297,33 @@ fn trace_works() {
assert!(!trace.is_none(), "result has a trace");
})
}

/// Check that various comparisons against `id` work as expected. This also
/// serves as a test that they work for `String` as well as `Bytes` fields
/// in general
#[test]
fn can_compare_id() {
// For each entry `(cond, sids)` in this array, check that a query with
// a where clause `cond` returns a list of songs whose `sid` are the
// ones listed in `sids`
let checks = [
("id_gt: @S2@", vec!["s3", "s4"]),
("id_gte: @S2@", vec!["s2", "s3", "s4"]),
("id_lt: @S2@", vec!["s1"]),
("id_lte: @S2@", vec!["s1", "s2"]),
("id_not: @S2@", vec!["s1", "s3", "s4"]),
];

for (cond, sids) in checks {
let query = format!("query {{ songs(where: {{ {cond} }}) {{ sid }} }}");
let sids: Vec<_> = sids
.iter()
.map(|sid| object! { sid: sid.to_string() })
.collect();
let exp = object! { songs: sids };
run_query(query, move |result, id_type| {
let data = extract_data!(result).unwrap();
assert_eq!(data, exp, "check {} for {:?} ids", cond, id_type);
})
}
}

0 comments on commit 4bf21f3

Please # to comment.