Hi all, I want to integrate <Pact> to our iOS appl...
# pact-swift
i
Hi all, I want to integrate Pact to our iOS application that works with the backend via GraphQL and I'm struggling finding a way to make it work. Does PactSwift has a helper method for working with GraphQL as pact-js ?
I'm getting the following error and I'm not sure if it is because of GraphQL or maybe my approach is not good.
Copy code
Failed to verify Pact! Actual request does not match expected interactions...

Reason:
	Missing request

Expected:
	POST /graphql
m
There's no graphql wrapper there but it's easy enough to do
You could take a look at the graphql wrapper in JS and borrow it.
That error is pretty clear tho - you're not sending a POST request to /graphql
m
I’ll have a look into what/how to support GraphQL in PactSwift
👍 2
i
Thank you guys, I will do some more digging ! 🙌
👍 1
Hi @Matt (pactflow.io / pact-js / pact-go) @Marko (IttyBittyApps / pact-swift), I tried to insert de graphql specific data in to the body, but I'm still getting the same error as above, not sure if I'm missing something, if you can please assist or point me to what I'm doing wrong. This are some screenshots with what I'm doing on my side:
Am I missing some configurations some where? I added the PactSwift package to my test target and wrote the above simple test with an endpoint that gets the minimum app required version.
Maybe my approach for trying to make it work with graphql is wrong, does that graphql wrapper planned to happen for the near future?
m
PactSwift does not (yet) support GraphQL. I’ll get the support going for it soon. Just can’t promise any dates.
Also, PactSwift mock server runs on a localhost on a randomly assigned port. I see you are assigning url to your client. If your client is sending it to the real server your test will fail due to localhost not being hit. Use the mockServiceURL passed into the .run methods completion block.
☝️ 1
i
Thank you for pointing me in to the right direction, but I managed to stumble upon another issue:
I'm getting this error and don't know how to go around it. If I try to send nil or NSNull it will throw me this "*failed - Error preparing pact! A key or value in the structure does not conform to 'Encodable'*"
I also tried to remove the "variables" key and value pair but I'm getting this error. Maybe I can try to remove the variables from the graphql query, from Apollo, not sure if that is possible,
m
PactSwift only accepts types that conform to
Codable
. As it iterates through the content, extracts any
Matcher
and
ExampleGenerators
you send it and erases them into a custom
AnyEncodable
type so that it can be sent across http and the payload is prepared for
PactSwiftMockServer
to consume and be able to interact with. When you serialised your json body into your
jsonData
you sent something that doesn't conform to
Encodable
- validate jsonData does not include something weird.
for the
"null"
, it should've been fine in your regular REST interactions as it's defined as a
String
. If you'd need to define a
"nullable_value": null
you'd need to use a
Matcher.MatchNull
- again due to conforming to
Codable
protocol.
though I am confused about
Copy code
Expected 'null' to be equal to 'null' - Body does not match the expected body definition
This message is coming from the pact rust core. There is also a way to log interactions between PactSwift and Pact rust-core by setting an env var in your scheme:
PACT_ENABLE_LOGGING: all
the
Expected a Map with keys id, operationName, query but received one with keys id, operationName, query, variables - Body does not match the expected body definition.
is pretty much a valid test and it is failing (because it seems to be too strict). You are defining the expectation that your client will send a specific object with 3 keys, but your client is actually sending an object with 4 keys. So that means your client is not honouring the contract and doing something different to the set expectations.
i
Hi again, I think I managed to make it work, but I'm having an issue with the dynamic port, can I make it static? Because when I start my services locally they go on to static port number.
Or, maybe I understood it wrong. Should that endpoint that I am running end up heating my local service and respond successfully?
I think I managed to make it work.
But in this case you don't need to have your API services running locally, I understood it wrong.
Also, if I want to create multiple tests, do I need to create multiple test classes with diferent consumer and provider names? Because, currently I see that it overwrites the last one in the
tmp/Pact
folder.
m
Awesome! Port for mock server is randomised for each test that’s run. The mock server stands in for your "real" API services (local or remote) if that’s what you’re saying/asking? Pact’s mock server is used as it has the logic to verify incoming interactions for client tests and responds with mocks. It takes your expectations, listens to the requests, responds with what you defined in your test and keeps a tally on what the interactions were and if they match. You should have a static MockService object to be able to append multiple tests on one provider (to one contract file). Otherwise it will only write the last interaction into pact file. See the examples provided on PactSwift’s github repo. Feel free to suggest any improvements for GraphQL specifics by sharing info here or raise an issue in the repo 👍
i
Thank you Marko for your time and fast responses ! 🙌