graphql

shen 2020-06-02T15:06:50.121900Z

is this a good place to ask lacinia questions?

gklijs 2020-06-02T15:27:58.122Z

Yes, it's a shame history only goes so far, otherwise you could see about 50% of this channel is exactly that.

shen 2020-06-02T16:00:40.122500Z

ah ha

shen 2020-06-02T16:03:16.125Z

I've implemented field preview in a resolver using executor/selects-field?. That seems to work well. My question is, how do I test that resolver by itself, outside of the context of a full query? I see that the lacinia unit tests use parse-query and parse-query->context here https://github.com/walmartlabs/lacinia/blob/b3c8d18e8c1dc30953fafa471927ee62238a3bba/test/com/walmartlabs/lacinia/selections_tests.clj#L28-L33 . Is there no simpler way to create a mock context?

chrisulloa 2020-06-02T18:33:31.126100Z

> My question is, how do I test that resolver by itself, outside of the context of a full query? You can pass in a context map to your resolver with the fields listed in the context, right?

chrisulloa 2020-06-02T18:33:49.126500Z

Like for example (defn resolver [context args parent] …)

chrisulloa 2020-06-02T18:45:13.128Z

ah okay, so part of selects-field? shows you how it’s stored in the context https://github.com/walmartlabs/lacinia/blob/c2a99358b35e6f829590c9f1df7e203da2b636db/src/com/walmartlabs/lacinia/executor.clj#L513

hlship 2020-06-03T17:29:22.135800Z

I don't encourage people to do this, as the internals and structure could change at any time. What I'd appreciate would be, if you go down this path, to report back so that we can address it properly in the framework somehow, perhaps functions to assemble a context reflecting the state you wish to test.

👍 1
hlship 2020-06-03T17:30:11.136Z

That being said, when we test our GraphQL we essentially pass full and correct queries in and assess the result map. We often mock out databases and external services in some way, but generally exercise the resolvers as-is.

hlship 2020-06-03T17:31:04.136200Z

That leverages lacinia to, for example, validate that the raw value returned from the resolver is valid, and deals with cases of a resolver returning a wrapped result, or an asynchronous resolver result promise, etc.

hlship 2020-06-03T17:31:55.136400Z

Effectively, we don't unit test resolvers per-se, we integration test queries into our schema.

hlship 2020-06-03T17:33:04.136600Z

In fact, I prefer to override things and actually send the request in via HTTP to truly see if it is working end-to-end. This can still be amazingly fast within a test suite, especially if your component system can persist between test functions.

👍 1
chrisulloa 2020-06-03T18:58:23.138400Z

This is what we usually do too.. we’ll stub out the selects-field? and then test conditional logic of the resolver (rather than building out the context with lacinia internals) and instead run a pedestal test servlet to test end-to-end thru HTTP calls. This is good information though to avoid doing this in the future.

chrisulloa 2020-06-02T18:46:53.129600Z

You could build out a context with this in it to test out any conditional behavior around selects-field? if you needed to, not sure if there’s a better way

{:com.walmartlabs.lacinia.constants/parsed-query {:selections [{:field-definition {:qualified-name :Something/something}}]}}