3

I want to introduce a scalar type to the graphql scheme and provide resolver for it.

I believe what I need to do is to provide the corresponding resolver for the new type as described here

I use spring boot starter to implement the server. com.graphql-java:graphql-spring-boot-starter:5.0.2

There is schema.graphqls:

type Query {
    pets(last: Int): [Pet]
}
type Pet {
    id: ID
    name: String
}

and QueryResolver:

@Component
public class Query implements GraphQLQueryResolver {

    @Autowired
    PetStore petStore;

    public Pet pet(long id) {
        return petStore.pet(id);
    }

    public List<Pet> pets(Integer last) {
        return petStore.pets().stream()
                .limit(last != null ? last : Integer.MAX_VALUE)
                .collect(Collectors.toList());
    }
}

I believe that there is a way to update a scheme like this:

type Pet {
    id: ID
    name: String
    dateOfBirth: LocalDateTime
}

scalar LocalDateTime

and provide resolver to define how new field value should be processed, like this:

@Component
public class DateResolver extends GraphQLScalarType {

    // serialize/deserialize logic

}

Unfochinatly I get following exception:

com.coxautodev.graphql.tools.SchemaClassScannerError: Expected a user-defined GraphQL scalar type with name 'LocalDateTime' but found none!
Dzmitry Atkayey
  • 332
  • 5
  • 12

2 Answers2

0

After some research, I found resolvers for the java types in the graphql-java-9.2.jar!/graphql/Scalars.class and inspired of it.

Dzmitry Atkayey
  • 332
  • 5
  • 12
  • the more obvious source would be the documentation: https://www.graphql-java.com/documentation/v12/scalars/ – UninformedUser Aug 16 '19 at 13:29
  • it makes also sense to use latest version `13.0`: https://mvnrepository.com/artifact/com.graphql-java/graphql-java/13.0 – UninformedUser Aug 16 '19 at 13:31
  • It is a transitive dependency in my case, I use the latest `graphql-spring-boot-starter` – Dzmitry Atkayey Aug 16 '19 at 15:55
  • I don't think so - latest version of `graphql-spring-boot-starter` is `5.10.0` which in fact imports `graphql-java` with version `13.0` transitively. – UninformedUser Aug 16 '19 at 16:54
  • Sorry, I'd not specified that I'm using `com.graphql-java:graphql-spring-boot-starter` from maven central, there is 5.0.2 is latest. https://mvnrepository.com/artifact/com.graphql-java/graphql-spring-boot-starter Can you recommend me where is 5.10.0 is available? – Dzmitry Atkayey Aug 18 '19 at 12:36
  • I'd go with this https://github.com/graphql-java-kickstart/graphql-spring-boot - as mentioned in the bottom of the page, add the JCenter repository to your POM file in order to get faster releases, but it's also contained in public Maven repo: https://mvnrepository.com/artifact/com.graphql-java-kickstart/graphql-spring-boot-starter/5.10.0 – UninformedUser Aug 18 '19 at 13:10
  • appreciate your advice – Dzmitry Atkayey Aug 19 '19 at 07:38
0

Not sure what your setup looks like but for me this is how I quickly resolved that exception:

I extended the GraphQlHttpServlet and there is where I load my graphql schema (overriding the getConfiguration() method etc to start the servlet... sorry I can't go into much detail but I'm sure looking at the docs will make what I just said make sense). In getConfiguration() I have:

return GraphQLConfiguration.with(createSchema())
        .with(customGraphQLContextBuilder)
        .build();

and in createSchema I load the graphql schema and I PARSE it:

.
.
            return SchemaParser.newParser()
                    .schemaString(schemaString)
                    .scalars(CustomDateTimeScalar.getDateTimeScalar())
                    .resolvers(myQueryResolverXYZ)
                    .build()
                    .makeExecutableSchema();
.
.

Just in case this sheds light on someone else that runs into this.