Hi, I’m using PactSwift to contract test some Grap...
# pact-swift
i
Hi, I’m using PactSwift to contract test some GraphQL endpoints and everything is working for
queries
but when I try to test a
mutation
I’m getting this error:
Copy code
/Users/ipoe/GitHubRepo/lp-pros-ios/LoanPalKitTests/Pact Tests/PactTests.swift:499: error: -[LoanPalKitTests.PactTests testAcceptTermsMutation] : failed - Test did not complete within 10.0 second timeout! Did you run testCompleted() block?
/Users/ipoe/GitHubRepo/lp-pros-ios/LoanPalKitTests/Pact Tests/PactTests.swift:499: error: -[LoanPalKitTests.PactTests testAcceptTermsMutation] : failed - Failed to verify Pact! Actual request does not match expected interactions...

Reason:
	Missing request

Expected:
	POST /graphql
This is the entire console log:
Copy code
Test Suite 'Selected tests' started at 2022-11-24 11:02:15.822
Test Suite 'LoanPalKitTests.xctest' started at 2022-11-24 11:02:15.824
Test Suite 'PactTests' started at 2022-11-24 11:02:15.824
Test Case '-[LoanPalKitTests.PactTests testAcceptTermsMutation]' started.
------ GraphQLOperation requestQuery -----
["query": Optional("mutation AcceptTermsAndConditions($input: ID!) {\n  acceptTerms(termsId: $input) {\n    __typename\n    currentTermsAccepted\n  }\n}"), "operationName": Optional("AcceptTermsAndConditions"), "variables": Optional(["input": Optional("TRMS-***")])]

Outgoing ----- AcceptTermsAndConditions ----- HTTPRequest details:
	Endpoint: <http://127.0.0.1:4358/graphql>
	Additional Headers: [
		X-APOLLO-OPERATION-TYPE: mutation,
		X-APOLLO-OPERATION-NAME: AcceptTermsAndConditions,
		Content-Type: application/json,
		X-Client-Metadata: ***,
		Authorization: ***,
		apollographql-client-version: 1.9.3-1,
		apollographql-client-name: com.loanpal.loanpalPros-apollo-ios,
	]
	Cache Policy: returnCacheDataElseFetch
	Operation: LoanPalKit.AcceptTermsAndConditionsMutation
	Context identifier: nil

Request variables: ["input": "TRMS-***"]

[iOS App]11:02:16.018 [INFO] Request started ----- AcceptTermsAndConditions -----
------ RequestBodyCreator -----
["operationName": Optional("AcceptTermsAndConditions"), "query": Optional("mutation AcceptTermsAndConditions($input: ID!) {\n  acceptTerms(termsId: $input) {\n    __typename\n    currentTermsAccepted\n  }\n}"), "variables": Optional(["input": Optional("TRMS-fdcef7bc-2484-468b-94b8-729a7f06a1e6")])]
/Users/ipoe/GitHubRepo/lp-pros-ios/LoanPalKitTests/Pact Tests/PactTests.swift:499: error: -[LoanPalKitTests.PactTests testAcceptTermsMutation] : failed - Test did not complete within 10.0 second timeout! Did you run testCompleted() block?
/Users/ipoe/GitHubRepo/lp-pros-ios/LoanPalKitTests/Pact Tests/PactTests.swift:499: error: -[LoanPalKitTests.PactTests testAcceptTermsMutation] : failed - Failed to verify Pact! Actual request does not match expected interactions...

Reason:
	Missing request

Expected:
	POST /graphql

Test Case '-[LoanPalKitTests.PactTests testAcceptTermsMutation]' failed (10.233 seconds).
Test Suite 'PactTests' failed at 2022-11-24 11:02:26.059.
	 Executed 1 test, with 2 failures (0 unexpected) in 10.233 (10.235) seconds
Test Suite 'LoanPalKitTests.xctest' failed at 2022-11-24 11:02:26.060.
	 Executed 1 test, with 2 failures (0 unexpected) in 10.233 (10.236) seconds
Test Suite 'Selected tests' failed at 2022-11-24 11:02:26.060.
	 Executed 1 test, with 2 failures (0 unexpected) in 10.233 (10.238) seconds
Should I do something special for
mutations
? Did you guys added support for GraphQL as I see you did for Pact JS?
Please ignore the “***“, from what I can see everything matches? Can I access some more details logs?
This is the code:
Copy code
func testAcceptTermsMutation() {
        let operation = AcceptTermsAndConditionsMutation(input: "TRMS-fdcef7bc-2484-468b-94b8-729a7f06a1e6")

        PactTests.mockService
            .uponReceiving("A request for accepting the terms and conditions")
            .given(ProviderState(description: "Accept terms and conditions", params: ["input": "TRMS-fdcef7bc-2484-468b-94b8-729a7f06a1e6"]))
            .withRequest(method: .POST, path: "/graphql", query: nil, headers: headers, body: operation.requestQuery().jsonObject)
            .willRespondWith(status: 200,
                             headers: nil,
                             body:
                                [
                                    "data": Matcher.SomethingLike(
                                        [
                                            "acceptTerms": Matcher.SomethingLike(
                                                [
                                                    "__typename": Matcher.EqualTo("User"),
                                                    "currentTermsAccepted": Matcher.SomethingLike(true)
                                                ]
                                            )
                                        ]
                                    )
                                ]
            )

        PactTests.mockService.run(timeout: nil) { mockServiceURL, completion in

            let graphClient = GraphQLClient(sessionClient: LoanPalURLSessionClient(), url: URL(string: "\(mockServiceURL)/graphql"))
            graphClient.executeMutation(operation: operation) { _, error in
                completion()

                if error != nil {
                    XCTFail("Accept terms and conditions request failed \(#function)")
                }
            }
        }
    }
I did a quick test and removed the call from under the mock service and the endpoint is called, so the issue is related to how the mutation is done, because the endpoint goes trough and all other queries are working just fine with Pact.
m
PactSwift does not have anything specific implemented to support for GraphQL
i
Hi @Marko (IttyBittyApps / pact-swift), But do you know why for my case the mutations are not working, do I need to do something special? We are using Apollo for everything that is related to networking.
m
You can set env var
PACT_ENABLE_LOGGING
to
all
in your scheme for pactswift to be more verbose at what it is doing. Hopefully that can give you a bit more info on what is happening.
We had someone ask about graphql here a few months ago. They had a few beginner bumps they made it across but after that iirc they’ve reported everything was working fine for them with the framework as is so no further investigation into graphql specifics was done.
Have you made any positive progress on this, @Iulian Poenaru?