ardatan / ardatan/graphql-tools
Bug(mock): Resolver with return type List is not use value from mock resolver.
- Dominant language
- TypeScript
- Stars
- 5.4k
- Forks
- 830
- Avg merge
- 10h 59m
- Merged PRs (30d)
- 45
Description
```
// server.js
const server = new ApolloServer({
...
mockEntireSchema: false,
});
// schema.gql
type User {
id: ObjectId
username: String
nickname: String
}
// mocks.js
export default {
User(){
nickname: 'John'
}
String: ()=>'Bob'
}
// resolvers.js
export default {
Query: {
// Both of query get Object type User with `undefined` value on `nickname` prop
// `user resolver` is working correctly. it return user with nickname `John`
user:(root, {id}, { db })=>db.get('user').find({ id }).value()
// `users resolver` is wrong. it return user with nickname `Bob`
users:(root, args, { db })=>db.get('user').value()
}
```
I found that we can fix this by edit this function:
https://github.com/ardatan/graphql-tools/blob/b8308efe906bfed02391a97c6f6699c66e71f1ff/packages/mock/src/addMocksToSchema.ts#L180-L206
In case of return type `List`, it will go to this line.
https://github.com/ardatan/graphql-tools/blob/b8308efe906bfed02391a97c6f6699c66e71f1ff/packages/mock/src/addMocksToSchema.ts#L205
This mean it will return list value from resolver and ignore list value from mock resolver which is wrong.
We need to merge value from resolver and mock server together and it done.
I add this code above that line and it work!.
```
if(Array.isArray(mockedValue) && Array.isArray(resolvedValue)){
const results = []
resolvedValue.forEach(function(element,index,array) {
const emptyObject = Object.create(Object.getPrototypeOf(resolvedValue[index]));
results.push(copyOwnProps(emptyObject, resolvedValue[index], mockedValue[index]))
})
return results;
}
```
I'm not quite sure. this is the right approach or not.
Hope it help. 😄
Contributor guide
Research direction
Start with packages/mock/src/addMocksToSchema.ts around lines 180-206 and reproduce the schema, mocks, and resolvers shown in the issue. Verify that list-returning resolvers merge resolved values with mock values while preserving the existing singular-object behavior; completion should address the reported nickname discrepancy without regressing other resolver return types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql, typescript
- Domain
- backend-api-design, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100