Comments (11)
As for the SELECT ... FROM ...
optimization, it is possible, but at this moment not very convenient if the field isn't an object (i.e. if you're selecting from an interface or a union).
If the current field is an object type, you can get the sub-field names injected simply via @GraphQLEnvironment Set<String> subFields
parameter, e.g.
@GraphQLQuery(name = "userVotes")
public List<Vote> getUserVotes(@GraphQLContext User user, @GraphQLEnvironment Set<String> subFields) {
//do a SELECT here using the field names from subFields instead of *
...
}
But, if the current field is not an object, there's no such easy way to inject the sub-fields. But what you can still do in inject @GraphQLEnvironment ResolutionEnvironment env
, from here you can get a hold of DataFetchingEnvironment
(standard graphql-java API) via env.dataFetchingEnvironment
and from there you can get the current field and it's sub-selections, but it gets tricky with aliases, conditional selects etc. graphql-java also doesn't deal with these cases, so I'll have to rework the sub-selection injection in graphql-spqr itself to be more usable. It's high on my list, so you'll get that feature soon :)
from graphql-spqr.
You're absolutely right, the documentation is the sore point of this project. I've been promising to do something abut for a while now, but it's a gigantic task...
In the meantime, can you post an example (or a link to a sample project if you have one) of what you're trying to achieve and I'll help you out.
from graphql-spqr.
Argh, the enum type is a genuine bug. I'll fix it immediately.
graphql-java changed the way it deals with duplicated types between versions 2 and 3, and graphql-spqr still has some leftover incompatibilities that pop up.
from graphql-spqr.
Ok if this is a bug i'll stop trying going around it :)
In the meantime i will remove the field to avoid it.
I'll explain what i want to achieve, since i am kinda stuck i don't have much of a project to provide.
My schema is :
- One table User
- One table Entity
- One table Qualifier
A user can vote for an Entity using a qualifier.
So i have an edge/many-to-many relationship with 3 ids called "Vote"
I have a class User, Entity and Qualifier.
For instance my User class look like this :
package com.dcode.andhrimnir.engine.domain;
import io.leangen.graphql.annotations.GraphQLId;
import io.leangen.graphql.annotations.GraphQLNonNull;
import io.leangen.graphql.annotations.GraphQLQuery;
import java.net.URL;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Locale;
public class User {
@GraphQLId()
@GraphQLQuery(name = "id", description = "A users's id")
@GraphQLNonNull
private String id;
private String email;
private String firstName;
private String lastName;
private String displayName;
private URL avatar;
@GraphQLNonNull
private Gender gender;
@GraphQLNonNull
private Locale language;
private LocalDate dateOfBirth;
private int level;
private LocalDateTime creationDate;
private LocalDateTime updateDate;
// Getters and setters
// ...
}
Other classes look the same, they have the same annotated id and query.
My questions there would be :
- Do i have to add GraphQLId(relayId = true) for subquery to work? I'm not sure.
- Do i have to annotate the class with GraphQLType ?
I also have a UserQueries class that i registered on my GraphQLSchema using withOperationsFromSingleton.
I can do a simple query by id with :
@GraphQLQuery(name = "user")
public User getUserById(@GraphQLId @GraphQLArgument(name = "id") @GraphQLNonNull String id) {
...
}
But where i am stuck completely is if i want to do a GraphQL query like :
{
user(id:1565890985580299324) {
firstName
lastName
email
votes {
entity {
id
}
qualifier {
id
}
}
}
}
I want all the votes the user did including the detail about the entity and qualifier.
I would want to reuse existing query by id for entity and qualifier if possible.
At this point i don't really know what to do.
What should i add to User, Entity, Qualifier, UserQueries classes?
Do i need to create a VoteEdge class that would implement Edge ? Coming for a graph database i suppose you would only need it if i want to have metadata on the edge (like a vote date)
Thanks a lot for your help
from graphql-spqr.
I've just fixed the enum issue and released version 0.9.2 that also contains out-of-the-box support for some of the types you're using (Locale
, LocalDate
, LocalDateTime
).
You can get it immediately from Bintray (should also sync with JCenter). Maven Central will sync in a couple of hours.
This will solve some of the issues you're having.
I'll also try answering some of the questions:
relayId
,Edge
etc are used for Relay Connection spec compatibility, they're only of interest if you're using Relay as the client on the front-end.@GraphQLType
is only used for meta-data: to override the type name and attach the description. Not mandatory.@GraphQLContext
is indeed the correct mechanism for injecting the enclosing object
You can find a working example at https://github.com/leangen/graphql-spqr-samples
I'll look into your specific example and give you more precise guidance a bit later, when I get a chance.
from graphql-spqr.
from graphql-spqr.
@depauwjimmy In my rush to fix the enum mapping, I missed a case when the same enum is used for both input and output π¨
I've now fixed it and added proper tests assuring uniqueness of all types, but it's not in 0.9.2... which means I'll have to release 0.9.3 really soon.
from graphql-spqr.
@kaqqao Thanks a lot for your fix and your insight.
Oh so i won't bother with Edge, relayId and GraphQLType since it does not fit my need.
I guess what was misleading to me was the exception i had with my enum Gender and the bug i encountered when using GraphQLContext.
I'll try it out without the enum type as to be sure i can understand how injection works.
I can wait to put back the enum once the 0.9.3 comes out.
from graphql-spqr.
I think i figured it out. I was on the right track when using GraphQLContext but i wanted to use it on an object that triggered the enum type exception bug which led me to think i was not using it properly.
Oh well :)
In the end it is really simple and now that i get it to work, your product is really nice to quickly get a graphql product started.
I have a simple question about optimizing my queries.
If the graphql query contains only the fields "id" and "name" for example, i would rather not do a "SELECT * FROM...."
Is there a way to map field asked in the query and fields for my select statement?
I have put aside my enum for now since i am using it for both input and ouput.
Thanks again
from graphql-spqr.
Good to hear you're back on track! 0.9.3 is out now, so you might want to give it a go.
from graphql-spqr.
I'll close this one as the enum bug has been fixed and I've opened #9 to track the progress of the sub-field injection feature. @depauwjimmy @string01 If you still need this one open, please comment.
@string01 Thanks a bunch for your comment :)
from graphql-spqr.
Related Issues (20)
- Setup GitHub Pages for documentation
- Apply bean validation annotations only if an appropriate group is enabled [Breaking]
- How to use executionResult data HOT 2
- Failing tests - ComplexityTest class
- Explicitly register Java classes to the GraphQL schema. HOT 4
- How to map a java class to GraphQL interface without using @GraphQLInterface annotation? HOT 5
- Best way to error handling HOT 1
- Help in Handling Exceptions with SPQR HOT 1
- Question: Extending an InputObject type and handle it in SPQR HOT 3
- SDL Printing Fails on Schema Directives with Arguments HOT 4
- Spring 3.2: Cannot resolve parameter names for constructor public io.leangen.graphql.spqr.spring.web.dto.GraphQLRequest(java.lang.String,java.lang.String,java.lang.String,java.util.Map) HOT 2
- Compile parameters to support Spring Boot 3.2.1 (Spring Framework 6.1) HOT 1
- null values sent in response json HOT 1
- Schema Generation Regression in 0.12.4 HOT 1
- Polymorphism type on input
- Scalars containing an Instant cannot be serialized over subscription
- Schema generation at compile time?
- BUG: The DelegatingTypeResolver breaks when updating the GraphQLSchema using a Visitor HOT 1
- Feature Request: Add support for GraphQLOutputType to the SchemaTransformer HOT 39
- Make a public OperationMapper#createResolver overload to help with creating custom field resolvers
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-spqr.