我使用带有猫鼬的graphQL来存储/获取数据。但是问题是,当我从前端请求数据时,查询始终会为变量返回null,但是下面代码段中的结果变量正在正确获取结果,而不是null
new GraphQLObjectType({
name: "RootQueryType",
fields: {
user: {
type: UserType,
args: { username : { type: GraphQLString } },
resolve: async (parent, args) => {
const result = await UserModel.find({username: args.username})
console.log("finding...")
console.log(result)
return result #
}
}
}
})
此查询返回的内容
data : {
data : {
user: {
username: null
user_auth_token: null
}
}
}
但是结果变量具有正确的以下值(出于安全原因而被冠以):
{
username: '<username>',
user_auth_token: '<tokenvalue>'
}
前端查询
const authTokenFetchQuery = (username) => {
return `query {
user(username: "${username}") {
username
user_auth_token
}
}`
}
const GFetcher = ({username}) => {
const [data, setData] = useState({})
const [loading, setLoading] = useState(true)
let fetcher = async () => {
const d = await Axios.post('http://localhost:3000/graphql', {
query: authTokenFetchQuery(username)
})
setData(d)
setLoading(false)
}
useEffect(() => {
if(loading === true) {
fetcher()
}
}, [loading])
console.log(data)
console.log(loading)
return (
<div>
Welcome {username}
</div>
)}