I am also facing some issue while hitting Graphql APIs through postman.
http://testEnvrironment/restcall/getReports
POST body
getReports(id:15){
isApproved
}
}
Entries of sample.graphqls file is
schema{
query: Query
}
type Query{
getReports(id: Long): PublishedReportInternal
}
type PublishedReportInternal{
sourceReport:ObjectRef
isApproved:Boolean
ignoreOutputFormatId:Boolean
}
type ObjectRef{
id : Long
qualifier : String
}
@Override
public ResponseEntity<Object> getReports(String query) {
ExecutionResult execute = graphQLService.getGraphQL().execute(query);
System.out.println("query::"+query);
System.out.println("execute result...."+execute);
return new ResponseEntity<>(execute, HttpStatus.OK);
}
@Service
public class CustomDataFetcher implements DataFetcher<PublishedReportInternal> {
@Inject
PublishRunReportInternalRestService service;
@Override
public PublishedReportInternal get(DataFetchingEnvironment environment) {
System.out.println("Entered into DataFetcher class...");
String reportId=environment.getArgument("id");
System.out.println("reportId is ::"+reportId);
if(!StringUtils.isEmpty(reportId)) {
return service.getReportById(new Long(reportId));
}
else {
return null;
}
}
}
Below is the implementation of GraphQLService
@Service
public class GraphQLService {
@Value("classpath:sample.graphqls")
private Resource schemaResource;
private GraphQL graphQL;
@Inject
private CustomDataFetcher customDataFetcher;
@PostConstruct
private void loadSchema() throws IOException {
// get the schema
File schemaFile = schemaResource.getFile();
System.out.println(schemaFile.getAbsolutePath());
// parse schema
TypeDefinitionRegistry typeRegistry = new SchemaParser().parse(schemaFile);
RuntimeWiring wiring = buildRuntimeWiring();
GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(typeRegistry, wiring);
graphQL = GraphQL.newGraphQL(schema).build();
}
private RuntimeWiring buildRuntimeWiring() {
return RuntimeWiring.newRuntimeWiring().type("Query", typeWiring -> typeWiring
.dataFetcher("getReports", customDataFetcher)).build();
}
public GraphQL getGraphQL() {
return graphQL;
}
}
Rest endpoint will be
@POST
@Path(value = "/getReports")
@Consumes
(value = MediaType.APPLICATION_JSON)
@Produces
(value = MediaType.APPLICATION_JSON) ResponseEntity<Object> getReports( String query);
Please try to help here as I am stuck completely with this problem.
Following jar has been used by me for this POC
compile files('libs/new/graphiql-spring-boot-starter-5.0.2.jar')
compile files('libs/new/graphql-java-9.2.jar')
compile files('libs/new/graphql-java-servlet-6.1.2.jar')
compile files('libs/new/graphql-java-tools-5.2.4.jar')
compile files('libs/new/graphql-spring-boot-autoconfigure-5.0.2.jar')
compile files('libs/new/graphql-spring-boot-starter-5.0.2.jar')
compile files('libs/new/java-dataloader-2.0.2.jar')
compile files('libs/new/antlr4-runtime-4.7.1.jar')
compile files('libs/new/reactive-streams-1.0.3.jar')
Above code snippet contains all the information which I have used to perform this POC, but while performing this POC I am consistently getting "Invalid Syntax" error. Please help in resolving this issue.
Regards Kushagra