1

这是使用一个相当复杂的实体集合,主要由 JPA 实体组成,但也包括从 API 获取的代理和实体。我只是真正注释了我试图用@GraphQLQuery注释获取的基类,但它进入了下面的层次结构,并且在某些时候找到了一个HashMap<String, String[]>带有硬编码内容的静态属性。(我学会了不要问......)此时它抛出了这个异常并且一切都停止了:

graphql.AssertException: Name must be non-null, non-empty and match [_A-Za-z][_0-9A-Za-z]* - was 'Map_String_String[]Scalar'
at graphql.Assert.assertValidName(Assert.java:58)
at graphql.schema.GraphQLScalarType.<init>(GraphQLScalarType.java:50)
at graphql.schema.GraphQLScalarType.<init>(GraphQLScalarType.java:45)
at io.leangen.graphql.util.Scalars.graphQLMapScalar(Scalars.java:325)
at io.leangen.graphql.generator.mapping.common.ObjectScalarAdapter.toGraphQLType(ObjectScalarAdapter.java:20)
at io.leangen.graphql.generator.mapping.common.ObjectScalarAdapter.toGraphQLType(ObjectScalarAdapter.java:16)
at io.leangen.graphql.generator.mapping.common.CachingMapper.toGraphQLType(CachingMapper.java:30)
at io.leangen.graphql.generator.OperationMapper.toGraphQLType(OperationMapper.java:179)
at io.leangen.graphql.generator.OperationMapper.toGraphQLType(OperationMapper.java:165)
at io.leangen.graphql.generator.OperationMapper.toGraphQLField(OperationMapper.java:138)
at io.leangen.graphql.generator.mapping.common.ObjectTypeMapper.lambda$getFields$3(ObjectTypeMapper.java:88)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1625)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at io.leangen.graphql.generator.mapping.common.ObjectTypeMapper.getFields(ObjectTypeMapper.java:89)
at io.leangen.graphql.generator.mapping.common.ObjectTypeMapper.toGraphQLType(ObjectTypeMapper.java:41)
at io.leangen.graphql.generator.mapping.common.ObjectTypeMapper.toGraphQLType(ObjectTypeMapper.java:33)
at io.leangen.graphql.generator.mapping.common.CachingMapper.toGraphQLType(CachingMapper.java:30)
at io.leangen.graphql.generator.OperationMapper.toGraphQLType(OperationMapper.java:179)
at io.leangen.graphql.generator.OperationMapper.toGraphQLType(OperationMapper.java:165)
at io.leangen.graphql.generator.OperationMapper.toGraphQLField(OperationMapper.java:138)
at io.leangen.graphql.generator.mapping.common.ObjectTypeMapper.lambda$getFields$3(ObjectTypeMapper.java:88)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.HashMap$ValueSpliterator.forEachRemaining(HashMap.java:1625)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at io.leangen.graphql.generator.mapping.common.ObjectTypeMapper.getFields(ObjectTypeMapper.java:89)
at io.leangen.graphql.generator.mapping.common.ObjectTypeMapper.toGraphQLType(ObjectTypeMapper.java:41)
at io.leangen.graphql.generator.mapping.common.ObjectTypeMapper.toGraphQLType(ObjectTypeMapper.java:33)
at io.leangen.graphql.generator.mapping.common.CachingMapper.toGraphQLType(CachingMapper.java:30)
at io.leangen.graphql.generator.OperationMapper.toGraphQLType(OperationMapper.java:179)
at io.leangen.graphql.generator.OperationMapper.toGraphQLType(OperationMapper.java:165)
at io.leangen.graphql.generator.OperationMapper.toGraphQLField(OperationMapper.java:138)
at io.leangen.graphql.generator.OperationMapper.lambda$generateQueries$0(OperationMapper.java:91)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at io.leangen.graphql.generator.OperationMapper.generateQueries(OperationMapper.java:92)
at io.leangen.graphql.generator.OperationMapper.<init>(OperationMapper.java:75)
at io.leangen.graphql.GraphQLSchemaGenerator.generate(GraphQLSchemaGenerator.java:868)
at com.ist.exam.graphql.services.GraphQLService.init(GraphQLService.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
... 39 more

我不知道这个名称是如何生成的,但它以“Map_String_String[]Scalar”的形式出现,然后在 assertValidName 上抛出。

我会用 注释该属性@GraphQLQuery(name="somethingClever"),但此时我们位于整个公司共享的库中,此处所做的更改必须通过更改请求和其他内容。

我试图用 排除这个代理包withBasePackages,但这似乎没有效果。这是尝试创建架构的方法:

GraphQLSchema schema = new GraphQLSchemaGenerator()
                    .withBasePackages("entities")
                    .withResolverBuilders(new AnnotatedResolverBuilder())
                    .withOperationsFromSingleton(examService)
                    .withValueMapperFactory(new JacksonValueMapperFactory())
                    .generate();
            graphQL = GraphQL.newGraphQL(schema).build();

有没有人见过这些问题中的任何一个?我可以将包裹列入黑名单而不是将其列入白名单吗?例如withBasePackages("!proxies")

4

1 回答 1

2

这是一个错误,因为不应生成无效名称。我将立即为 GraphQL SPQR 的下一个版本修复此问题(在撰写本文时,当前版本为 0.9.9)。

至于它最终是如何成为的Map_String_String[]Scalar,它相当涉及......地图非常棘手,因为在 GraphQL 中没有什么能比得上地图。这些选项基本上是将映射视为键入的键值对列表或只是作为未知的动态结构(所谓的JSON 标量)。SPQR 支持这两种方法,但默认情况下将地图视为复杂 (JSON) 标量。为了保持对内部内容的一些了解,它将为每个底层 Java 类型生成一个不同命名的标量。在这样做时,它必须为每个生成一个唯一的名称。在您的情况下,它会发现 aMap<String, String[]>会生成您看到的无效名称 - Map_String_String[]Scalar

您可以通过注册一个可以捕获这种情况的自定义或所有复杂的标量来轻松解决此问题TypeMapper,并为它们提供所有相同的名称而不是唯一的名称。

例如

public class CustomObjectScalarMapper extends ObjectScalarMapper {
    private static final String OBJECT_SCALAR_NAME = "ObjectScalar";

    @Override
    protected String getTypeName(AnnotatedType type, BuildContext buildContext) {
        return OBJECT_SCALAR_NAME;
    }

    @Override
    protected String getInputTypeName(AnnotatedType type, BuildContext buildContext) {
        return OBJECT_SCALAR_NAME;
    }
}

并替换现有的ObjectScalarMapper通过:

generator.withTypeMappers((conf, current) -> current.replace(ObjectScalarMapper.class, new CustomObjectScalarMapper()))

有了这个,所有复杂的标量都将被调用ObjectScalar,你就不会有问题了。

如果您希望更精细,也可以覆盖该supports方法。


一个小小的说明,您的生成器配置不必要地将一堆东西设置为其默认值。

GraphQLSchema schema = new GraphQLSchemaGenerator()
                    .withBasePackages("entities")
                    .withResolverBuilders(new AnnotatedResolverBuilder())
                    .withOperationsFromSingleton(examService)
                    .withValueMapperFactory(new JacksonValueMapperFactory())
                    .generate();

相当于

GraphQLSchema schema = new GraphQLSchemaGenerator()
                    .withBasePackages("entities")
                    .withOperationsFromSingleton(examService)
                    .generate();
于 2019-01-02T12:57:06.120 回答