0
votes

I have developed an application using graphql framework with the spqr and the dependencies are given below. My spring boot application has a context path say, server.servlet-path=/asdfg

    <dependency>
        <groupId>io.leangen.graphql</groupId>
        <artifactId>spqr</artifactId>
        <version>0.9.7</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-spring-boot-starter</artifactId>
        <version>4.2.0<</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java-tools</artifactId>
        <version>5.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphiql-spring-boot-starter</artifactId>
        <version>4.2.0<</version>
    </dependency>

The application is a Java / Spring Boot application where my end point is a PersonQuery - shown here

@Component
public class PersonQuery {
    @GraphQLQuery(name = "getPersons")
    public List<Person> getFirstNPersons(@GraphQLArgument(name = "count") int count){
        List<Person> result = new ArrayList<>();
        ...
        return result.stream().limit(count).collect(Collectors.toList());
    }
}

Now on application start up, I load the personQuery and then build Schema. (I use spqr and there are NO .graphqls file with type definitions but they are annotated as you can above) with a @RestController class.

@RestController
public class GraphQLController {
    private static final Logger LOGGER = LoggerFactory.getLogger(GraphQLController.class);
    private final GraphQL graphQL;
    @Autowired
    public GraphQLController(PersonQuery personQuery) {
        // Schema generated from query classes
        GraphQLSchema schema = new GraphQLSchemaGenerator().withResolverBuilders(
                // Resolve by annotations
                new AnnotatedResolverBuilder(),
                // Resolve public methods inside root package
                new PublicResolverBuilder("com.xyz.asd.services"))
                // Query Resolvers
                .withOperationsFromSingleton(personQuery)
                .withValueMapperFactory(new JacksonValueMapperFactory()).generate();
        graphQL = GraphQL.newGraphQL(schema).build();
    }

    @PostMapping(value = "/graphql", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public Map<String, Object> indexFromAnnotated(@RequestBody Map<String, String> request, HttpServletRequest raw) {
        ExecutionResult executionResult = graphQL.execute(ExecutionInput.newExecutionInput().query(request.get("query"))
                .operationName(request.get("operationName")).context(raw).build());
        return executionResult.toSpecification();
    }

The above code is good enough to bring up http://localhost:8080/asdfg/graphiql where "asdfg" is my application context path. But in the graphIQL UI, I could not see the schema under documentation as it normally shows. The reason I found is though graphIQL UI makes a POST call with /graphql it is not adding context on the path. That is POST http://localhost:8080/graphql instead of POST http://localhost:8080/asdfg/graphql.

If I remove the context path from the spring application, the application works well. Is there a way I can include the context path in the POST http://localhost:8080/graphql call?

1
Can you be more specific about what you need help with? Can you provide some more context to your question? - maxpaj
Thanks. I updated the original thread with more code snippets and details. - Pearl Matt

1 Answers

0
votes

Maybe you should change your *.graphqls file?

mapping: /graphql --> mapping: /graphql

From:

graphql:
  servlet:
    mapping: /graphql

To:

graphql:
      servlet:
        mapping: /asdfg/graphql