【问题标题】:How to implement Custom Scalar in Graphql-java?如何在 Graphql-java 中实现自定义标量?
【发布时间】:2018-12-04 21:23:31
【问题描述】:

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

作者:com.coxautodev.graphql.tools.SchemaClassScannerError:预期的 用户定义的名为“电子邮件”的 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')      

请帮忙...

【问题讨论】:

标签: graphql


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2018-02-12
    • 2020-08-03
    • 2022-08-05
    • 2020-08-03
    • 2018-05-29
    • 2020-10-21
    • 2016-10-06
    • 2010-11-17
    • 2020-11-23
    相关资源
    最近更新 更多