feat(graphql): query verifies the response#767
Merged
Conversation
KarlMae
suggested changes
Jan 6, 2025
Contributor
KarlMae
left a comment
There was a problem hiding this comment.
While testing I converted some services to use the generic and it's so much cleaner, thanks!
I left some improvement ideas.
| if (graphQLResponse.errors && graphQLResponse.errors.length > 0) { | ||
| throw this.extractGraphQLError(graphQLResponse); | ||
| } | ||
| return graphQLResponse.data as T; |
Contributor
There was a problem hiding this comment.
IMO the function would read much smoother like this:
let response = await fetch(`https://${this.apiServer}/graphql`, init);
let responseJson = await response.json();
if (responseJson.errors?.length) {
throw this.extractGraphQLError(responseJson);
}
if (!responseJson.data) {
throw new Error(`Server response is not valid GraphQL response. Response: ${ JSON.stringify(responseJson) }`);
}
return responseJson.data as T;
It subjectively has an easier to follow structure:
- Fetch
- Check for errors
- Check for data and unwrap it
The casting is also not necessary?
let graphQLResponse: GraphqlResponse = responseJson;
Member
Author
There was a problem hiding this comment.
Casting is still needed but I refactored it a bit
| const query = queryToString(queryDocument); | ||
| ): Promise<T> { | ||
| const query = print(queryDocument); | ||
| if (!query || query.length === 0) { |
Contributor
There was a problem hiding this comment.
I think we can remove this check as well.
Query can not be undefined as it is clearly set on the line above.
Query can also not have length 0 because gql DocumentNode can not be empty as tested here:
DOM
<button (click)="handleClick()">Test</button>
class
async handleClick(): Promise<void> {
await Qminder.GraphQL.query(gql``);
}
Result
Screen.Recording.2025-01-06.at.10.31.25.mov
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Breaking change how
Qminder.GraphQL.queryworks.stringas an argument. Have to usegqlfromgraphql-tagdatain the responsedatafrom unwrapped responseExample usage is in
app.ts