Writing a custom Local Storage API for GraphiQL

How to customize GraphiQL history interactions

Janith Silva
4 min readJun 15, 2023

Hello people! Today I’m gonna share with you all how to write a custom local storage API for GraphiQL.

GraphiQL is a powerful in-browser IDE for exploring GraphQL APIs. It provides an interactive interface that allows developers to write and execute GraphQL queries, view responses, and explore the schema. One useful feature of GraphiQL is its ability to persist queries and settings in the browser’s local storage. In this blog post, we’ll guide you through the process of setting up a custom local storage API for a GraphiQL viewer, allowing you to store data specific to your application or component.

Table of Contents:

  1. What is GraphiQL?
  2. The Benefits of Custom Local Storage
  3. Building the Custom Local Storage API
  4. Usage of the Custom Local Storage API
  5. Conclusion

What is GraphiQL?

GraphiQL is an interactive development environment for GraphQL. It provides a user-friendly interface where developers can write GraphQL queries, view the results, and explore the schema. It is widely used during the development and testing phases of GraphQL APIs. GraphiQL simplifies the process of interacting with GraphQL by offering auto-completion, syntax highlighting, and documentation introspection.

The Benefits of Custom Local Storage

By default, GraphiQL stores query history and user preferences in the browser’s local storage. However, in some cases, you may want to isolate the storage for specific components or applications. This is where a custom local storage API comes in handy. It allows you to define a unique identifier (componentId) for each instance of a GraphiQL viewer, preventing conflicts and providing a more controlled storage environment. With a custom local storage API, you can store and retrieve data specific to your component or application without interfering with other instances of GraphiQL or other applications using local storage.

Building the Custom Local Storage API

The custom local storage API provides several methods that you can use to interact with the local storage. Here’s a detailed explanation of each method:

  1. setItem(key, value): This method is used to store an item in the local storage. It takes two parameters: key, which represents the key under which the item will be stored, and value, which represents the value of the item.
  2. getItem(key): This method retrieves the value of an item from the local storage. It takes a key as a parameter and returns the corresponding value.
  3. removeItem(key): Use this method to remove an item from the local storage. It takes a key as a parameter and removes the item associated with that key from the storage.
  4. length: This property returns the length of the value of a specific item in the local storage. If the item doesn't exist, it defaults to 0.
  5. key(index): This method returns the key at a given index in the local storage. It can be useful when you want to iterate over the keys or access a specific key by its index.
  6. clear(): This method clears all items from the local storage. It removes all key-value pairs associated with the key, effectively wiping out the stored data specific to the component or application.

By utilizing these methods, you can store, retrieve, remove, and manage data in the local storage according to your needs.

How did I implement it?

To set up a custom local storage API for GraphiQL, I used the following code snippet:

The customStorageAPI function accepts an object componentId as a parameter. It returns an object that provides methods for interacting with the local storage. The setItem method stores an item in the local storage with a key prefixed by the componentId. The getItem method retrieves an item from the local storage using the corresponding key. The removeItem method removes an item from the local storage. The length property returns the length of the value of a specific item (graphiql:queries in this example). The key method returns the key at a given index in the local storage. Finally, the clear method clears all items from the local storage.

Usage of the Custom Local Storage API

To use the custom local storage API with GraphiQL, you need to configure the storage prop of the GraphiQL component. Here's an example:

In the above code, we pass the custom storage API object as the storage prop to the GraphiQL component. We specify the componentId as "myComponent" to uniquely identify this instance of the GraphiQL viewer. The custom storage API will handle storing and retrieving data using keys prefixed with the componentId, ensuring isolation and preventing conflicts with other components or applications using local storage.

Conclusion

In this blog post, we explored the concept of a custom local storage API for GraphiQL viewers. We discussed the benefits of having a custom local storage solution and provided a code snippet for building the API. Additionally, we demonstrated how to use the custom storage API with the GraphiQL component. By implementing a custom local storage API, you can tailor the storage behavior of GraphiQL to meet your specific needs and prevent interference with other components or applications using local storage.

By harnessing the power of custom local storage, you can enhance the functionality and flexibility of your GraphiQL viewer, creating a more personalized and controlled GraphQL development experience.

Remember, customizing local storage is not limited to GraphiQL alone; you can apply similar techniques to other applications that utilize local storage to provide a tailored storage solution.

Happy coding with GraphiQL. Peace!

--

--

No responses yet