我正在尝试使GraphQL Java 服务器与 GraphiQL 服务器一起工作。
使用在本地运行的 GraphiQL,我提交了一个带有以下参数的查询:
我的 Spring 控制器(从此处复制)如下所示:
@Controller
@EnableAutoConfiguration
public class MavenController {
private final MavenSchema schema = new MavenSchema();
private final GraphQL graphql = new GraphQL(schema.getSchema());
private static final Logger log = LoggerFactory.getLogger(MavenController.class);
@RequestMapping(value = "/graphql", method = RequestMethod.OPTIONS, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object executeOperation(@RequestBody Map body) {
log.error("body: " + body);
final String query = (String) body.get("query");
final Map<String, Object> variables = (Map<String, Object>) body.get("variables");
final ExecutionResult executionResult = graphql.execute(query, (Object) null, variables);
final Map<String, Object> result = new LinkedHashMap<>();
if (executionResult.getErrors().size() > 0) {
result.put("errors", executionResult.getErrors());
log.error("Errors: {}", executionResult.getErrors());
}
log.error("data: " + executionResult.getData());
result.put("data", executionResult.getData());
return result;
}
}
理论上,executeOperation
应该在我在 GraphiQL 中提交查询时调用。不是,我在控制台输出中看不到日志语句。
我在做什么错?MavenController.executeOperation
当在 GraphiQL 中提交查询时,如何确保调用 ?
更新 1(13.01.2017 13:35 MSK):这是有关如何重现错误的教程。我的目标是创建一个基于 Java 的 GraphQL 服务器,我可以使用 GraphiQL 与之交互。如果可能的话,这应该在本地工作。
我已经读过,为了做到这一点,以下步骤是必要的:
- 设置 GraphQL 服务器。这方面的一个例子可以在这里找到https://github.com/graphql-java/todomvc-relay-java。该示例使用 Spring Boot,但您可以使用任何您喜欢的 HTTP 服务器来实现它。
- 设置 GraphiQL 服务器。这有点超出了这个项目的范围,但基本上你需要在上面的步骤 1 中让 GraphiQL 与服务器对话。它将使用自省来加载模式。
我检查了项目todomvc-relay-java,根据我的需要对其进行了修改并将其放入目录E:\graphiql-java\graphql-server
中。您可以在此处下载包含该目录的存档。
第 1 步:安装 Node.JS
第2步
去那里E:\graphiql-java\graphql-server\app
跑npm install
。
第 3 步
npm start
从同一目录运行。
第4步
去那里E:\graphiql-java\graphql-server
跑gradlew start
。
第 5 步
运行docker run -p 8888:8080 -d -e GRAPHQL_SERVER=http://localhost:8080/graphql merapar/graphql-browser-docker
。
Docker 来源:graphql-browser-docker
第 6 步
在禁用 XSS 检查的情况下启动 Chrome,例如"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --args --disable-xss-auditor
. 一些消息来源声称您必须杀死所有其他 Chrome 实例才能使这些参数生效。
第 7 步
在该浏览器中打开http://localhost:8888/ 。
第 8 步
尝试运行查询
{
allArtifacts(group: "com.graphql-java", name: "graphql-java") {
group
name
version
}
}
实际结果:
1) Chrome控制台Fetch API cannot load http://localhost:8080/graphql. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access. The response had HTTP status code 403. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
选项卡中的错误: .
2) Chrome网络选项卡中的错误:
更新 2 (14.01.2017 13:51):目前,CORS 在 Java 应用程序中的配置方式如下。
主类:
@SpringBootApplication
public class Main {
public static void main(String[] args) throws Exception {
SpringApplication.run(Main.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedMethods("OPTIONS")
.allowedOrigins("*")
.allowedHeaders(
"Access-Control-Request-Headers",
"Access-Control-Request-Method",
"Host",
"Connection",
"Origin",
"User-Agent",
"Accept",
"Referer",
"Accept-Encoding",
"Accept-Language",
"Access-Control-Allow-Origin"
)
.allowCredentials(true)
;
}
};
}
}
WebSecurityConfigurerAdapter
子类:
@Configuration
@EnableWebSecurity
public class SpringWebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
System.out.println("SpringWebSecurityConfiguration");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("OPTIONS");
source.registerCorsConfiguration("/**", config);
http
.addFilterBefore(new CorsFilter(source), ChannelProcessingFilter.class)
.httpBasic()
.disable()
.authorizeRequests()
.anyRequest()
.permitAll()
.and()
.csrf()
.disable();
}
@Override
public void configure(WebSecurity web) throws Exception {
}
}
控制器:
@Controller
@EnableAutoConfiguration
public class MavenController {
private final MavenSchema schema = new MavenSchema();
private final GraphQL graphql = new GraphQL(schema.getSchema());
private static final Logger log = LoggerFactory.getLogger(MavenController.class);
@CrossOrigin(
origins = {"http://localhost:8888", "*"},
methods = {RequestMethod.OPTIONS},
allowedHeaders = {"Access-Control-Request-Headers",
"Access-Control-Request-Method",
"Host",
"Connection",
"Origin",
"User-Agent",
"Accept",
"Referer",
"Accept-Encoding",
"Accept-Language",
"Access-Control-Allow-Origin"},
exposedHeaders = "Access-Control-Allow-Origin"
)
@RequestMapping(value = "/graphql", method = RequestMethod.OPTIONS, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Object executeOperation(@RequestBody Map body) {
[...]
}
}
MavenController.executeOperation
是当我在 GraphiQL 中发出查询请求时应该调用的方法。
更新3(15.01.2017 22:05):尝试使用CORSFilter
,新的源代码在这里。没有结果,我仍然收到“无效的 CORS 响应”错误。