leangen / graphql-spqr

Build a GraphQL service in seconds
Apache License 2.0
1.09k stars 179 forks source link

Default & Required Argument Examples #422

Closed austinarbor closed 1 year ago

austinarbor commented 2 years ago

Hello, Can you please add examples of how to make an input argument required and how to specify a default value for them? The documentation has a tip

The @Argument annotation does not have a "required" flag, nor the option to specify a default value. Both of these can be specified at the GraphQL schema level and are enforced by GraphQL Java.

However I can't find any further documentation or examples in the repo or in the graphql-java guides on how to do this programmatically.

Ideally we'd just be "decorating" the existing schema rather than having to specify the entire schema via file

kaqqao commented 1 year ago

Not really sure where you got the above quote from, but it definitely was not from SPQR. In fact, there is no such annotation as @Argument in SPQR. I think you're confusing Spring GraphQL with SPQR's SpringBoot Starter.

Anyway, in SPQR, there are multiple ways to customize everything about the generated schema, but here's the most direct approach:

E.g.

@GraphQLQuery
// BookId is be required
public Book findBook(@GraphQLNonNull BookId id) {
        return ...;
}

Or

@GraphQLQuery
// Non-null list of non-null IDs is required
public List<Book> findBooks(@GraphQLNonNull List<@GraphQLNonNull BookId> id) {
        return ...;
}

Or

@GraphQLQuery
// Default value specified
public List<Book> findByTag(@GraphQLArgument(name = "tag", defaultValue = "popular") String tag) {
        return ...;
}

Or

@GraphQLQuery
// The given default value is an object
public Book echo(@GraphQLArgument(name = "in", defaultValue = "{\"title\":\"Funky Monkey\"}") Book in) {
        return in;
}
StringKe commented 1 year ago

@kaqqao @GraphQLNonNull cannot be used with @GraphQLArgument, which loses the name and description of the field.

austinarbor commented 1 year ago

@kaqqao you're right, I meant to open this ticket on the spring-graphql project and I have no idea how I opened it up on this project instead, sorry about that! Thanks for answering anyways!

@StringKe I haven't had any problem using @GraphQLNonNull together with @GraphQLArgument in multiple projects

kaqqao commented 1 year ago

@kaqqao @GraphQLNonNull cannot be used with @GraphQLArgument, which loses the name and description of the field.

No clue what you mean. You can definitely use those annotations together. But whenever someone is complaining about annotations not working or other bizarre behavior, it's because of Lombok.

StringKe commented 1 year ago

@kaqqao

https://github.com/leangen/graphql-spqr/blob/master/src/main/java/io/leangen/graphql/annotations/GraphQLNonNull.java#L9

When I look at the source code the setting here is not ElementType.PARATMER.

When I give @GraphQLArgument(description="test") @ElementType java.lang.String name it doesn't work.

import io.leangen.graphql.annotations.GraphQLSubscription;
import io.leangen.graphql.annotations.GraphQLMutation;
import io.leangen.graphql.annotations.GraphQLQuery;
import io.leangen.graphql.annotations.GraphQLArgument;
import io.leangen.graphql.annotations.GraphQLNonNull;

public class GenerateResult {
    @GraphQLQuery(name = "user")
    public com.txys.service.system.pojo.vo.SystemUserVO method0currentSystemUserInfo(
    ) {
        return null;
    }

    @GraphQLMutation(name = "logout")
    public java.lang.Boolean method1logout(
    ) {
        return null;
    }

    @GraphQLMutation(name = "login")
    public com.txys.service.system.pojo.vo.SystemUserVO method2login(
        @GraphQLArgument(name = "username", description = "username") java.lang.String username, 
        @GraphQLArgument(name = "password", description = "password") java.lang.String password
    ) {
        return null;
    }

    @GraphQLQuery(name = "testPermission")
    public java.lang.String method3testPermission(
        @GraphQLArgument(name = "checkLogin", description = "checkLogin") java.lang.Boolean checkLogin, 
        @GraphQLArgument(name = "checkRole", description = "checkRole") java.lang.Boolean checkRole, 
        @GraphQLArgument(name = "checkPermission", description = "checkPermission") java.lang.Boolean checkPermission
    ) {
        return null;
    }

    @GraphQLMutation(name = "kickSystemUser")
    public java.lang.Boolean method4kickSystemUser(
        @GraphQLArgument(name = "account", description = "account") java.lang.String account, 
        @GraphQLArgument(name = "device", description = "device") java.lang.String device
    ) {
        return null;
    }

    @GraphQLMutation(name = "addSystemUser")
    public com.txys.service.system.pojo.vo.SystemUserVO method5addSystemUser(
        @GraphQLArgument(name = "dto", description = "dto") com.txys.service.system.pojo.dto.SystemUserDTO dto
    ) {
        return null;
    }

    @GraphQLQuery(name = "findSystemUser")
    public org.springframework.data.domain.Page<com.txys.service.system.pojo.vo.SystemUserVO> method6searchSystemUser(
        @GraphQLArgument(name = "username", description = "username") java.lang.String username, 
        @GraphQLArgument(name = "searchRequest", description = "searchRequest") com.txys.commom.core.jpa.SearchRequest searchRequest
    ) {
        return null;
    }

    @GraphQLMutation(name = "addSystemMenu")
    public com.txys.service.system.pojo.vo.SystemMenuVO method7addSystemUser(
        @GraphQLArgument(name = "dto", description = "dto") com.txys.service.system.pojo.dto.SystemMenuDTO dto
    ) {
        return null;
    }

    @GraphQLQuery(name = "findSystemMenu")
    public org.springframework.data.domain.Page<com.txys.service.system.pojo.vo.SystemMenuVO> method8searchSystemUser(
        @GraphQLArgument(name = "searchRequest", description = "searchRequest") com.txys.commom.core.jpa.SearchRequest searchRequest
    ) {
        return null;
    }

}

If I write import statements for all types there is no problem.