ariadne icon indicating copy to clipboard operation
ariadne copied to clipboard

Initial attempt at mocking servers

Open patrys opened this issue 6 years ago • 3 comments
trafficstars

Opening this as a draft to start the discussion. This gives you a set of tools to return fake (but compliant) responses for any schema and means to provide custom factories for certain data types.

To play with it:

from ariadne import load_schema_from_path, make_executable_schema
from ariadne.asgi import GraphQL
from ariadne.mocking import FactoryMap, MockResolverSetter

type_defs = load_schema_from_path("schema.graphql")

factory_map = FactoryMap()


@factory_map.type("Int")
def fake_int(value):
    # value is whatever the parent's key/attribute
    # with the same name as this field contained
    if value is not None:
        # if parent resolver provided a value, use it
        return value
    return 42


schema = make_executable_schema(type_defs, [..., MockResolverSetter(factory_map)])
app = GraphQL(schema, debug=True)

patrys avatar Apr 14 '19 09:04 patrys

CC: @tomchristie as you've briefly mentioned similar services.

patrys avatar Apr 14 '19 09:04 patrys

I was recently thinking about GQL Schema mocking / faking issue and came to some requirements that might be worth considering:

  • when on non-production env mixing up real data with fake is convenient to work on features until backend implements anything, cross-referencing might be hard but extremely valuable
  • most useful way to fake is IMO by providing hierarchical data structure, maybe external json so it can be easily modified by frontend devs
  • integrating something like faker needs to be relatively simple

salwator avatar Apr 20 '19 10:04 salwator

@salwator I'm using the above with faker. It will respect whichever real resolvers you already have in place so you can add real data progressively as the implementation happens. I specifically did not want to optimize around static data sources as this contributes to a problem of client code optimizing for a particular set of data.

patrys avatar Apr 23 '19 16:04 patrys