Skip to content

Support null variables #426

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

Merged
merged 5 commits into from
Mar 11, 2023
Merged

Support null variables #426

merged 5 commits into from
Mar 11, 2023

Conversation

zth
Copy link
Owner

@zth zth commented Feb 27, 2023

Closes #348

This introduces a directive @rescriptRelayNullableVariables on operations that will allow you to leverage Js.Nullable.t to send null explicitly to your APIs.

You can test this in your project by running yarn add rescript-relay@support-nullable-variables.

How it works in a nutshell:

module Mutation = %relay(`
    mutation TestNullableVariablesMutation($avatarUrl: String) @rescriptRelayNullableVariables {
      updateUserAvatar(avatarUrl: $avatarUrl) {
        user {
          avatarUrl
        }
      }
    }
`)

// All optional variables in this operation can now be `null`, which will be preserved and sent to your server.
Mutation.commitMutation(~environment=RelayEnvironment.environment, ~variables={avatarUrl: Js.Nullable.null}, ())

// Or if you want to send a value:
Mutation.commitMutation(~environment=RelayEnvironment.environment, ~variables={avatarUrl: Js.Nullable.return("some-avatar-url")}, ())

This works for:

  • Variables in queries/mutations/subscriptions.
  • Input objects (they'll automatically be nullable when you use this directive)

The reason for this being an explicit directive and not applied globally is that this isn't the default you want, working with options is still so much easier.

Notes:

  • This is in a sense a "temporary" solution. I'll want to do a retake on this and a bunch of related things eventually. But that's more for a "RescriptRelay 2.0", and this will be the way to go for the foreseeable future.
  • Use this only if you really need it.
  • It doesn't work for refetch variables. That likely won't be covered until a "RescriptRelay 2.0".

@mununki
Copy link

mununki commented Mar 1, 2023

type Input {
  a: Int!
  b: Int
  c: int
}
module M = %relay(`
  mutation Test($input: Input!) @rescriptRelayNullableVariables {
    test(input: $input) {
      result
    }
  }
`)

M.commitMutation(~variables=M.makeVarialbes(~input=M.makeVariables_input(~a=1, ~b=Js.Nullable.null, ())))

The variables on the network request would be something like{a: 1, b: null}?

@zth
Copy link
Owner Author

zth commented Mar 1, 2023

type Input {
  a: Int!
  b: Int
  c: int
}
module M = %relay(`
  mutation Test($input: Input!) @rescriptRelayNullableVariables {
    test(input: $input) {
      result
    }
  }
`)

M.commitMutation(~variables=M.makeVarialbes(~input=M.makeVariables_input(~a=1, ~b=Js.Nullable.null, ())))

The variables on the network request would be something like{a: 1, b: null}?

Not 100%, you'll need to wrap everything with Js.Nullable. I haven't added support for object makers (most likely won't since they might be going away in the future), so it'd need to be:

M.commitMutation(~variables=(input: Js.Nullable.return({a: Js.Nullable.return(1), b: Js.Nullable.null})}, ())

But then yes, the network request would be: {input: {a: 1, b: null}}

@zth
Copy link
Owner Author

zth commented Mar 7, 2023

Has anyone tried this for real yet? Cc @lozlow

@zth
Copy link
Owner Author

zth commented Mar 11, 2023

Will merge this and we can iterate on it more if needed.

@zth zth merged commit cd18e42 into master Mar 11, 2023
@zth zth deleted the support-null-variables branch March 11, 2023 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Distinguishing between nulls and undefindes in variables
2 participants