KiYo
KiYo

Reputation: 71

How to implement Custom Scalar in Graphql-java?

I am new to graphql. I am trying to implement custom scalar type "Email". but am getting below error. Could you please help me?

by: com.coxautodev.graphql.tools.SchemaClassScannerError: Expected a user-defined GraphQL scalar type with name 'Email' but found none! at com.coxautodev.graphql.tools.SchemaClassScanner.validateAndCreateResult(SchemaClassScanner.kt:144) ~[graphql-java-tools-4.3.0.jar:na] at com.coxautodev.graphql.tools.SchemaClassScanner.scanForClasses(SchemaClassScanner.kt:94) ~[graphql-java-tools-4.3.0.jar:na]

Configurations :

scalar Email

type Greeting {
  id: ID!
  message: String!
  email:Email
}

type Query {
  getGreeting(id: ID!): Greeting
}

type Mutation {
  newGreeting(message: String!): Greeting!
}

Version info:

springBootVersion = '1.5.8.RELEASE'

com.graphql-java:graphql-java:6.0')

com.graphql-java:graphql-java-tools:4.3.0')

com.graphql-java:graphql-java-servlet:4.7.0')

org.springframework.boot:spring-boot-starter-web')

com.graphql-java:graphql-spring-boot-starter:3.10.0')

com.graphql-java:graphiql-spring-boot-starter:3.10.0')      

Please help...

Upvotes: 7

Views: 5044

Answers (1)

F. Labusch
F. Labusch

Reputation: 21

Try this:

@Component
public class ScalarEMail extends GraphQLScalarType {
    public ScalarEMail() {
        super("Email", "Scalar Email", new Coercing() {
            @Override
            public Object serialize(Object o) throws CoercingSerializeException {
                return ((Email) o).getEmail();
            }

            @Override
            public Object parseValue(Object o) throws CoercingParseValueException {
                return serialize(o);
            }

            @Override
            public Object parseLiteral(Object o) throws CoercingParseLiteralException {
                return Email.valueOf(((StringValue) o).getValue());
            }
        });
    }
}

Upvotes: 2

Related Questions