7

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

Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
KiYo
  • 71
  • 1
  • 3
  • You must define `Email` scalar type with a resolve function. https://stackoverflow.com/questions/46034801/custom-scalar-in-graphql-java – sametcodes Jun 26 '18 at 11:48
  • we are using schema first approach not code first approach. – KiYo Jun 26 '18 at 17:38

1 Answers1

2

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());
            }
        });
    }
}
F. Labusch
  • 21
  • 2