Giter VIP home page Giter VIP logo

Comments (2)

mikebm avatar mikebm commented on July 25, 2024

I am also looking for best practices on this...

My current challenge is how to handle this during a server-side render(SSR). In the case of the REST API returning a 404, it causes a 404 error to be thrown, which then has to be handled within a catch statement. I have logic in the Query component for handling this behavior but during a SSR this logic is never hit because it throws exceptions instead.

Your solution is an option but painful like you said.

Another option, which I am not happy with, is setting the errorPolicy="all" on the Query component.

This causes the SSR to succeed and allows the apollo client to cache all the other successful responses. However, it then causes the client to re-issue the request and handle it accordingly, which results in the re-issuing same request the server already performed.

from datasource-rest.

jalovatt avatar jalovatt commented on July 25, 2024

I just ran into this myself, although in my case it was in the context of testing with an intentionally bad id for the API I'm hitting - i.e. expect(doTheThing(badId)).toThrow() or .toEqual(null) and the 404 was breaking it.

Here's a less painful version of the original solution, making use of the fact that await is just a fancy Promise:

return dataSources.listingApi.getListing(args.reference)
  .catch(err => {
    if(err.extensions.response.status === 404) {
      return null;
    }
      throw err;
    }
  })
  • Note that return await is unnecessary in an async function; you can just return the awaited expression.
  • This will get even shorter if throw expressions are added to ES (currently Stage 2, and available with Babel:
.catch(err => ((err.extensions.response.status === 404) ? null : throw err)

from datasource-rest.

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.