- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个 GraphQL Spring Boot 应用程序以在现有 REST Web API 之上创建一个 GraphQL 层,但我在处理架构中的 HashMap 时遇到了问题。
Timeline
类有一个名为 dataReference
的字段,它是一个 HashMap。我尝试在 graphql 模式中将其定义为 DataReference
类型的列表,这是一个键/值对,但我收到以下错误:
Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
at com.foo.bar.graphql.AppLauncher.main(AppLauncher.java:43)
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:125)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.<init>(TomcatWebServer.java:86)
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getTomcatWebServer(TomcatServletWebServerFactory.java:414)
at org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory.getWebServer(TomcatServletWebServerFactory.java:174)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152)
... 8 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletRegistrationBean' defined in com.foo.bar.graphql.AppLauncher: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 'servletRegistrationBean' threw exception; nested exception is com.coxautodev.graphql.tools.SchemaClassScannerError: Unable to match type definition (ListType{type=TypeName{name='DataReference'}}) with java type (java.util.Map<java.lang.String, java.lang.String>): Java class is not a List or generic type information was lost: java.util.Map<java.lang.String, java.lang.String>
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:625)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:455)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:236)
at org.springframework.boot.web.servlet.ServletContextInitializerBeans.getOrderedBeansOfType(ServletContextInitializerBeans.java:224)
at org.springframework.boot.web.servlet.ServletContextInitializerBeans.addServletContextInitializerBeans(ServletContextInitializerBeans.java:100)
at org.springframework.boot.web.servlet.ServletContextInitializerBeans.<init>(ServletContextInitializerBeans.java:88)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getServletContextInitializerBeans(ServletWebServerApplicationContext.java:250)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.selfInitialize(ServletWebServerApplicationContext.java:237)
at org.springframework.boot.web.embedded.tomcat.TomcatStarter.onStartup(TomcatStarter.java:54)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5098)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:839)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1429)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:134)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:944)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:261)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:422)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:770)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at org.apache.catalina.startup.Tomcat.start(Tomcat.java:370)
at org.springframework.boot.web.embedded.tomcat.TomcatWebServer.initialize(TomcatWebServer.java:106)
... 13 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.ServletRegistrationBean]: Factory method 'servletRegistrationBean' threw exception; nested exception is com.coxautodev.graphql.tools.SchemaClassScannerError: Unable to match type definition (ListType{type=TypeName{name='DataReference'}}) with java type (java.util.Map<java.lang.String, java.lang.String>): Java class is not a List or generic type information was lost: java.util.Map<java.lang.String, java.lang.String>
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:620)
... 55 more
Caused by: com.coxautodev.graphql.tools.SchemaClassScannerError: Unable to match type definition (ListType{type=TypeName{name='DataReference'}}) with java type (java.util.Map<java.lang.String, java.lang.String>): Java class is not a List or generic type information was lost: java.util.Map<java.lang.String, java.lang.String>
at com.coxautodev.graphql.tools.TypeClassMatcher.error(TypeClassMatcher.kt:22)
at com.coxautodev.graphql.tools.TypeClassMatcher.match(TypeClassMatcher.kt:65)
at com.coxautodev.graphql.tools.TypeClassMatcher.match(TypeClassMatcher.kt:28)
at com.coxautodev.graphql.tools.SchemaClassScanner.scanResolverInfoForPotentialMatches(SchemaClassScanner.kt:215)
at com.coxautodev.graphql.tools.SchemaClassScanner.scanQueueItemForPotentialMatches(SchemaClassScanner.kt:206)
at com.coxautodev.graphql.tools.SchemaClassScanner.scanQueue(SchemaClassScanner.kt:103)
at com.coxautodev.graphql.tools.SchemaClassScanner.scanForClasses(SchemaClassScanner.kt:81)
at com.coxautodev.graphql.tools.SchemaParserBuilder.scan(SchemaParserBuilder.kt:149)
at com.coxautodev.graphql.tools.SchemaParserBuilder.build(SchemaParserBuilder.kt:155)
at com.foo.bar.graphql.AppLauncher.servletRegistrationBean(AppLauncher.java:55)
at com.foo.bar.graphql.AppLauncher$$EnhancerBySpringCGLIB$$1ac15725.CGLIB$servletRegistrationBean$0(<generated>)
at com.foo.bar.graphql.AppLauncher$$EnhancerBySpringCGLIB$$1ac15725$$FastClassBySpringCGLIB$$c7dabbfc.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at com.foo.bar.graphql.AppLauncher$$EnhancerBySpringCGLIB$$1ac15725.servletRegistrationBean(<generated>)
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.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 56 more
这是我的架构文件:
extend type Query {
getFoo: Timeline
}
type Timeline {
dataReference: [DataReference]
}
type DataReference {
key: String
value: String
}
还有我的模型类:
package com.foo.bar.graphql.model;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap;
import java.util.Map;
@Getter @Setter
public class Timeline {
private Map<String, String> dataReference = new HashMap<>();
}
我找到了这个帖子 Return HashMap<String, Object> from GraphQL-Java关于在模式中定义 map 的问题。在接受的答案中,选项 1) 对我来说不可行,因为模型是从现有的 Web API 中获取的并且不能更改。上面列出了我对选项 2 的尝试,但没有奏效。我还尝试了选项 3(使用标量):
架构:
extend type Query {
getFoo: Timeline
}
scalar Object
type Timeline {
dataReference: Object
}
Servlet 注册:
@Bean
public ServletRegistrationBean servletRegistrationBean() {
GraphQLSchema schema = SchemaParser.newParser()
.resolvers(fooResolver)
.file("graphql/foo.graphqls")
.scalars(ExtendedScalars.Object)
.build()
.makeExecutableSchema();
ExecutionStrategy executionStrategy = new AsyncExecutionStrategy();
GraphQLServlet servlet = new SimpleGraphQLServlet(schema, executionStrategy);
ServletRegistrationBean bean = new ServletRegistrationBean(servlet, "/graphql");
return bean;
}
pom.xml:
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-extended-scalars</artifactId>
<version>1.0</version>
</dependency>
但我收到此错误:Caused by: com.coxautodev.graphql.tools.SchemaClassScannerError: Expected a user-defined GraphQL scalar type with name 'Object' but found none!
如果有人可以帮助我实现它,我更喜欢在模式中包含键/值对的方法。
最佳答案
最简单的方法是使用 DTO。使用像 mapstruct 这样的 DTO 映射器例如,将允许您自动映射除 Map 字段之外的所有字段,您将在其中应用转换。
你像这样创建一个 DTO :
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TimelineDTO {
private List<DataReference> dataReference = new ArrayList();
}
获取到TimeLine对象后,将其映射到TimelineDTO,进行Map到List的转换。
List<DataReference> dataReferences = new ArrayList<>();
for (Map.Entry<String,String> entry : super.getDataReference().entrySet()) {
dataReferences.add(new DataReference(entry.getKey(), entry.getValue()));
}
return dataReferences;
在您的 GraphQL 合约中,您改为返回 DTO:
extend type Query {
getFoo: TimelineDTO
}
type TimelineDTO {
dataReference: [DataReference]
}
type DataReference {
key: String
value: String
}
关于java - Java GraphQL 模式中的 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55970945/
下列说法正确的是: Javascript == Typescript Typescript != Javascript 对于 Dgraph 的 GraphQL+ 来说也是如此吗? GraphQL ==
我正在尝试通过使用 .graphql 文件并传递变量来对 Karate 进行测试。在我的 graphql 架构中,我试图重用另一个 .graphql 文件中的片段。我尝试按照 https://www.
从他们的文档中,它说: The leaf values of any request and input values to arguments are Scalars (or Enums) and
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 5 年前。 Improve
有没有一种技术可以让我在 GraphQL 中像这样声明 ADT? // TypeScript type SomeAlgebraicDataType = | { state: 'A', subSta
在基于 graphql API 构建新应用程序时,我们遇到了以下问题: 我们有一个带有输入字段的突变,其类型是具有自己验证规则的自定义标量(在这种情况下,输入是格式正确的电子邮件地址)。 在客户端,应
从语法上讲,您可以在模式中定义查询或变更,使其返回类型。 但是,操作定义(即客户端调用的查询或突变)必须有一个 SelectionSet,所以我必须这样做: mutation X { field }
我希望能听到这里专家的一些意见。我目前正在 NextJS 项目上工作,我的 graphql 正在运行在另一个 repo 中设置的模拟数据上。现在后端由其他开发人员构建,正在慢慢从模拟数据转向真实数据。
Graphql 中的架构和文档有什么区别? 架构是这样的: type Query { fo: String } 但文件是这样的: query SomeQuery { foo { ba
type Person { firstName: String!, lastName: String!, age: Int! } 如何查询所有 18 岁以上的人? 最佳答案 这
有没有办法提供 GraphQL Schema 设计的可视化图表(类似于 UML)? 背景: 我已经有了一个架构设计,要转换成 GraphQL API。但是,在开始 GraphQL 开发之前,我想创建我
我想了解 GraphQL 的(Java)实现是否足够智能,如果在其中一个提取器的执行期间抛出异常,可以取消预定的数据提取? 例如,我运行一个查询来检索客户的所有订单。假设客户有 100 个订单。这意味
我是graphql的新手,但是我在努力查询。 我想通过他们的电子邮件地址返回用户 我有一个类型定义的调用V1User,它具有以下字段 ID, 电子邮件, 密码, 角色 要根据电子邮件返回用户,此查询中
我将GraphQL包装器放在现有的REST API上,如Zero to GraphQL in 30 minutes中所述。我有一个产品的API端点,该端点具有一个指向嵌套对象的属性: // API R
在 GraphQL 中,空格似乎很重要,因为它分隔 token : { human(id: "1000") { name height } } 然而,spec says那个空格
我正在尝试使用带有属性的 sequelize 获取数据并将其传递给 graphql。 结果在控制台中很好,但 graphql 查询为属性字段返回 null。 我的解析器 getUnpayedL
有没有办法在 graphql 查询中生成静态值? 例如,假设我有一个 user具有名称和电子邮件字段的对象。出于某种原因,我总是希望用户的状态为“已接受”。我怎样才能写一个查询来完成这个? 我想做的事
我已关注 the documentation about using graphql-tools to mock a GraphQL server ,但是这会引发自定义类型的错误,例如: Expect
我今天在生产中有以下 graphql 模式定义: type BasketPrice { amount: Int! currency: String! } type BasketItem {
像这样的graphql模式: type User { id: ID! location: Location } type Location { id: ID! user: User }
我是一名优秀的程序员,十分优秀!