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

skip_serializing_none does not work #439

Closed
Nohac opened this issue Nov 1, 2022 · 7 comments
Closed

skip_serializing_none does not work #439

Nohac opened this issue Nov 1, 2022 · 7 comments
Labels
bug Something isn't working

Comments

@Nohac
Copy link

Nohac commented Nov 1, 2022

I tried asking this question on the actual PR #431 that was meant to solve this issue, but got no response, so I'm just gonna move the question into this issue.

I've added skip_serializing_none to the struct as stated in the README, but doing so still leaves me with null values after serialization.

My first guess was that this hasn't been released yet, so I switch over to the main branch in Cargo.toml, but that changed nothing. Here's the code I'm using:

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "graphql/schema.graphql",
    query_path = "graphql/insert_mutation.graphql",
    response_derives = "Debug",
    skip_serializing_none
)]
pub struct InsertMutation;
let vars = Variables {
    // .. omitted
    user_id: None,
    // .. omitted
};

let q = crate::graphql::InsertMutation::build_query(vars);

let skipped = serde_json::to_string_pretty(&q).unwrap();
println!("{skipped}");

Output:

{
  "variables": {
    // .. omitted
    "user_id": null,
    // .. omitted
  },
  ...
}

Is there something obvious I'm missing here to make this work, or is this something that warrants it's own issue?

@tomhoule
Copy link
Member

tomhoule commented Nov 1, 2022

I unfortunately don't have time to spend on this project to investigate further or fix things, but I'm happy to review PRs. FWIW it looks like a bug.

@tomhoule tomhoule added the bug Something isn't working label Nov 1, 2022
@Nohac
Copy link
Author

Nohac commented Nov 1, 2022

Thanks for the quick reply.

This is unfortunately a blocking issue for me, unless there's some way to work around this issue. Since hasura 2.x now reports an error if input fields with null values are present, it's no longer possible to get the mutations to work.

I might try opening a PR if I get the time. Any hints on how approach fixing this issue would be appreciated.

@Nohac
Copy link
Author

Nohac commented Nov 1, 2022

So I tried to switch to the git version again, and this time it seems to fix the problem, it's only v0.11.0 that doesn't work. So my first assumption was correct, that this features has not yet been released. Not sure why switching last time didn't work, might have been a caching issue or something.

Hoping we'll see a v0.12.0 soon! Until then, I'm just gonna depend on the git repo directly.

@Nohac Nohac closed this as completed Nov 1, 2022
@L1ghtman2k
Copy link

I am also running into this issue in v0.12.0, I think this issue should be reopened.

When expanding macro with skip_serialize_none, it doesn't seem like it is applied to the variables.

Example:

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "./github.schema.graphql",
    query_path = "./github.query.graphql",
    response_derives = "Debug, PartialEq",
    skip_serialize_none
)]
pub struct BranchProtectionsView;

github.schema.graphql: https://docs.github.com/public/schema.docs.graphql

github.query.graphql:

query BranchProtectionsView($owner: String!, $name: String!, $cursor: String, $first: Int = 90) {
    repository(owner: $owner, name: $name) {
        branchProtectionRules(first: $first, after: $cursor) {
            pageInfo {
                endCursor
                hasNextPage
            }
            totalCount
            edges {
                cursor
                node {
                    pattern
                    requiresApprovingReviews
                }
            }
        }
    }
}

@Akremkadri
Copy link

I am encountering the same issue in v0.13.0. This problem should be reopened. When expanding a macro with skip_serialize_none, it appears that it is not being applied to the variables in the query.

@elimirks
Copy link

elimirks commented May 19, 2024

It seems like it only works for nested variables. Here's a test patch to reproduce:

diff --git a/graphql_client/tests/skip_serializing_none.rs b/graphql_client/tests/skip_serializing_none.rs
index 0d26184..40b0f9f 100644
--- a/graphql_client/tests/skip_serializing_none.rs
+++ b/graphql_client/tests/skip_serializing_none.rs
@@ -13,6 +13,7 @@ fn skip_serializing_none() {
     use skip_serializing_none_mutation::*;
 
     let query = SkipSerializingNoneMutation::build_query(Variables {
+        foo: None,
         param: Some(Param {
             data: Author {
                 name: "test".to_owned(),
@@ -26,4 +27,5 @@ fn skip_serializing_none() {
     println!("{}", stringified);
 
     assert!(stringified.contains(r#""data":{"name":"test"}"#));
+    assert!(stringified.contains(r#""variables":{"param":{"data":{"name":"test"}}}"#));
 }
diff --git a/graphql_client/tests/skip_serializing_none/query.graphql b/graphql_client/tests/skip_serializing_none/query.graphql
index 028ae76..f648300 100644
--- a/graphql_client/tests/skip_serializing_none/query.graphql
+++ b/graphql_client/tests/skip_serializing_none/query.graphql
@@ -1,4 +1,4 @@
-mutation SkipSerializingNoneMutation($param: Param) {
+mutation SkipSerializingNoneMutation($param: Param, $foo: Int) {
   optInput(query: $param) {
     name
     __typename
---- skip_serializing_none stdout ----
{"variables":{"param":{"data":{"name":"test"}},"foo":null},"query":"mutation SkipSerializingNoneMutation($param: Param, $foo: Int) {\n  optInput(query: $param) {\n    name\n    __typename\n  }\n}\n","operationName":"SkipSerializingNoneMutation"}
thread 'skip_serializing_none' panicked at graphql_client/tests/skip_serializing_none.rs:30:5:
assertion failed: stringified.contains(r#""variables":{"param":{"data":{"name":"test"}}}"#)

elimirks added a commit to elimirks/graphql-client that referenced this issue May 19, 2024
@elimirks
Copy link

elimirks commented May 19, 2024

@Akremkadri looks like you also ran into this issue recently, I submitted a PR here: #485

Does it work for you? The new test checks for both root level and nested variable Options now, so I think everything is covered.

elimirks added a commit to elimirks/graphql-client that referenced this issue May 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants