如何在Graphql-java中实现自定义标量?

时间:2018-06-26 08:16:47

标签: graphql

我是graphql的新手。我正在尝试实现自定义标量类型“电子邮件”。但正在低于错误。你能帮我吗?

  

作者:com.coxautodev.graphql.tools.SchemaClassScannerError:预期   用户定义的名称为“ Email”的GraphQL标量类型,但未找到!在   com.coxautodev.graphql.tools.SchemaClassScanner.validateAndCreateResult(SchemaClassScanner.kt:144)   〜[graphql-java-tools-4.3.0.jar:na]在   com.coxautodev.graphql.tools.SchemaClassScanner.scanForClasses(SchemaClassScanner.kt:94)   〜[graphql-java-tools-4.3.0.jar:na]

配置:

scalar Email

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

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

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

版本信息:

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')      

请帮助...

1 个答案:

答案 0 :(得分:0)

尝试一下:

@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());
            }
        });
    }
}