Skip to content

Conversation

@2ndalpha
Copy link
Member

@2ndalpha 2ndalpha commented Dec 18, 2024

Breaking change how Qminder.GraphQL.query works.

  • It does not take string as an argument. Have to use gql from graphql-tag
  • It throws when there is no data in the response
  • It returns data from unwrapped response
  • It supports generics

Example usage is in app.ts

@2ndalpha 2ndalpha requested a review from KarlMae December 18, 2024 10:27
@2ndalpha 2ndalpha marked this pull request as ready for review December 18, 2024 10:28
Copy link
Contributor

@KarlMae KarlMae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Fetch
  2. Check for errors
  3. Check for data and unwrap it

The casting is also not necessary?

let graphQLResponse: GraphqlResponse = responseJson;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

@KarlMae KarlMae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go 🚀

@2ndalpha 2ndalpha added this pull request to the merge queue Jan 8, 2025
Merged via the queue into master with commit 9e78f37 Jan 8, 2025
9 checks passed
@2ndalpha 2ndalpha deleted the siim/graphql-query branch January 8, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants