GraphQL Error and Response handling (with Spring for GraphQL)

  Рет қаралды 4,271

Nils Hartmann

Nils Hartmann

Күн бұрын

Пікірлер: 9
@theparten
@theparten 7 ай бұрын
great video❤, but i need to ask what's the best way on the client side who consumes the graphql api to know if the response is an error or not. Got used to REST where the http codes themselves are enough and now graphql seems a bit trickier.
@nils-hartmann
@nils-hartmann 7 ай бұрын
actually http response codes doesn't play an important role in graphql communication (now), so you would only check for errors in your application by inspecting the result: either you have "data" or "errors" field (indicating a technical error) or both. If you have "data", then - depending on your schema - that field can contain "expected" errors (like field validation errors, unmet requirements etc.) There is a spec proposal "GraphQL over http" that would add some http status codes, but I'm unsure about the state of that draft (github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md)
@kumard3109
@kumard3109 Жыл бұрын
Good Explanation. Thanks a lot.
@kc_official
@kc_official 2 жыл бұрын
Thanks for the crisp and clear video. Would like to know how we can perform attribute name resolution? For example let's say graphql schema has attributes with snake_case and Java will be using camelCase, how we are going to setup the configuration to resolve this?
@nils-hartmann
@nils-hartmann 2 жыл бұрын
I'm not sure if there is an easy or generic way. There is the PropertyDataFetcher from graphql-java that is used if you don't register your own DataFetcher or resolver/mapping function for a field. That DataFetcher uses some naming conventions (java bean style). Maybe it would be somehow possible to write and register an own default datafetcher, but that might not be easy. If you only have a few cases where java attribute name does not match graphql field name, I would implement own SchemaResolver for that fields
@kc_official
@kc_official 2 жыл бұрын
@@nils-hartmann thanks for the reply, you are right. PropertyDataFetcher is the default one and following Java bean naming convention, but I was looking more generic way, like how we are going to mention the @JsonProperty annotation as part of our DTO or like @Column(name="") as part of the entity. It would be great, if you can have a video or article on the same how we are achieve that. I see in graphql-java-tools they were able to achieve the same with FieldResolverScanner. Is there any way we can use the same in Spring for GraphQL?
@Anbu_Sampath
@Anbu_Sampath 2 жыл бұрын
Curious to know how union types error response handled in client side. Because what concrete class object mapping handles both success and error response .
@nils-hartmann
@nils-hartmann 2 жыл бұрын
In general I see two options: first create a class that contains all fields from all union types you actually have included in your query. Something like this for the mutation from the video: // contains the fields you have selected in your query from Vet record VetResponse(String id) {} record AddVetResponse(VetResponse vet, String errorMessage) {} (mabye vet and errorMessage could be Optional's) Or you select also the __typename field in your query (mutation addVet(...) { __typename ...on AddVetSuccessPayload { ... } ...onAddVetFailedPayload { ... } }). The __typename field returns the name of the union type in your answer ("AddVetSuccessPayload" or "AddVetFailedPayload"). Based on the typename you can handle your response differently. For example using the execute-Method from Spring GraphQL's GraphQlClient Object (docs.spring.io/spring-graphql/docs/1.0.0-SNAPSHOT/reference/html/#client-requests-execute) you could first read the __typename field and then determine which field(s) you call the toEntity method for. It also might be that is easier to create an own Error-Object for the AddVetErrorPayload type (type AddVetErrorPayload { error: AddVetError }) if you have more than one field in the error type. This is definitley a place where code generators (like www.graphql-code-generator.com/ for TypeScript) would be helpful 😊 Hope that helps!
@Anbu_Sampath
@Anbu_Sampath 2 жыл бұрын
@@nils-hartmann Thanks, I will try and let you know how it goes.
DataLoader with "Spring for GraphQL"
20:51
Nils Hartmann
Рет қаралды 3,8 М.
#10.RLC Framework - Episode4 - Kernel Class.
4:16
Omar Abdulwahhab
Рет қаралды 10
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.
We Attempted The Impossible 😱
00:54
Topper Guild
Рет қаралды 56 МЛН
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
IntelliJ IDEA Tooling for (Java) GraphQL development
18:24
Nils Hartmann
Рет қаралды 1,7 М.
Introduction into TanStack Router for React
1:04:26
Nils Hartmann
Рет қаралды 4,3 М.
Google’s Quantum Chip: Did We Just Tap Into Parallel Universes?
9:34
Async Method Execution in Spring
33:12
Developer Hut
Рет қаралды 107
TCP Socket Tutorial In Java
5:34
I Don't know
Рет қаралды 54
Keyboard and mouse input
3:15
Automation with Aman Garg
Рет қаралды 11
Сестра обхитрила!
00:17
Victoria Portfolio
Рет қаралды 958 М.