Comments (5)
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.
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.
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.
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.
Closing this issue for the time being, it can still be referenced if people have questions.
from graphql-tools.
Related Issues (20)
- Project Roadmap HOT 2
- Drop dependency on fast-url-parser HOT 2
- DEBUG=1 results in console.time() warnings
- @graphql-tools/mock does not work with Vitest HOT 5
- Deterministic (seeded) enum values
- Stitching: object field is null when all selections are delegated
- Http Executor encodes headers into query parameters which can leak Authorization headers
- Ability to get names of all the stitched queries in a middleware
- requireResolversToMatchSchema: 'warn' throws an error
- Support .astro file loading on graphql-tag-pluck HOT 1
- Troubles with connectionParams on subscriptions
- Adding content-type header is not supported when fetching schema
- Query does not provide a operation name as a property in the request body causing some server configurations to reject the request
- @graphql-tools/mock 9.0.1 causes panic: Internal errors HOT 2
- @graphql-tools/merge directive arguments compare should not require identical ordering HOT 1
- load babel options in `graphql-tag-pluck`
- [mock] relayStylePaginationMock sometimes returns an incorrect value for hasNextPage
- Batch execution throws an error when Array.prototype has additional property
- Federation @join__field requires parameter handling improvement
- filterSchema argumentFilter provides incorrect typeName
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql-tools.