Replies: 2 comments
-
I had the same issue. Turns out that mocks in their current form take two arguments:
The following mocks should log the same way: const mocks1 = {
Query: (query, vars) => ({
myQuery: () => {
console.log(query, vars)
},
})
}
const mocks2 = {
Query: () => ({
myQuery: (query, vars) => {
console.log(query, vars)
},
})
} The following line gave me insight into the api: https://github.com/apollographql/graphql-tools/blob/7eaf4e19f48e85425d45d31830eb607805542fb9/src/mock.ts#L49 This is completely undocumented and I would love confirmation from the maintainers here on whether we can assume this API will remain consistent. |
Beta Was this translation helpful? Give feedback.
-
Hi! Sorry for the almost year late response. The repository has only recently moves to new management. I have not delved too much into the mocking code, but as far as I can tell, mock resolvers take the usual resolver arguments
You can skip to the end for the short answer....which is that I don't think there is anything here (yet) that can do that for you, but we should be able to extract something out of the current code that can. Just to make sure I understand your use case... It sounds to me like you have a client that is firing up queries with arguments specified as variables and you are encountering some sort of block with that, because not having a way to generate valid input variables is preventing you from getting mock data back. Is that about right? Usually mocks I would think are set up to ignore all arguments, because they are defined wherever the type appears. I suppose you may be defining some fields like so:
In the case of The mocking algorithm superimposed on the schema by addMocksToSchema (https://github.com/ardatan/graphql-tools/blob/master/packages/mock/src/mocking.ts#L95-L101), adds a default resolver to every field that does a few additional default things similar to the So, to make a long story even longer, it sounds like you want your mock to actually process the arguments, rather than simply ignore them, maybe something like this:
This might help you mock adding a person, but, of course, it would also mean that to properly get the name of an existing person, you would need to specify arguments with the age on person retrieval, which might not actually be possible. So, you might need to actually provide a fake resolver rather than a type mock, i.e. use
Having said that, back to your question, you want to quickly spin up on the client side some mocking data for some variables. So....the short answer is as promised above....I don't think we have anything for you here (yet). The non-exported 'mockType' helper function (https://github.com/ardatan/graphql-tools/blob/master/packages/mock/src/mocking.ts#L90) creates a new resolver that reads from the passed in and default mocks to generate a value depending on the requested output type. We could probably extract out of that a function that reads from specified/default mocks to return a value and configure it so it would work for input types as well. |
Beta Was this translation helpful? Give feedback.
-
Looking into generating mock data with graphql-code-generator and wondering if there was any way to leverage graphql-tools to generate the input variables so that I could then generate all the mocked responses.
I noticed the
forEachField
was exported but seems to only gives you the scalar types and any custom resolvers you provided itBeta Was this translation helpful? Give feedback.
All reactions