herbsjs / herbs2gql

Create a GraphQL endpoint based on Entities and Use Cases
MIT License
14 stars 21 forks source link

required usecase-response into usecase2mutation #24

Open italojs opened 3 years ago

italojs commented 3 years ago

Problem

I'm trying to use usecase2mutation with an usecase that don't needs a response, but the usecase2mutation requires it

my usecase:

const useCase = ({ profileRepository }) =>
  usecase('Delete Profile', {
    request: { indexer: String },

    'Update the Profile': step(async ctx => {
      await profileRepository.delete(ctx.req.indexer)
      return Ok()
    })
  })

when I transform it usingusecase2mutation(usecase, defaultResolver(usecase) I got this error:

> herbs-project@1.0.0 start /Users/italojs/dev/herbjs/herbs-cli/lab
> node src/index.js

/Users/italojs/dev/herbjs/herbs-cli/lab/node_modules/herbs2gql/src/usecase2type.js:14
        throw error
        ^

InvalidUseCase: {"response":[{"cantBeEmpty":true},{"cantBeNull":true}]}
    at usecase2type (/Users/italojs/dev/herbjs/herbs-cli/lab/node_modules/herbs2gql/src/usecase2type.js:10:23)
    at usecase2mutation (/Users/italojs/dev/herbjs/herbs-cli/lab/node_modules/herbs2gql/src/usecase2mutation.js:4:12)
    at /Users/italojs/dev/herbjs/herbs-cli/lab/src/infra/api/graphql/mutations.js:5:47
    at Array.map (<anonymous>)
    at Object.factory (/Users/italojs/dev/herbjs/herbs-cli/lab/src/infra/api/graphql/mutations.js:5:32)
    at Object.<anonymous> (/Users/italojs/dev/herbjs/herbs-cli/lab/src/infra/api/graphql/index.js:29:75)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14) {
  invalidArgs: { response: [ { cantBeEmpty: true }, { cantBeNull: true } ] }
}

it says that my usecase needs an response type,I tried use null but I got the same error.

Solution

I believe we can create an scalar Void type by default into the default schema, so we can use it when a usecase return nothing.

Inside useCaseValidator we can remove the line 16 const responseValidation = validate(useCase.responseSchema, defaultValidator). So when we get a usecase without response type into usecaseResponse2gql we can return Void

the final result will be somethinkg like

type Mutation {
  deleteProfile(indexer: string): Void
}

here we have an clear explanation about the solution https://stackoverflow.com/a/61714123

sanderiw commented 2 years ago

I can work on this issue!!! Jose Eduardo will help me on this PR. :-)

sanderiw commented 2 years ago

We've been working on this issue, but we noticed it is a little more complicated than what is described here… Now we`re gonna work on more unit testing. Maybe this PR should be worth some extra points 😃😃😃

italojs commented 2 years ago

@sanderiw are you still working on this issue?