wolfiton

wolfiton OP

Hi everyone,

How can i test my schema with wormwood when i have my schema in MyAppWeb/schema.ex?

Wormwood docs tells me that I need to load_gql MyCoolApplication.MyAbsintheSchema, "assets/js/queries/MyQuery.gql"

and this to my knowledge looks like the client part of graphql.

So can I test my back-end of my absinthe api with this?(confused)

Github with docs here GitHub - wormwood-elixir/wormwood: Wormwood is a tiny library to aid in testing GraphQL queries against an Absinthe schema. It allows you to test your query documents inside ExUnit test modules, and requires no HTTP requests to occur during testing. · GitHub

Also this article Wormwood - an Explicit Way to Test Absinthe GraphQL APIs | Black Duck Blog
Update: Found the examples link but no test wormwood/lib/examples at 0.1.0 · wormwood-elixir/wormwood · GitHub
Reported an issue regarding this problem here Problems understanding how to use the library for testing an absinthe schema · Issue #8 · wormwood-elixir/wormwood · GitHub

Found an example here for test GitHub - wormwood-elixir/wormwood at 0.1.0 · GitHub

Thanks.

First 10 of 12 Posts Switch mode

wolfiton

wolfiton OP

I think, I go it this library makes absinthe feel like working with graphql in nodejs.

The gql files wormwood/assets at 0.1.0 · wormwood-elixir/wormwood · GitHub
Absinthe implementation: wormwood/lib/examples at 0.1.0 · wormwood-elixir/wormwood · GitHub
Test examples: wormwood/test/examples at 0.1.0 · wormwood-elixir/wormwood · GitHub

wolfiton

wolfiton OP

Does anyone have any opinions regarding this testing framework?

Qqwy

Qqwy

TypeCheck Core Team

Hi @wolfiton! Since your question is quite specific, it can take a day (or a couple of days) before someone who uses Wormwood + Absynthe to get back to you, so hang tight :slight_smile: .

Darin

Darin

Hey @wolfiton,

I literally just integrated Wormwood into my application, so I am by no means an expert, but I can walk you through what it looks like for me.

First, I created the queries that I wanted to test. These are the .gql files that you saw in the assets folder in the Wormwood project. These are the queries that are going to be run against your GQL schema, the same as what will happen on the frontend. I put mine in a folder called test/support/queries, and then created different ones for each use case: one to feature current user data, one to fetch data based on a slug, one to fetch all the data associated with a model, etc.

Second, I created the tests themselves. This I put into a folder called test/myproject_web/schema. Here is an example of what the test looks like. My application is a book tracking application, hence the usage of book related info.

defmodule MyAppWeb.Test.BooksSchemaTest do
  use MyApp.DataCase, async: true
  use Wormwood.GQLCase

  load_gql(MyAppWeb.Schema.Schema, "test/support/queries/books.gql")

  describe "books.gql" do
    test "should return all 6 books" do
      result = query_gql()
      {:ok, query_data} = result
      assert length(get_in(query_data, [:data, "books"])) == 6
    end
  end
end

Some things to note.

  1. I have use MyApp.DataCase, async: true at the top, which is different from docs. Just due to how my database is setup, I need this to run my tests.
  2. The line load_gql(MyAppWeb.Schema.Schema, "test/support/queries/books.gql") basically says "my GQL schema is located in the module that is the first argument, and I want to run the query located in the file at the second argument).
  3. query_gql() will run the query, and return the result in an OK tuple. The rest is just the test logic. In my case, I am checking that after I run the query, it returns 6 different books, as that is the number of books I have included in my seed.exs.

As for my opinions of the framework, they aren’t too fleshed out, but I can give you some first thoughts. I do like it because it seems like an easy way to test the actual GraphQL queries. I was testing the functions that my queries resolve to before (and still am), which was pretty close, but this is nice because it is just one extra layer of abstraction. Also, I don’t need to try entering things into Graphiql, and instead can just look at my test queries and run them if I want to see how they’re going to behave, meaning they serve as a bit of documentation. Finally, more tests are always nice.

I hope that helps.

wolfiton

wolfiton OP

Awesome, thanks @Darin for sharing your thoughts and experience with this testing framework.

I also have a couple of questions if you don’t mind:

Have you tested any middleware or a subscription with this testing framework yet?

Does dataloader work with this testing framework?

Do you think it will be supported long term?

wolfiton

wolfiton OP

Just tried your method for a simple test and it works great. Thanks for sharing.

wolfiton

wolfiton OP

Has problems when you use something like this in the schema

@desc "Queryable fields for Blog Articles"
  query do
    field :blog_articles, list_of(:blog_article) do
      resolve(fn _, _, _ ->
        {:ok, Blog.list_articles()}
      end)
    end
  end

  @desc "define fileds that can be acessed for Blog queries"
  object :blog_article do
    field :id, :id
    field :title, :string
  end
end

The gql files will not recognize this format and it will not translate to BlogArticle and BlogArticles it will just give errors for test with invalid argument and return nil.

I feel that this is a bug.

Because if I use : :article and :articles it will translate to Article and Articles.

Darin

Darin

No problem.

Have you tested any middleware or a subscription with this testing framework yet?

Not yet. I do have some simple middleware I use for authentication that might be an easy one to test. I might try that over the weekend, and I could inform you of what my experiences are. I haven’t tried mocking the authentication stuff at all for testing, so I’m not really sure what I’ll run into.

Does dataloader work with this testing framework?

I have dataloader setup for the queries that I tested and it seemed to work just fine. I haven’t gone into the logs or anything to check that the queries themselves are done in bulk, but I don’t see why not.

Do you think it will be supported long term?

I can’t answer this one for sure. The last commit was in late October of last year, and it doesn’t seem to be insanely popular with only 41 stars so I doubt there will be much community contribution at this point. However, my mindset is that I need to test these GraphQL queries, and this seems like a good way to do it now. If in the future another library pops up that seems to be more maintained and popular it’ll probably be a lot easier for me to swap them out if I have already broken out my .gql files and specific GraphQL test cases. Therefore I think it’s probably more worth my time to just jump in and start writing some tests using Wormwood rather than let the analysis paralysis set in.

wolfiton

wolfiton OP

Thanks have you encounter my problem in #post7 with the naming problems?

wolfiton

wolfiton OP

Will you contribute to wormwood testing framework, because it seems that you find it useful and use it in your projects?

Because if you would, I would like to join in and try to help with what I can to make it very easy to use.

I also find it very useful just thinking that i will already have my graphql setup for the front-end from wormwood so i will not need to write it again for the client(just copy paste).

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement