Mocking schema capabilities
Start on client-side features before your server supports them
If your GraphQL server doesn't yet support a field that your client will use, you can still start building against that field by mocking its behavior within the client.
For example, let's say we want to add a feature to the Space Explorer app from description field to our schema's Rocket type:
type Rocket {id: ID!name: Stringtype: Stringdescription: String # field not yet supported}
But what if our back-end team isn't finished adding support for the description field? By mocking the field's behavior, we can still start developing the feature in our client. To do so, we'll follow the steps below.
1. Define a client-side schema (recommended)
Our client application can define a
Although a client-side schema isn't required for mocking, it helps team members understand your app's local capabilities. It also unlocks powerful local state support in tools like the
This client-side schema extends the Rocket type to add a description field (make sure to name the variable typeDefs as shown):
const typeDefs = gql`extend type Rocket {description: String}`;
We can then provide this schema to the ApolloClient constructor, like so:
const client = new ApolloClient({uri: 'http://localhost:4000/graphql',cache: new InMemoryCache(),typeDefs});
2. Define a read function
read functionOur client app doesn't yet know how to populate the Rocket.description field. To fix this, we can define a read function
Let's define our read function in the configuration object we provide to the InMemoryCache constructor:
const cache = new InMemoryCache({typePolicies: {Rocket: {fields: {description: {read() { // Read function for Rocket.descriptionreturn 'Placeholder rocket description';}},},},},});
This enables us to query the field, but we might not want to show the same boilerplate description for every rocket. To add variety to our mocked output, we can use a library like
import { faker } from "@faker-js/faker";// Returns 1 or 2 sentences of Lorem Ipsumconst oneOrTwoSentences = () =>faker.lorem.sentences(Math.random() < 0.5 ? 1 : 2);
We can then update our read function like so:
// (within InMemoryCache constructor)read() {return oneOrTwoSentences();}
Make sure to include libraries like faker.js only in your development build, because they can needlessly increase your production bundle size.
3. Query with the @client directive
@client directiveWe're ready to execute a query that includes our new field. Here's an abridged GET_LAUNCH_DETAILS query from the description field added:
export const GET_LAUNCH_DETAILS = gql`query LaunchDetails($launchId: ID!) {launch(id: $launchId) {siterocket {typedescription @client}}}`;
Notice that this field includes the @client directive. This directive tells Apollo Client not to include description in the query it sends to our server. This is important for two related reasons:
- The
descriptionfield is populated entirely locally, so including it in network requests isn't helpful. - The
descriptionfield isn't in our server-side schema yet, so including it will produce a GraphQL error.
We can now execute this query in a component with the useQuery hook as usual:
export default function LaunchDetails({ launchId }) {const { data } = useQuery(GET_LAUNCH_DETAILS, { variables: { rocketId } });return (<div><p>Rocket Type: {data.launch.rocket.type}</p><p>Description: {data.launch.rocket.description}</p></div>);}
4. Use live data when ready
When your server's support for the Rocket.description field is ready, you can begin using it by doing the following:
- Remove the
@clientdirective fromdescriptionin every query that includes it. - Remove the field's
readfunction (or modify the function so that it uses the current cached value instead of a random string).
For more information on the Apollo Client features used here, see the following: