You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to set up a mock server for testing purposes that returns mock data that is deterministic across runs - i.e. using seeds.
However, I don't want all calls returning in the same info (e.g. all Strings to be "Hello World"), so my idea was to use the query path and variables as a seed for something like Fakerjs - i.e.
I'm unsure if achieving this using @graphql-tools/mock is possible, and if so - how? The public API for custom-resolving scalars does not pass the query path to the resolver.
Ideally, I'd do something like this:
import{addMocksToSchema,mockServer}from'@graphql-tools/mock'// cache of a query path to Faker instance, so multiple calls to the same thing increment the internal count of faker, and not create a new instance, removing the usefulness of a seeddeclareconstfakerInstances: {/* Get or create a Faker instance for the given context */get(context: {path: string,variables?: object}): Faker;}// Mock objectconstmocks={Int: ({ path, variables })=>fakerInstances.get({ path, variables }).number.int(),Float: ({ path, variables })=>fakerInstances.get({ path, variables }).number.float(),String: ({ path, variables })=>fakerInstances.get({ path, variables }).word.sample(),DateTime: ({ path, variables })=>fakerInstances.get({ path, variables }).date.recent().toISOString()}constserver=mockServer(schema,mock);
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm trying to set up a mock server for testing purposes that returns mock data that is deterministic across runs - i.e. using seeds.
However, I don't want all calls returning in the same info (e.g. all Strings to be
"Hello World"
), so my idea was to use the query path and variables as a seed for something like Fakerjs - i.e.and the query
I'm unsure if achieving this using
@graphql-tools/mock
is possible, and if so - how? The public API for custom-resolving scalars does not pass the query path to the resolver.Ideally, I'd do something like this:
Any help would be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions