GithubHelp home page GithubHelp logo

Comments (9)

dillonkearns avatar dillonkearns commented on June 26, 2024

Hey @xtian thanks for opening the issue. I thought about this when I wrote that code, but somehow it didn't seem like the obvious choice to ignore the data whenever an error occurs unconditionally, or to return both errors and data. Now that I think about it more, I think that maybe having multiple functions to handle these different strategies would be a good idea.

Apollo Client has a similar approach. They allow you to configure Apollo's error handling strategy so that when there is both success data and > 0 errors in the response, you can choose to have it:

  1. Ignore the error and act exactly like a regular successful response (this is the current Graphqelm behavior)
  2. Ignore the data and act exactly like a server error (perhaps this is a sensible default? It's hard to say, I'm not used to thinking about having both errors and a result in this kind of context so it's hard to know what the semantics should be)
  3. Give you both the data and the errors

For example, the Graphqelm.Http module could provide the following send functions (and similar for toTask:

-- Function for option 2
-- This would be the default that most people would use and it would behave as you might expect
send : (Result Error decodesTo -> msg) -> Request decodesTo -> Cmd msg
-- Function for option 1
sendIgnoreErrorIfHasData : (Result Error decodesTo -> msg) -> Request decodesTo -> Cmd msg
-- Function for option 3
sendIncludeErrorsIfHasData :
    (Result Error ( decodesTo, List GraphqlError ) -> msg)
    -> Request decodesTo
    -> Cmd msg

Maybe these names could be more concise or expressive, but something like that. Another approach would be to have a union type to say which kind of request you want to make explicitly, but I think I like the idea of encouraging the best practice (and obvious behavior) without confusing configuration options, and then exposing the less common options if you need them so I think I prefer having separate functions as above.

I also like that this multiple functions approach doesn't clutter up the return types by having this error data that you can forget to check if you want it to just fail if there were errors.

What do you think of the multiple functions approach?

from elm-graphql.

xtian avatar xtian commented on June 26, 2024

What do you think of the multiple functions approach?

I think that's a great idea! Would definitely work for our use-case

from elm-graphql.

dillonkearns avatar dillonkearns commented on June 26, 2024

Great! That feels like the right direction to me as well.

I wonder if it should be a major version bump? Because in a way this could be considered a breaking change.

from elm-graphql.

dillonkearns avatar dillonkearns commented on June 26, 2024

@xtian I'm thinking about merging these strategies into one. I've implemented this on master and I think this gives us the benefits of all of them pretty well. In a nutshell,

  • If there are any errors in the response, the whole thing is treated as an error
  • When there are errors in the response, the contents of data is always available as part of the data type (see the data type here)
  • If the decoder for data succeeds, the Error will contain ParsedData decodesTo. Otherwise it will contain UnparsedData Json.Decode.Value

I like this strategy because it always allows you to use the data when there is an error if you'd like to (or see it for debugging), but it also makes it clear when there's been an error instead of blurring that line. And the standard use that works for most people is easy to find and to use.

What do you think of this approach?

from elm-graphql.

xtian avatar xtian commented on June 26, 2024

That seems simpler to me. Sounds good!

from elm-graphql.

dillonkearns avatar dillonkearns commented on June 26, 2024

@xtian this has been published in Graphqelm Elm package version 11.0.0 (more details in the changelog). Let me know how this works for you. Thanks for reporting the issue!

from elm-graphql.

dillonkearns avatar dillonkearns commented on June 26, 2024

Hey @xtian I think this is resolved so I'll close this out, but please let me know if you have any issues or feedback. Thanks!

from elm-graphql.

xtian avatar xtian commented on June 26, 2024

@dillonkearns Just got around to upgrading our app to 11.1.0. These changes work great. Thanks so much!

from elm-graphql.

dillonkearns avatar dillonkearns commented on June 26, 2024

Fantastic, glad to hear it!

from elm-graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.