0

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

CBroe
  • 91,630
  • 14
  • 92
  • 150
Kushagra Bindal
  • 131
  • 2
  • 8
  • did you try running it through playground? your generated schema should looks like: type Query { getReports(id: Long): PublishedReportInternal } and this can be queried like query Reports($id: Long) { getReports(id: $id) { isAccepted } } – Deeksha Pandit May 11 '21 at 18:12
  • yes as a standalone spring boot application it is working... but when I am trying to integrating it with enterprise application having authentication/authorization logic+ other filters in place and proper DB and cache calls in place then it is giving me above mentioned error. It looks like I am obviously missing something which needs to be taken care here. Please help in understanding the root cause. – Kushagra Bindal May 11 '21 at 18:16
  • try wrapping your query part with query {} ________________________________________ query { getReports(id:15){ isApproved } } – Deeksha Pandit May 11 '21 at 18:18
  • I will try to put "query" before { and will update you. – Kushagra Bindal May 11 '21 at 18:29
  • meanwhile I tried to implement 1 simple one without any input type Query{reportFormats : [ReportOutputFormat]} type ReportOutputFormat{ id : String name : String description : String defaultformat : Boolean } I am using below query to hit the server query{     reportFormats{         id         name     } } and tried {     reportFormats{         id         name     } } as well, but still the result is same. I am getting same error. What could be root cause of this problem. – Kushagra Bindal May 11 '21 at 18:29
  • did you try running it first through playground? – Deeksha Pandit May 11 '21 at 18:34
  • Standalone application yes with sample repository and data but as I mentioned earlier as well that enterprise application I have directly tried, and I am getting below error. To access the api I need to have proper authentication token to be generated which I am assuming that won't be possible with playground... Any suggestion on this as how can we try playground in enterprise application. – Kushagra Bindal May 11 '21 at 18:41
  • I think same api should not generate the token but if you have token , that can be passed from the playground HTTP headers section. – Deeksha Pandit May 11 '21 at 18:47
  • I am getting below error in my logs while trying to call this API through playground. {\"operationName\":\"IntrospectionQuery\",\"variables\":{},\"query\":\"query IntrospectionQuery {\\n __schema {\\n queryType {\\n name\\n }\\n mutationType {\\n name\\n }\\n subscriptionType {\\n name\\n }\\n types {\\n ...FullType\\n }\\n directives {\\n name\\n description\\n locations\\n args {\\n ...InputValue\\n }\\n }\\n }\\n}\\n\\nfragment FullType on __Type {\\n – Kushagra Bindal May 11 '21 at 19:12
  • I have enabled the qraphql debug log and found that it is giving me below error " Query failed to parse" Is there any specific reason for the same. – Kushagra Bindal May 12 '21 at 06:33
  • I enable debug log and found that I am getting org.antlr.v4.runtime.misc.ParseCancellationExcept"org.antlr.v4.runtime.NoViableAltException" and thus my query is failing to parse. Can you please help in pointing out the root cause of this. – Kushagra Bindal May 12 '21 at 07:08
  • @DeekshaPandit I tried it today on one sample application and I have observed one important thing, when I am running through POSTMAN(Chrome/Window) it is working with contentType application/json but when trying with Postman as Graphql it is throwing above error. I am getting in playground as well. I have taken two sample and same happens with both cases https://github.com/TechPrimers/spring-boot-graphql-query-example/ & https://www.leawy.com/blog/75/graphql-query-language.htm – Kushagra Bindal May 12 '21 at 13:25
  • I code in nodejs where things are little simpler but if it is still an issue, we can connect on screenshare. – Deeksha Pandit May 13 '21 at 14:39
  • Thanks Deeksha I was able to resolve this issue. I was using postman with content-type as graphql which when I switched to RAW(JSON) it starts landing on the server properly. It is now working properly at my end. Now trying some complex scenarios. Thanks again for your all help & inputs – Kushagra Bindal May 14 '21 at 12:50

0 Answers0