Giter Site home page Giter Site logo

Comments (7)

martijnwalraven avatar martijnwalraven commented on July 23, 2024

We talked a little about this at the Berlin meetup. As I mentioned there, I'm working on a new version that fixes some issues with GraphQLMap (among many other changes).

I want to make sure the new design works for your use case as well. Can you tell me more about what you're trying to do? What networking library are you using, how do you integrate with it, and why do you need to use it instead of the default network transport?

from apollo-ios.

martijnwalraven avatar martijnwalraven commented on July 23, 2024

I just released version 0.4.0 of Apollo iOS, which includes a redesigned GraphQLMap that will hopefully solve your issue. You should be able to access query.variables.jsonValue to get a representation suitable for JSON serialization.

Please give it a try and let me know what you think! (You'll also have to update apollo-codegen to 0.9, but it will warn you if you don't.)

from apollo-ios.

fruitcoder avatar fruitcoder commented on July 23, 2024

Hey Martijn!
I just updated Carthage :) It looks like I imagined it to be, thanks!
I actually dropped Moya for the GraphQL part since the mindset of static routes felt like contradicting the approach of GraphQL. I will stub and test my view models by stubbing the network transport with fake responses.
I really don't know if there should be a separation of concerns for the network and parsing part, since those are so interconnected. Usually, I test whether my parsing code can deal with bad json, but basically you already handle this automatically. Everything that should go wrong should be network errors :) so I'm happy with how you implemented it!

Thanks for your work!

from apollo-ios.

martijnwalraven avatar martijnwalraven commented on July 23, 2024

Good to hear! I agree Maya doesn't add much value here and seems more confusing than helpful.

I'd be interested in hearing more about how you approach testing, to better understand the requirements and to see if there's anything more we could add to make it easier.

Stubbing the network transport is probably the best solution right now, especially if you'd like to test for error conditions. I think you're right and you don't you need to test for bad JSON. The only bad JSON I can think of would be an invalid GraphQL response, probably as a result of a bug in the server library. So you're left with either a network or GraphQL errors to test for.

I'm wondering how adding caching and consistency management (which I'm working on right now) will affect testing. Depending on what you're testing, it may make sense to preload the store with test data for example, instead of stubbing the network transport.

from apollo-ios.

fruitcoder avatar fruitcoder commented on July 23, 2024

Hey Martijn,

I've now tried and failed to stub a fake NetworkTransport for a query. I tried:

class DetailsQueryStubTransport: NetworkTransport {
  func send<Operation : GraphQLOperation>(operation: Operation, completionHandler: @escaping (GraphQLResponse<Operation>?, Error?) -> Void) -> Cancellable {
    if operation is DetailsQuery {
      completionHandler?(GraphQLResponse(operation: operation, rootObject: JSONObject(dictionaryLiteral: ??))
    }
  }
}

but I don't really know what to put in there. For me it would be awesome if I could directly stub the a json response string like this:

{
  "data": {
    "viewer": {
      "clip": {
        "id": "abcd",
        "title": "test title",
        "description": null,
        "teaserImages": {
          "edges": []
        }
     }
  }
}

To make that work you could somehow inject an encoded json response (Data) that will in turn will call the completion handler with a valid GraphQLOperation.Data.

Also, I changed the access level of the HTTPNetworkTransport class, to make it easy to subclass and debug, why a request might have gone wrong (easier logging): here

from apollo-ios.

martijnwalraven avatar martijnwalraven commented on July 23, 2024

You should be able to just construct a GraphQLResponse from a Swift-based JSON representation, like this:

let response = GraphQLResponse(operation: query, rootObject: [
  "data": [
    "hero": ["__typename": "Droid", "name": "R2-D2"]
   ]
])

(See ParseQueryResponseTests.swift for more examples.)

If you really want to pass in a JSON string instead, you could create a helper method that uses JSONSerialization.jsonObject to parse the string into a dictionary, something like this:

func jsonObject(string: String) -> JSONObject {
  let data = string.data(using: .utf8)!
  return (try! JSONSerialization.jsonObject(with: data, options: [])) as! JSONObject
}

from apollo-ios.

fruitcoder avatar fruitcoder commented on July 23, 2024

I will try that, thanks :)

from apollo-ios.

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.