Moh*_*nji 6 javascript javascript-objects graphql graphql-js
我想为应用程序使用单个更改将用户信息发送到服务器,然后在输出中获取顶级查询。(我知道这不是一个好的约定,但我想这样做是为了测试我是否可以提高性能)。
因此,只有一个突变会获取用户的信息并返回提要。这种变化更新了在每个查询中作为请求上下文获取的用户信息。上下文用于生成个性化提要。但是,当我调用此更改时,返回的输出是使用旧上下文计算的。我需要做的是更新这个相同突变的上下文。
我放下了代码的简化版本来展示发生了什么:
const UserType = new GraphQLObjectType({
name: 'User',
fields: () => ({
someData: {
type: GraphQLList(Post),
resolve: (user, args, context) => getFeed(context) // context in here is the old context.
},
})
})
const someMutation = mutationWithClientMutationId({
name: 'someMutation',
inputFields: {
location: { type: GraphQLString },
},
outputFields: {
user: {
type: UserType,
resolve: (source, args, context) => getUser(context.location),
},
},
mutateAndGetPayload: async (data, context) => {
updateUserInfo(data)
// I have tried updating context like this but it's not working.
context = { location: data.location }
return {
// I even tried putting user here like this:
// user: getUser(data.location)
// However, the resulting query fails when running getFeed(context)
// the context is still the old context
}
},
})
Run Code Online (Sandbox Code Playgroud)
这就是 JavaScript 的工作原理。您可以重新分配函数参数的值,但这不会更改函数传递的值。
function makeTrue (value) {
value = true
console.log(value) // true
}
var myVariable = false
makeTrue(myVariable)
console.log(myVariable) // false
Run Code Online (Sandbox Code Playgroud)
如果传递给函数的值是对象或数组,则可以对其进行变异,原始值也会发生变异,因为 Javascript 中的对象和数组是通过引用传递的。
function makeItTrue (value) {
value.it = true
console.log(value.it) // true
}
var myVariable = { it: false }
makeTrue(myVariable)
console.log(myVariable.it) // true
Run Code Online (Sandbox Code Playgroud)
换句话说,您需要改变context参数而不是重新分配它。
| 归档时间: |
|
| 查看次数: |
427 次 |
| 最近记录: |