mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
fork refine
This commit is contained in:
43
packages/graphql/src/utils/generateUseOneSubscription.ts
Normal file
43
packages/graphql/src/utils/generateUseOneSubscription.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { MetaQuery, BaseKey } from "@refinedev/core";
|
||||
import * as gql from "gql-query-builder";
|
||||
import pluralize from "pluralize";
|
||||
import camelCase from "camelcase";
|
||||
|
||||
type GenerateUseOneSubscriptionParams = {
|
||||
resource: string;
|
||||
meta: MetaQuery;
|
||||
id?: BaseKey;
|
||||
};
|
||||
|
||||
type GenerateUseOneSubscriptionReturnValues = {
|
||||
variables: any;
|
||||
query: string;
|
||||
operation: string;
|
||||
};
|
||||
|
||||
export const generateUseOneSubscription = ({
|
||||
resource,
|
||||
meta,
|
||||
id,
|
||||
}: GenerateUseOneSubscriptionParams): GenerateUseOneSubscriptionReturnValues => {
|
||||
if (!id) {
|
||||
console.error(
|
||||
"[useSubscription]: `id` is required in `params` for graphql subscriptions",
|
||||
);
|
||||
}
|
||||
|
||||
const singularResource = pluralize.singular(resource);
|
||||
const camelResource = camelCase(singularResource);
|
||||
|
||||
const operation = meta.operation ?? camelResource;
|
||||
|
||||
const { query, variables } = gql.subscription({
|
||||
operation,
|
||||
variables: {
|
||||
id: { value: id, type: "ID", required: true },
|
||||
},
|
||||
fields: meta.fields,
|
||||
});
|
||||
|
||||
return { query, variables, operation };
|
||||
};
|
||||
Reference in New Issue
Block a user