node.js - GraphQL: Creating and Returning an Object in a Resolver?
问题描述
I've got a mutation resolver that I call directly from the server like this:
import {graphql} from "graphql";
import {CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION} from "../../query-library";
import AllResolvers from "../../resolvers";
import AllSchema from "../../schema";
import {makeExecutableSchema} from "graphql-tools";
const typeDefs = [AllSchema];
const resolvers = [AllResolvers];
const schema = makeExecutableSchema({
typeDefs,
resolvers
});
const {data, errors} = await graphql(
schema,
CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION,
{},
{caller: 'synced-cron'},
{timeStarted: new Date().toISOString().slice(0, 19).replace('T', ' ')}
)
The mutation resolver is called and runs correctly. I don't need it to return anything, but GraphQL throws a warning if it doesn't, so I'd like it to return an object, any object.
So I'm trying it like this:
SCHEMA
cronJobToFindUsersWhoHaveGoneOffline(timeStarted: String): myUserData
QUERY
// note -- no gql. This string is passed directly to function graphql()
// where it gets gql applied to it.
const CRON_JOB_TO_FIND_USERS_WHO_HAVE_GONE_OFFLINE_MUTATION = `
mutation ($timeStarted: String){
cronJobToFindUsersWhoHaveGoneOffline(timeStarted: $timeStarted){
id,
},
}
`;
RESOLVER
cronJobToFindUsersWhoHaveGoneOffline(parent, args, context) {
return Promise.resolve()
.then(() => {
// there is code here that finds users who went offline if any
return usersWhoWentOffline;
})
.then((usersWhoWentOffline) => {
// HERE'S WHERE I HAVE TO RETURN SOMETHING FROM THE RESOLVER
let myUserDataPrototype = {
__typename: 'myUserData',
id: 'not_a_real_id'
}
const dataToReturn = Object.create(myUserDataPrototype);
dataToReturn.__typename = 'myUserData';
dataToReturn.id = 'not_a_real_id';
return dataToReturn; <==GRAPHQL IS NOT HAPPY HERE
})
.catch((err) => {
console.log(err);
});
},
}
GraphQL throws this warning:
data [Object: null prototype] {
cronJobToFindUsersWhoHaveGoneOffline: [Object: null prototype] { id: 'not_a_real_id' } }
errors undefined
I have tried all kinds of different ways to fix this, but I haven't figured out the correct syntax yet.
What is a good way to handle this?
解决方案
这似乎不是一个警告。看起来您正在将结果写入某个地方的控制台。
推荐阅读
- angular - Angular 4错误无法分配给对象'[object Object]'的只读属性'groups'
- c# - 仅使用 caml 查询动态下载文件,而不是通过获取文件夹方法
- ruby-on-rails - 为什么我在使用 accept_nested_attributes_for 时无法保存嵌套属性
- javascript - permissionBlocker 类禁用 IE 上的滚动
- powershell - 如何在正斜杠后删除地址的最后一部分?
- bash - 如何运行 bash 脚本,但跳过最后两行?
- elasticsearch - 当我包含小时和分钟时,Kibana 无法从弹性搜索中获取日期
- php - php cron作业失败但手动工作
- sql - 从函数返回 INSERT 输出
- java - 使用 Hibernate 连接到 MySql 服务器时出现 NoSuchMethodError