Git Product home page Git Product logo

Comments (5)

knaeckeKami avatar knaeckeKami commented on July 30, 2024 1

Ok.

It is recommended to not use HTTP status codes 4xx or 5xx in situations where you could use a well-formed graphql response, e.g something like

{
"data": null,
"errors": [ {"message": "User is not authenticated", "code": "..."}] 
}

There is no definitive standard for this, as graphql is not bound to a particular transport method (HTTP, websockets... ), but there is this spec: https://github.com/graphql/graphql-over-http , specifically https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md

So for Content-Types of application/json, which the HttpClient uses, the spec says

The server SHOULD NOT use a 4xx or 5xx status code.

This is why HttpLinks throws an Exception instead of returning a Response() object with the parsed errors, which is why your graphqlErrors list is null and the error details are only in your LinkException.

HttpLink should probably make this configurable though.

What you can do here instead:

  • put an ErrorLink() before your HttpLink() so you can turn these Exceptions into Responses with proper graphlErrors
  • just cast your exceptions to HttpLinkServerException, but then you are relying on the current implementation of your terminal link
  • open a PR in the gql-dart repo to add configuration in the HttpLink on which status code are expected (which should return a normal Response if well-formed) and which are unexpected errors which should always be treated a exception

from ferry.

dunatron avatar dunatron commented on July 30, 2024 1

You shouldn't really be throwing errors for graphQL servers if you can help it IMO.
Errors are schemaless and hard to keep track of.

Consider using Unions instead, that way it makes it easier for an application to respond to what has happened.
Your application is then not hijacked by REST status codes and you can make objects of types that make sense for your application

interface BaseError {
  message: String!
}

type UnknownError implements BaseError {
  message: String!
}

type NotAllowedError implements BaseError {
  message: String!
}

type UsersPage {
  count: Int!
  edges: [UserEdge!]!
  pageInfo: PageInfo!
}

union UsersPageResult =
    UsersPage
  | NotAuthed
  | NotAllowedError
  | UnknownError
  
type Query {
  users(input: UserCursorPaginationInput!): UsersPageResult!
}

from ferry.

pmcavoy89 avatar pmcavoy89 commented on July 30, 2024

Hm... seems like this solved it;

print("${(res.linkException as HttpLinkServerException).parsedResponse}");

Is there any documentation around this or a better way we can expose the error?

from ferry.

knaeckeKami avatar knaeckeKami commented on July 30, 2024

Does the server send an HTTP status code > 300 for this error?

the httpLink only parses requests with HTTP status code >= 200 <300, otherwise it throws an exception.

See https://github.com/gql-dart/gql/blob/master/links/gql_http_link/lib/src/link.dart#L80

from ferry.

pmcavoy89 avatar pmcavoy89 commented on July 30, 2024

Yup. It throws a 401. That's what made me think I needed to cast the res.linkException with HttpLinkServerException.

throw new GraphQLError('User is not authenticated', {
  extensions: {
    code: 'Unauthenticated',
    http: { status: 401 },
  },
});

from ferry.

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.