Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5998

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T02:28:08+00:00 2024-11-27T02:28:08+00:00

Fast Frontend Development With Mocked GraphQL

  • 60k

Frontend developers spend a lot of time integrating their components and pages with the backend API. Sometimes, they can't continue working on a specific feature until it's implemented on the backend. And other times, they can't even load the app because the backend API might be down or they don't have an internet connection. Or maybe they have a slow internet connection, which would make the development workflow slower.

This wouldn't be an issue if the frontend developers have access to the backend project locally, but sometimes they don't.

One good solution to this problem is mocking the GraphQL data on the frontend side.

In this article, I'll show you how to set that up for GraphQL Apollo.

We need to create a new Apollo client

When building frontend apps, we usually instantiate an Apollo client, give it the GraphQL server url, configure it, and then start using it.

We still need this, but also we need to create another instance for the mocked data. However, instead of giving it the GraphQL server url, we give it a schema.

Only one client should be used when running the app – we can use an env variable to decide which one to use.

function createApolloClient() {   if (env.USE_MOCK) {     return createMockedClient()   }   return createLiveClient() }  function createLiveClient() {   // Creates and returns an Apollo client based on a GraphQL server URL   const apolloClient = new ApolloClient({     uri: 'http://yourgraphqlserver'   }) }  createMockedClient() {   // Creates and returns an Apollo client based on a provided schema   // I will show the code later } 
Enter fullscreen mode Exit fullscreen mode

In this article, we are interested in createMockedClient().

createLiveClient() should be what you usually write when creating an Apollo client with a GraphQL server url.

Defining the schemas

As mentioned above, we have to provide the schema code directly to the mocked client. But there are two cases when mocking the data. It's either mocking operations (queries or mutations) that don't exist on the backend yet, or mocking operations that already exist on the backend.

The first case would be when we want to start implementing a feature on the frontend without waiting for the backend to be ready. For this case we need to define the schema on the frontend, but we need to be sure we are using the same schema as what the backend will implement.

The other case is useful when we want to work on the frontend without being affected by the internet connection. In this case, the schema has to be the same as the live one. We can easily get the same schema from the backend by downloading it with the help of some tools, like graphql-cli{target=_blank} or get-graphql-schema{target=_blank}.

To make it easy to distinguish between the existing schema and the new one, I like to store each one in a different file – schema.js for the one downloaded from the server, and nextSchema.js for the one defined on the frontend only.

These files will look something like this:

// schema.js export const schema = gql`   // Existing types and inputs   type Item {}    // Exisiting queries and mutations   type Query {}   type Mutation {} `  // nextSchema.js export const schema = gql`   // New types and inputs   type NewItem {}    // New queries and mutations   type Query {}   type Mutation {} ` 
Enter fullscreen mode Exit fullscreen mode

createMockedClient()

Let me first show you an example of how an Apollo client with mocked data can be created, and then I'll explain it below.

But first make sure to install the needed NPM packages:

npm install @graphql-tools/schema @graphql-tools/mock @faker-js/faker 
Enter fullscreen mode Exit fullscreen mode

import { ApolloClient, InMemoryCache } from '@apollo/client/core' import { makeExecutableSchema } from '@graphql-tools/schema' import { addMocksToSchema } from '@graphql-tools/mock' import { SchemaLink } from '@apollo/client/link/schema' import { faker } from '@faker-js/faker'  import { schema } from './schema.js' import { nextSchema } from './nextSchema.js'  function createMockedClient() {   const executableSchema = makeExecutableSchema({     typeDefs: [schema, nextSchema]   })    const schemaWithMocks = addMocksToSchema({     executableSchema,      mocks: {       Query: () => ({         items: [...new Array(5)]       }),        Item: () => ({         id: faker.datatype.uuid(),         title: faker.lorem.sentence()       })     },      resolvers: {       Mutation: {         updateItem: (_, { itemId, title }) => {           return {             message: 'Item updated'           }         }       }     }   })    const apolloClient = new ApolloClient({     cache: new InMemoryCache(),     link: new SchemaLink({ schema: schemaWithMocks })   })    return apolloClient } 
Enter fullscreen mode Exit fullscreen mode

We are doing three things in this function.

First, we combine the two schemas and make it executable:

const executableSchema = makeExecutableSchema({   typeDefs: [schema, nextSchema] }) 
Enter fullscreen mode Exit fullscreen mode

Then we define the mock data to the schemas:

const schemaWithMocks = addMocksToSchema({   executableSchema,    mocks: {     Query: () => ({       items: [...new Array(5)]     }),      Item: () => ({       id: faker.datatype.uuid(),       title: faker.lorem.sentence()     })   },    resolvers: {     Mutation: {       updateItem: (_, { itemId, title }) => {         return {           message: 'Item updated'         }       }     }   } }) 
Enter fullscreen mode Exit fullscreen mode

Note how we add the query and type mocks in mocks and mutation mocks in resolvers.

We specify what each field in the type should return when it's returned from a query, like this:

Item: () => ({   id: faker.datatype.uuid(),   title: faker.lorem.sentence() }) 
Enter fullscreen mode Exit fullscreen mode

We are using faker{target=_blank} here to generate random values, but you can use any values you want.

If a query returns an array of a specific object type, then we specify how many instances of that object it should return by defining an empty array with a length:

Query: () => ({   items: [...new Array(5)] }) 
Enter fullscreen mode Exit fullscreen mode

The last step is to create the Apollo client and give it the schema with mocks through SchemaLink (instead of uri).

const apolloClient = new ApolloClient({   cache: new InMemoryCache(),   link: new SchemaLink({ schema: schemaWithMocks }) })  return apolloClient 
Enter fullscreen mode Exit fullscreen mode

How the workflow would look like

The cool thing about this mocking approach is that you don't need to change the production code to use it.

However, every time the live schema is updated on the backend, you have to download it and define mocks for it if you want to use the mocked client.

So, the steps are:

  1. Download latest schema using the tools mentioned above. Or if the schema is not available on the backend yet, add the expected schema changes to nextSchema.js.
  2. Add query and mutation mocks through mock and resolvers in addMocksToSchema.

apigraphqljavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.