diff --git a/public/app/features/explore/QueryLibrary/QueryTemplateForm.tsx b/public/app/features/explore/QueryLibrary/QueryTemplateForm.tsx index 7ee55018f35..6b0a2e99fe5 100644 --- a/public/app/features/explore/QueryLibrary/QueryTemplateForm.tsx +++ b/public/app/features/explore/QueryLibrary/QueryTemplateForm.tsx @@ -10,8 +10,8 @@ import { Input } from '@grafana/ui/src/components/Input/Input'; import { Trans, t } from 'app/core/internationalization'; import { getQueryDisplayText } from 'app/core/utils/richHistory'; import { useAddQueryTemplateMutation, useEditQueryTemplateMutation } from 'app/features/query-library'; -import { DataQuerySpec } from 'app/features/query-library/api/types'; -import { AddQueryTemplateCommand } from 'app/features/query-library/types'; +import { DataQueryFullSpec } from 'app/features/query-library/api/types'; +import { AddQueryTemplateCommand, EditQueryTemplateCommand } from 'app/features/query-library/types'; import { useDatasource } from '../QueryLibrary/utils/useDatasource'; @@ -77,8 +77,8 @@ export const QueryTemplateForm = ({ onCancel, onSave, queryToAdd, templateData } }); }; - const handleEditQueryTemplate = async (EditQueryTemplateCommand: DataQuerySpec) => { - return editQueryTemplate(EditQueryTemplateCommand) + const handleEditQueryTemplate = async (editQueryTemplateCommand: EditQueryTemplateCommand) => { + return editQueryTemplate(editQueryTemplateCommand) .unwrap() .then(() => { getAppEvents().publish({ @@ -103,11 +103,8 @@ export const QueryTemplateForm = ({ onCancel, onSave, queryToAdd, templateData } const temporaryDefaultTitle = data.description || t('explore.query-library.default-description', 'Public', { timestamp: timestamp }); - if (templateData?.fullSpec) { - handleEditQueryTemplate({ - ...templateData.fullSpec, - spec: { ...templateData.fullSpec.spec, title: data.description }, - }).then((isSuccess) => { + if (templateData?.uid) { + handleEditQueryTemplate({ uid: templateData.uid, partialSpec: { title: data.description } }).then((isSuccess) => { onSave(isSuccess); }); } else if (queryToAdd) { diff --git a/public/app/features/explore/QueryLibrary/QueryTemplatesTable/types.ts b/public/app/features/explore/QueryLibrary/QueryTemplatesTable/types.ts index 99a98a97697..24ab20d15a4 100644 --- a/public/app/features/explore/QueryLibrary/QueryTemplatesTable/types.ts +++ b/public/app/features/explore/QueryLibrary/QueryTemplatesTable/types.ts @@ -1,5 +1,5 @@ import { DataQuery, DataSourceRef } from '@grafana/schema'; -import { DataQuerySpec } from 'app/features/query-library/api/types'; +import { DataQueryFullSpec } from 'app/features/query-library/api/types'; export type QueryTemplateRow = { index: string; @@ -10,5 +10,5 @@ export type QueryTemplateRow = { createdAtTimestamp?: number; user?: string; uid?: string; - fullSpec?: DataQuerySpec; + fullSpec?: DataQueryFullSpec; }; diff --git a/public/app/features/query-library/api/factory.ts b/public/app/features/query-library/api/factory.ts index 6fd2d0011ab..8496df9731e 100644 --- a/public/app/features/query-library/api/factory.ts +++ b/public/app/features/query-library/api/factory.ts @@ -1,10 +1,10 @@ import { createApi } from '@reduxjs/toolkit/query/react'; -import { AddQueryTemplateCommand, DeleteQueryTemplateCommand, QueryTemplate } from '../types'; +import { AddQueryTemplateCommand, DeleteQueryTemplateCommand, EditQueryTemplateCommand, QueryTemplate } from '../types'; import { convertAddQueryTemplateCommandToDataQuerySpec, convertDataQueryResponseToQueryTemplates } from './mappers'; import { baseQuery } from './query'; -import { DataQuerySpec } from './types'; +import { DataQueryFullSpec, DataQueryPartialSpec } from './types'; export const queryLibraryApi = createApi({ baseQuery, @@ -29,11 +29,11 @@ export const queryLibraryApi = createApi({ }), invalidatesTags: ['QueryTemplatesList'], }), - editQueryTemplate: builder.mutation({ + editQueryTemplate: builder.mutation({ query: (editQueryTemplateCommand) => ({ - url: `${editQueryTemplateCommand.metadata.name}`, - method: 'PUT', - data: editQueryTemplateCommand, + url: `${editQueryTemplateCommand.uid}`, + method: 'PATCH', + data: [{ op: 'replace', path: '/spec/title', value: editQueryTemplateCommand.partialSpec.title }], }), invalidatesTags: ['QueryTemplatesList'], }), diff --git a/public/app/features/query-library/api/mappers.ts b/public/app/features/query-library/api/mappers.ts index 08bd081ed13..cbeb728ff9f 100644 --- a/public/app/features/query-library/api/mappers.ts +++ b/public/app/features/query-library/api/mappers.ts @@ -1,7 +1,7 @@ import { AddQueryTemplateCommand, QueryTemplate } from '../types'; import { API_VERSION, QueryTemplateKinds } from './query'; -import { CREATED_BY_KEY, DataQuerySpec, DataQuerySpecResponse, DataQueryTarget } from './types'; +import { CREATED_BY_KEY, DataQueryFullSpec, DataQuerySpecResponse, DataQueryTarget } from './types'; export const parseCreatedByValue = (value?: string) => { // https://github.com/grafana/grafana/blob/main/pkg/services/user/identity.go#L194 @@ -43,7 +43,7 @@ export const convertDataQueryResponseToQueryTemplates = (result: DataQuerySpecRe export const convertAddQueryTemplateCommandToDataQuerySpec = ( addQueryTemplateCommand: AddQueryTemplateCommand -): DataQuerySpec => { +): DataQueryFullSpec => { const { title, targets } = addQueryTemplateCommand; return { apiVersion: API_VERSION, diff --git a/public/app/features/query-library/api/types.ts b/public/app/features/query-library/api/types.ts index bf10cbc936e..bfd4af997e0 100644 --- a/public/app/features/query-library/api/types.ts +++ b/public/app/features/query-library/api/types.ts @@ -6,6 +6,14 @@ export type DataQueryTarget = { }; export type DataQuerySpec = { + title: string; + vars: object[]; // TODO: Detect variables in #86838 + targets: DataQueryTarget[]; +}; + +// TODO : change put in API to PATCH and use DataQuerySpec instead of the full spec to try to not have shenanigans + +export type DataQueryFullSpec = { apiVersion: string; kind: string; metadata: { @@ -14,16 +22,14 @@ export type DataQuerySpec = { creationTimestamp?: string; annotations?: { [key: string]: string }; }; - spec: { - title: string; - vars: object[]; // TODO: Detect variables in #86838 - targets: DataQueryTarget[]; - }; + spec: DataQuerySpec; }; +export type DataQueryPartialSpec = Partial; + export type DataQuerySpecResponse = { apiVersion: string; - items: DataQuerySpec[]; + items: DataQueryFullSpec[]; }; export const CREATED_BY_KEY = 'grafana.app/createdBy'; diff --git a/public/app/features/query-library/types.ts b/public/app/features/query-library/types.ts index 274857de25d..f38475e4b7f 100644 --- a/public/app/features/query-library/types.ts +++ b/public/app/features/query-library/types.ts @@ -1,6 +1,6 @@ import { DataQuery } from '@grafana/schema'; -import { DataQuerySpec } from './api/types'; +import { DataQueryFullSpec, DataQueryPartialSpec } from './api/types'; export type QueryTemplate = { uid: string; @@ -8,7 +8,7 @@ export type QueryTemplate = { targets: DataQuery[]; createdAtTimestamp: number; user?: string; - fullSpec: DataQuerySpec; + fullSpec: DataQueryFullSpec; }; export type AddQueryTemplateCommand = { @@ -16,6 +16,11 @@ export type AddQueryTemplateCommand = { targets: DataQuery[]; }; +export type EditQueryTemplateCommand = { + uid: string; + partialSpec: DataQueryPartialSpec; +}; + export type DeleteQueryTemplateCommand = { uid: string; };