crud-graphql-plugin
v1.0.7
Published
The `useDynamicGraphQL` custom hook abstracts the logic for handling GraphQL queries and mutations using the Apollo Client library in a React Native environment.
Downloads
4
Readme
The useDynamicGraphQL
custom hook abstracts the logic for handling GraphQL queries and mutations using the Apollo Client library in a React Native environment.
Table of Contents
- Introduction
- Dependencies
- Usage
- Installation
- Example
- API Reference
- Returned Object
- Mutation Handlers
- Data Processing Functions
- Contributing
- License
Introduction
This custom hook simplifies the management of GraphQL queries and mutations in a React Native application by providing a set of abstractions built on top of Apollo Client's useQuery
and useMutation
hooks.
Dependencies
Usage
import {useDynamicGraphQL} from 'crud-graphql-plugin';
const MyComponent= () => {
const {
loading,
error,
data,
createItem,
updateItem,
deleteItem,
deleteAllItems,
queryAllItems,
filterItems,
} = useDynamicGraphQL(
// pass your GraphQL queries and mutations here
);
}
Installation
npm i crud-graphql-plugin
Example
import { useDynamicGraphQL } from 'crud-graphql-plugin ';
// Define your GraphQL queries and mutations
const getItemsQuery = /* ... */;
const createItemMutation = /* ... */;
const updateItemMutation = /* ... */;
const deleteItemMutation = /* ... */;
const deleteAllItemsMutation = /* ... */;
const queryAllItemsQuery = /* ... */;
const filterItemsQuery = /* ... */;
// Use the custom hook
const {
loading,
error,
data,
createItem,
updateItem,
deleteItem,
deleteAllItems,
queryAllItems,
filterItems,
} = useDynamicGraphQL(
getItemsQuery,
createItemMutation,
updateItemMutation,
deleteItemMutation,
deleteAllItemsMutation,
queryAllItemsQuery,
filterItemsQuery,
);
// ... rest of your component logic
API Reference
Returned Object
The hook returns an object with the following properties:
• loading: Boolean indicating whether the GraphQL query is loading.
• error: Error object if an error occurred during the GraphQL operation.
• data: Result data from the GraphQL query.
• createItem: Function for handling the creation of items.
• updateItem: Function for handling the updating of items.
• deleteItem: Function for handling the deletion of items.
• deleteAllItems: Function for handling the deletion of all items.
• queryAllItems: Function for retrieving and processing data from the GraphQL query for all items.
• filterItems: Function for filtering items based on a searched text.
Mutation Handlers
• handleToCreate(variables): Handles the creation of items using the createItemMutation.
• handleToUpdate(variables): Handles the updating of items using the updateItemMutation.
• handleToDelete(variables): Handles the deletion of items using the deleteItemMutation.
• handleDeleteAllItems(): Handles the deletion of all items using the deleteAllItemsMutation.
Data Processing Functions
• handleQueryAllItems(): Retrieves and processes data from the GraphQL query for all items.
• handleFilterItems(searchedText): Filters items based on the searched text.
Contributing
Contributions are welcome!
License
This project is licensed under the ISC License