apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

[Feature Request] Create dynamicVariables for mutation MockedProvider #89

Open gabrieluizramos opened 5 years ago

gabrieluizramos commented 5 years ago

Previosly created on react-apollo issue #2466

Intended outcome: Use components at storybook, mocking a response for a mutation based on dynamic variables (e.g: using HoC that updates the variables and refetch the mutation)

Actual outcome: Was throwing an error like no mocked responses for the query

screen shot 2018-10-03 at 11 10 24 am

How to reproduce the issue: Creating a component with MockedProvider and using dynamic values as variables, such as: https://codesandbox.io/s/w27mz5r44w

Version


Maybe, creating some kind of dynamicVariables inside the mocks array for mockedProvider would be a nice way to help these cases, as PR 2467

skratchdot commented 3 years ago

it would be nice if request could be a function (similar to how response can be). if it returns "true" then we return the response, else we look for other mocks. I need to dig up how the code currently works, but I see:

https://www.apollographql.com/docs/react/api/react/testing/#example-mocks-array

const mocks = [
  {
    request: {
      query: GET_DOG,
      variables: { index: 4 }
    },
    result: {
      data: {
        dog: {
          name: "Douglas"
        }
      }
    }
  },
  {
    request: {
      query: GET_DOG,
      variables: { index: 8 }
    },
    error: new Error("Something went wrong")
  }
]

maybe we could make this also work:

const mocks = [
  {
    request: ({ query, variables }) => query === GET_DOG && variables.index === 4,
    result: {
      data: {
        dog: {
          name: "Douglas"
        }
      }
    }
  },
  {
    request: {
      query: GET_DOG,
      variables: { index: 8 }
    },
    error: new Error("Something went wrong")
  }
]

That way we could make either 4 or 5 return the response by editing the code above:

request: ({ query, variables }) => query === GET_DOG && [4, 5].includes(variables.index)
rockettomatooo commented 3 years ago

Any updates on this? This would be a very pleasant feature to have!

My Usecase is storybook: I have a form in storybook and that form triggers a mutation. Right now, I have to exactly match the contents of the form to the mock to make this work. It would be very cool if I could just accept any variables and return them in the response - kind of like an echo server.

Thanks in advance! :) (I don't quite know who to tag here unfortunately :/)

c-sauerborn commented 2 years ago

Hello, we would also be interested in such a feature. We are having the same challenge for our storybook mocks.