GithubHelp home page GithubHelp logo

Comments (5)

helfer avatar helfer commented on May 1, 2024

A refinement of the idea above:
Mock objects are created only when they are requested by a resolver, but once they have been created, they get inserted into a pool of objects of that type. Whenever an object of this type needs to be returned by a mock resolver, we check the pool to see if we find something that matches the requirements of the kind of object that that mock resolver generates. If we do, we return that object instead of creating a new one.
Lists (and lists of lists) would get existing objects from the pool until the pool is depleted, then generate new ones to get to the expected number of items in the list.

Generating mock objects in this way would have the benefit that data returned in subsequent queries would be consistent while not giving up the convenience of creating mock data on the fly.

The main challenge for implementing this is creating an easy to use interface for creating mock scalars that we can search against if some randomness is desired. One solution that comes to mind is to create classes for all scalars like we did for MockList. A random int between 10 and 20 for example would be represented as new MockInt(10,20). If type Person has an age, and a mock resolver for the Person type returns people with ages between 10 and 20, we can then use the MockInt instance to search the store of existing Person and return a matching one if it exists, or create a new one if it doesn't.

Strings could be represented by their length, a regexp that they have to match etc. If we go down this path, my guess is that further down the road it will lead to duplicating much of the functionality of boo1ean/casual, but in a way that lets us search our pool for a matching randomly generated thing.

from graphql-tools.

helfer avatar helfer commented on May 1, 2024

Actually, thinking about this a little bit more, it seems attractive at first, but doesn't really get us all the way there. If you want mocked data to be completely realistic and support almost anything, I think the best way to go is to generate that data at startup according to some rules which can be defined in a similar fashion to the mock data before.

For example, let's say you want to mock some users who have todo lists with tasks. Some lists have owners, some don't. Some tasks are completed, some are not.

For this scenario, you'd like to be able to say how many users you want to generate, then how many lists each user should have, then how many tasks each list should have and how many of them should be complete vs. incomplete. Then you probably also want to create some lists that don't have owners. You can do that separately.

Next you'll have to define actual resolve functions that work against this JSON store, which is basically like writing a server, but just a slightly simpler one because you don't have to write the loaders. What that really boils down to is that we've basically written a server. Now we're mocking the backends and the loaders, but you have to write all the resolve functions. That's useful too, but it requires a lot more effort than before.

My intuition here is to see how far people can go with stateless mocking and then find other solutions for more complex tasks, if that's desired.

from graphql-tools.

gbisheimer avatar gbisheimer commented on May 1, 2024

Great work you are doing with apollostack! Keep going!

Just wondering if you know this tool for creating a mock api. The UI is nice and configuring the mock data pretty fast and easy.

from graphql-tools.

helfer avatar helfer commented on May 1, 2024

Thanks! Yes, I checked mockapi.io when I was writing the medium post. The UI is really nice, but I think we could do much better with creating mock data, especially relational one. When I tried it, I couldn't even figure out how to mock an array of integers, which should be trivial imho.

The other downside of mockapi is that you can't run it locally, so it's not as useful for unit testing.

With relatively little work, some could build a really good mocking service for GraphQL with a great UI like mockapi, but much more powerful, I think.

from graphql-tools.

helfer avatar helfer commented on May 1, 2024

Closing this issue for the time being, it can still be referenced if people have questions.

from graphql-tools.

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.