validation exchange #1084
-
I spent some time yesterday trying to create a validation exchange to check operation variables before they reach the cache or server. It'll spit back errors and prevent the operation from forwarding if it fails validation. I'm thinking you'll use it a little like this. // in the component
let arguments = {
name: [variable, {length: 8, not_null: true, other_check: ()=>{pass custom rule here}]
}
// in the exchange.
const rule_book = {
length: () => check_length(),
not_null: () => check_not_null(),
} I was musing about it this morning and I think there's a snag with my idea. If someone triggers an operation which fails validation the data field given back will be empty and we'll be left with an empty UI in some cases (for queries with arguments). Is there anyway to get around this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You'll probably want to mimick the errors a server would send back for invalid variables, so instead of dropping the mutation you may want to instead send a mock response in your exchange immediately that contains an actual error. You can even do this without constructing a full response, since we have a helper that converts a server result to an However it'd probably make more sense for your exchange to output a result with |
Beta Was this translation helpful? Give feedback.
You'll probably want to mimick the errors a server would send back for invalid variables, so instead of dropping the mutation you may want to instead send a mock response in your exchange immediately that contains an actual error. You can even do this without constructing a full response, since we have a helper that converts a server result to an
urql
result (with aCombinedError
). So if you wanted to just write a simplenetworkError
this would help: https://formidable.com/open-source/urql/docs/api/core/#makeerrorresultHowever it'd probably make more sense for your exchange to output a result with
GraphQLError
instances using this helper: https://formidable.com/open-source/urql/docs/api/…