How to get the generated scheme file .graphqls using SPQR?
Asked Answered
H

2

5

I really like the way SPQR makes easy to integrate with an existing system, the only thing I'd like to see is the .graphqls file so I can learn more on GraphQL Syntax.

Is there a way to generate the scheme file from an existing code with SPQR annotations integrated?

To provide some code let's use the same code from the GitHub site

Entity:

public class User {

    private String name;
    private Integer id;
    private Date registrationDate;

    @GraphQLQuery(name = "name", description = "A person's name")
    public String getName() {
        return name;
    }

    @GraphQLQuery
    public Integer getId() {
        return id;
    }

    @GraphQLQuery(name = "regDate", description = "Date of registration")
    public Date getRegistrationDate() {
        return registrationDate;
    }
}

Service class:

class UserService {

    @GraphQLQuery(name = "user")
    public User getById(@GraphQLArgument(name = "id") Integer id) {
      ...
    }
}

Expected Output:

type User {
    id: Int!
    name: String!
    registrationDate: String
}

Query{
 user(Int):User 
}
Hemialgia answered 29/8, 2018 at 21:47 Comment(0)
T
9

This is not really SPQR specific. All the needed classes are provided by graphql-java itself:

new SchemaPrinter().print(schema);

To get a more detailed output (with scalars, introspection types etc), provide the options to the printer:

new SchemaPrinter(
                  // Tweak the options accordingly
                  SchemaPrinter.Options.defaultOptions()
                          .includeScalarTypes(true)
                          .includeExtendedScalarTypes(true)
                          .includeIntrospectionTypes(true)
                          .includeSchemaDefintion(true)
            ).print(schema);
Trend answered 4/9, 2018 at 16:49 Comment(2)
I'm using spqr-spring-boot-starter library. Could you please help me how to get an instance for schema in your above code snippet?Steffy
@Steffy You should be able to simply inject it.Trend
B
0

Adding graphql.spqr.gui.enabled=true in the application properties enables the gui(playground). The schema and documentation can be explored and downloaded from the GUI.

Belligerence answered 3/8, 2021 at 3:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.