Skip to content

Commit

Permalink
MMT-3409: Adds deleteTool mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
macrouch committed Oct 24, 2023
1 parent 7871119 commit 1ab3ea5
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 16 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,28 @@ For all supported arguments and columns, see [the schema](src/types/tool.graphql
}
}

###### Deleting a Tool

mutation DeleteTool(
$providerId: String!
$nativeId: String!
) {
deleteTool(
providerId: $providerId
nativeId: $nativeId
) {
conceptId
revisionId
}
}

variables:

{
"providerId": "EXAMPLE",
"nativeId": "tool-1"
}

#### Variables

For all supported arguments and columns, see [the schema](src/types/variable.graphql).
Expand Down
4 changes: 4 additions & 0 deletions src/cmr/concepts/tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export default class Tool extends Concept {
*/
constructor(headers, requestInfo, params) {
super('tools', headers, requestInfo, params)

const { providerId } = params

this.ingestPath = `providers/${providerId}/tools`
}

/**
Expand Down
109 changes: 101 additions & 8 deletions src/datasources/__tests__/tool.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import nock from 'nock'

import toolDatasource from '../tool'
import { deleteTool as toolSourceDelete, fetchTools as toolSourceFetch } from '../tool'

let requestInfo

describe('tool', () => {
describe('tool#fetch', () => {
const OLD_ENV = process.env

beforeEach(() => {
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('tool', () => {
}]
})

const response = await toolDatasource({}, {
const response = await toolSourceFetch({}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
Expand Down Expand Up @@ -146,7 +146,7 @@ describe('tool', () => {
}]
})

const response = await toolDatasource({}, {
const response = await toolSourceFetch({}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('tool', () => {
}]
})

const response = await toolDatasource({}, {
const response = await toolSourceFetch({}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('tool', () => {
}]
})

const response = await toolDatasource({
const response = await toolSourceFetch({
params: {
concept_id: 'T100000-EDSC'
}
Expand Down Expand Up @@ -287,7 +287,7 @@ describe('tool', () => {
}]
})

const response = await toolDatasource({
const response = await toolSourceFetch({
params: {
concept_id: 'T100000-EDSC'
}
Expand Down Expand Up @@ -318,7 +318,7 @@ describe('tool', () => {
})

await expect(
toolDatasource({
toolSourceFetch({
params: {
conceptId: 'T100000-EDSC'
}
Expand All @@ -331,3 +331,96 @@ describe('tool', () => {
).rejects.toThrow(Error)
})
})

describe('subscription#delete', () => {
const OLD_ENV = process.env

beforeEach(() => {
jest.resetAllMocks()

jest.restoreAllMocks()

process.env = { ...OLD_ENV }

process.env.cmrRootUrl = 'http://example.com'

// Default requestInfo
requestInfo = {
name: 'deleteTool',
alias: 'deleteTool',
args: {
conceptId: 'T100000-EDSC',
nativeId: 'test-guid'
},
fieldsByTypeName: {
ToolMutationResponse: {
conceptId: {
name: 'conceptId',
alias: 'conceptId',
args: {},
fieldsByTypeName: {}
},
revisionId: {
name: 'revisionId',
alias: 'revisionId',
args: {},
fieldsByTypeName: {}
}
}
}
}
})

afterEach(() => {
process.env = OLD_ENV
})

test('returns the CMR results', async () => {
nock(/example/)
.defaultReplyHeaders({
'CMR-Request-Id': 'abcd-1234-efgh-5678'
})
.delete(/ingest\/providers\/EDSC\/tools\/test-guid/)
.reply(201, {
'concept-id': 'T100000-EDSC',
'revision-id': '2'
})

const response = await toolSourceDelete({
nativeId: 'test-guid',
providerId: 'EDSC'
}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
}
}, requestInfo)

expect(response).toEqual({
conceptId: 'T100000-EDSC',
revisionId: '2'
})
})

test('catches errors received from cmrDelete', async () => {
nock(/example/)
.delete(/ingest\/providers\/EDSC\/tools\/test-guid/)
.reply(500, {
errors: ['HTTP Error']
}, {
'cmr-request-id': 'abcd-1234-efgh-5678'
})

await expect(
toolSourceDelete({
nativeId: 'test-guid',
providerId: 'EDSC'
}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
}
}, requestInfo)
).rejects.toThrow(Error)
})
})
23 changes: 22 additions & 1 deletion src/datasources/tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import toolKeyMap from '../utils/umm/toolKeyMap.json'

import Tool from '../cmr/concepts/tool'

export default async (params, context, parsedInfo) => {
export const fetchTools = async (params, context, parsedInfo) => {
const { headers } = context

const requestInfo = parseRequestedFields(parsedInfo, toolKeyMap, 'tool')
Expand All @@ -20,3 +20,24 @@ export default async (params, context, parsedInfo) => {
// Return a formatted JSON response
return tool.getFormattedResponse()
}

export const deleteTool = async (args, context, parsedInfo) => {
const { headers } = context

const requestInfo = parseRequestedFields(parsedInfo, toolKeyMap, 'tool')

const {
ingestKeys
} = requestInfo

const tool = new Tool(headers, requestInfo, args)

// Contact CMR
tool.delete(args, ingestKeys, headers)

// Parse the response from CMR
await tool.parseDelete(requestInfo)

// Return a formatted JSON response
return tool.getFormattedDeleteResponse()
}
6 changes: 4 additions & 2 deletions src/graphql/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ import orderOptionSource from '../datasources/orderOption'
import serviceSource from '../datasources/service'
import serviceDraftSource from '../datasources/serviceDraft'
import toolDraftSource from '../datasources/toolDraft'
import toolSource from '../datasources/tool'
import variableDraftSource from '../datasources/variableDraft'
import variableSource from '../datasources/variable'

import { deleteTool as toolSourceDelete, fetchTools as toolSourceFetch } from '../datasources/tool'

import {
deleteSubscription as subscriptionSourceDelete,
fetchSubscription as subscriptionSourceFetch,
Expand Down Expand Up @@ -136,7 +137,8 @@ export default startServerAndCreateLambdaHandler(
subscriptionSourceFetch,
subscriptionSourceIngest,
toolDraftSource,
toolSource,
toolSourceDelete,
toolSourceFetch,
variableDraftSource,
variableSource
},
Expand Down
8 changes: 6 additions & 2 deletions src/resolvers/__tests__/__mocks__/mockServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import {
fetchSubscription as subscriptionSourceFetch,
ingestSubscription as subscriptionSourceIngest
} from '../../../datasources/subscription'
import toolSource from '../../../datasources/tool'
import {
deleteTool as toolSourceDelete,
fetchTools as toolSourceFetch
} from '../../../datasources/tool'
import toolDraftSource from '../../../datasources/toolDraft'
import variableDraftSource from '../../../datasources/variableDraft'
import variableSource from '../../../datasources/variable'
Expand Down Expand Up @@ -60,7 +63,8 @@ export const buildContextValue = (extraContext) => ({
subscriptionSourceFetch,
subscriptionSourceIngest,
toolDraftSource,
toolSource,
toolSourceDelete,
toolSourceFetch,
variableDraftSource,
variableSource
},
Expand Down
45 changes: 45 additions & 0 deletions src/resolvers/__tests__/tool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,51 @@ describe('Tool', () => {
})
})

describe('Mutation', () => {
test('deleteTool', async () => {
nock(/example/)
.defaultReplyHeaders({
'CMR-Took': 7,
'CMR-Request-Id': 'abcd-1234-efgh-5678'
})
.delete(/ingest\/providers\/EDSC\/tools\/test-guid/)
.reply(201, {
'concept-id': 'T100000-EDSC',
'revision-id': '2'
})

const response = await server.executeOperation({
variables: {
nativeId: 'test-guid',
providerId: 'EDSC'
},
query: `mutation DeleteTool (
$providerId: String!
$nativeId: String!
) {
deleteTool (
providerId: $providerId
nativeId: $nativeId
) {
conceptId
revisionId
}
}`
}, {
contextValue
})

const { data } = response.body.singleResult

expect(data).toEqual({
deleteTool: {
conceptId: 'T100000-EDSC',
revisionId: '2'
}
})
})
})

describe('Tool', () => {
describe('collections', () => {
test('returns collections when querying a published record', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/resolvers/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export default {
}
}

return dataSources.toolSource({
return dataSources.toolSourceFetch({
conceptId: toolConceptIds,
...handlePagingParams(args, tools.length)
}, context, parseResolveInfo(info))
Expand Down
12 changes: 10 additions & 2 deletions src/resolvers/tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ export default {
tools: async (source, args, context, info) => {
const { dataSources } = context

return dataSources.toolSource(handlePagingParams(args), context, parseResolveInfo(info))
return dataSources.toolSourceFetch(handlePagingParams(args), context, parseResolveInfo(info))
},
tool: async (source, args, context, info) => {
const { dataSources } = context

const result = await dataSources.toolSource(args, context, parseResolveInfo(info))
const result = await dataSources.toolSourceFetch(args, context, parseResolveInfo(info))

const [firstResult] = result

return firstResult
}
},

Mutation: {
deleteTool: async (source, args, context, info) => {
const { dataSources } = context

return dataSources.toolSourceDelete(handlePagingParams(args), context, parseResolveInfo(info))
}
},

Tool: {
collections: async (source, args, context, info) => {
const { dataSources } = context
Expand Down
16 changes: 16 additions & 0 deletions src/types/tool.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,19 @@ type Query {
conceptId: String @deprecated(reason: "Use `params.conceptId`")
): Tool
}

type Mutation {
deleteTool (
"Provider ID of the tool."
providerId: String!
"The native id of a tool."
nativeId: String!
): ToolMutationResponse
}

type ToolMutationResponse {
"The unique concept id assigned to the tool."
conceptId: String
"The revision of the tool."
revisionId: String
}

0 comments on commit 1ab3ea5

Please sign in to comment.