Git Product home page Git Product logo

Comments (10)

dsebastien avatar dsebastien commented on July 29, 2024 5

Here's my take on the above, in case anyone's interested: https://medium.com/@dSebastien/graphql-api-integration-tests-in-a-spring-boot-2-x-kotlin-application-5840d3c5d66f
HTH! :)

from graphql-spring-boot.

oliemansm avatar oliemansm commented on July 29, 2024 4

With a little help from the example of @dsebastien and the effort of @mxab we added the first version of graphql-spring-boot-starter-test that should help with writing your tests. See example-graphql-tools for example how to use it.

from graphql-spring-boot.

tamland avatar tamland commented on July 29, 2024 3

The issue seems to be that MockMvc isn't working with servlets. A workaround is to disable the servlet mapping and add spring wrapper controller like so:

@RestController
public class GQLController {

    @Autowired
    private GraphQLServlet graphQLServlet;

    @RequestMapping("graphql")
    public void graphql(HttpServletRequest request, HttpServletResponse response) throws Exception {
        graphQLServlet.service(request, response);
    }
 }

from graphql-spring-boot.

sazzer avatar sazzer commented on July 29, 2024 2

from graphql-spring-boot.

sazzer avatar sazzer commented on July 29, 2024 1

from graphql-spring-boot.

oliemansm avatar oliemansm commented on July 29, 2024

Would like that as well

from graphql-spring-boot.

oliemansm avatar oliemansm commented on July 29, 2024

That certainly helps, but I'm struggling specifically with the part how to ensure that not my entire context is auto loaded, and that only the relevant GraphQL part is loaded. How did you solve that? Do you include a specific configuration for your test, or do you exclude all the stuff you don't need?

The other stuff (Jpa, MVC controllers, etc) which I don't need for the GraphQL part takes too long to load for my tests, so I'm looking for a way to cut that load time considerably.

from graphql-spring-boot.

oliemansm avatar oliemansm commented on July 29, 2024

The maintenance of that is indeed a big downside.. I'll stick with the entire context for now, thanks!

from graphql-spring-boot.

dsebastien avatar dsebastien commented on July 29, 2024

How do you maintain the test queries? As escaped Strings in code or within separate files?
I'd like to put my test queries in dedicated (.graphql) files, for example:

mutation {
  createThing(name: "foo", description: "bar") {
    uuid,
    name,
    description
  }
}

And an easy way in Java to convert the above to what I actually need to send over the wire:

{
  "query": "mutation {\n  createThing(name: \"e\", description: \"cool\") {\n    uuid,\n    name,\n    description\n  }\n}",
  "variables": null
}

Has anyone done this yet? :)

from graphql-spring-boot.

neerajsu avatar neerajsu commented on July 29, 2024

I had an issue with using GraphQLTestTemplate provided with spring-boot-starter-test where my tests were @Transactional.

If I annotated tests with @Transactional and say I created a new record in a table (Obviously it wasn't committed yet as the test execution hasn't completed). Then I invoked a query using GraphQLTestTemplate, the resolvers were not able to see the created record. This is because GraphQLTestTemplate uses TestRestTemplate which invokes a http call, so when the execution comes to the resolvers a new Transaction is created, and this transaction doesn't have access to the entities created before.

If I get rid of @Transactional from test, then the test works fine, as the record is actually committed into the DB. But the side effect is that test records are now in the DB, and other integration tests running might break.

So, to support transactional integration testing I created a custom Tester here. You just need to autowire it into your test and use it. It usage is exactly the same as GraphQLTestTemplate, except it will ensure to maintain the Transactional context of test cases when annotated with @Transactional.

Although the issue is closed, I added this comment in case someone else comes looking for a similar requirement just like I did.

from graphql-spring-boot.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.