GithubHelp home page GithubHelp logo

Subquery/Edges/... about graphql-spqr HOT 11 CLOSED

leangen avatar leangen commented on May 17, 2024
Subquery/Edges/...

from graphql-spqr.

Comments (11)

kaqqao avatar kaqqao commented on May 17, 2024 1

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.

kaqqao avatar kaqqao commented on May 17, 2024

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.

kaqqao avatar kaqqao commented on May 17, 2024

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.

depauwjimmy avatar depauwjimmy commented on May 17, 2024

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.

kaqqao avatar kaqqao commented on May 17, 2024

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.

string01 avatar string01 commented on May 17, 2024

from graphql-spqr.

kaqqao avatar kaqqao commented on May 17, 2024

@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.

depauwjimmy avatar depauwjimmy commented on May 17, 2024

@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.

depauwjimmy avatar depauwjimmy commented on May 17, 2024

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.

kaqqao avatar kaqqao commented on May 17, 2024

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.

kaqqao avatar kaqqao commented on May 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.