0

I'm new to Spring Boot and I just started using graphql-spqr for Spring Boot since it allows for easy bootstrapping of Java projects.

However, as per my understanding, GraphQL basically allows the fetching of selected fields from the database. As per the examples, I've seen, this type of selection in the graphql-spqr library happens on the client side. Is there a way to do selection both client-side and server-side so as to speed up the queries?

I've looked into EntityGraph examples for GraphQL but they are mostly implemented for complex queries that involve JOINs. However, nothing exists for simple queries like findAll(), findById() etc.

I would like to use findAll() with the server fetching only the fields as requested by the client. How can I do that?

4
  • 2
    That implementation is entirely left upto you, graphql is just the communication protocol between the front end and the backend, consider it a sort of REST endpoint, it will allow you to make different type of endpoint, but how each endpoint is updated it's upto you
    – tsamridh86
    Commented Apr 19, 2021 at 4:42
  • Going on about the solution, you might have to pass the columns down the to sql level & might have to perform : select < columns > from table etc. in your implementation, as I don't think there's a library that does that for you, from the code to database layer. You can have a look at Hasura, but I'm not sure it will work to your specifications :)
    – tsamridh86
    Commented Apr 19, 2021 at 4:44
  • Understood. is there a way to generate dynamic queries for SQL based on the fields GraphQL client requests?
    – user782400
    Commented Apr 19, 2021 at 4:45
  • I think a tool called hasura might help, but I've only read articles about it, & have never tried it myself... you can have a look at it
    – tsamridh86
    Commented Apr 19, 2021 at 4:46

1 Answer 1

2

What was said in the comments is correct: GraphQL (and hence SPQR, as it's merely a tool to hook the schema up) does not know anything about SQL, databases, JOINs or anything else. It's a communication protocol, the rest is up to you. As for your situation, you'd have to inject the subselection into the resolver and pass it down to SQL. In the simplest case, it can look like this (in pseudo code):

public List<Book> books(@GraphQLEnvironment Set<String> fields) {
    //pass the requested field names further
    return database.query("SELECT " + fields + " FROM book");
}

You can inject ResolutionEnvironment using the same annotation, in case you need the full context.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.