0

I’m looking for a way to run a query against a mock data object.

The problem I currently mock the GQL response via . That works great, but the mock’s result comes back exactly as defined. The mock response can return fields the query did not ask for.

The request I would like to have a fake data library and generate the mock result data by getting the fake data and “running” the query against it. Apollo iOS has pretty much what I want 1, but it’s not available for React + Typescript. For example:

const fakeUserData = {id: 1, name: "First Last"};
query = gql`query { user { id } }`;
query.data.from(fakeUserData):
// Returns: {id: 1} without `name`.

Does this exist somewhere? Does this pattern make sense? Thanks!

The only thing I've found is the iOS client .Data.from(mock), but it doesn't work with TypeScript.

1 Answer 1

0

You can write the "too much" data to the store using cache.writeQuery after which you can also query for partial data.

For an example, see the Apollo Client tests themselves

      cache.writeQuery({
        query,
        data: { hello: "from cache" },
      });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.