带有 Keystone 的 GraphQL 需要字段类型必须是输出类型,但得到:未定义
Posted
技术标签:
【中文标题】带有 Keystone 的 GraphQL 需要字段类型必须是输出类型,但得到:未定义【英文标题】:GraphQL with Keystone wants field type must be Output Type but got: undefined 【发布时间】:2017-09-21 10:40:37 【问题描述】:我不想为我的 keystone.js 应用程序创建 GraphQL 端点(通过 express 中间件 express-graphql)。
这是我的 Keystone 架构:
const keystone = require('keystone');
const Types = keystone.Field.Types;
const User = new keystone.List('User');
const localStorage = new keystone.Storage(
adapter: keystone.Storage.Adapters.FS,
fs:
path: './src/upload/avatars',
publicPath: '/upload/avatars',
,
);
User.add(
name: type: Types.Name, required: true, index: true ,
email: type: Types.Email, initial: true, index: true ,
password: type: Types.Password, initial: true, required: true ,
, 'Profile',
photo: type: Types.File, storage: localStorage ,
, 'Permissions',
isAdmin: type: Boolean, label: 'Can access Keystone', index: true ,
,
);
// Provide access to Keystone
User.schema.virtual('canAccessKeystone').get(function ()
return this.isAdmin;
);
User.defaultColumns = 'name, email, isAdmin';
User.register();
keystone.js 中作为文件类型实现的字段 Photo 的关键问题。
在我的 GraphQL 架构下:
import GraphQLSchema, GraphQLID, GraphQLList, GraphQLNonNull, GraphQLObjectType, from 'graphql';
const keystoneTypes = require('keystone-graphql').Types; const keystone = require('keystone');
const User = keystone.list('User');
const userType = new GraphQLObjectType( name: 'User', fields: ()
=> (
id: type: new GraphQLNonNull(GraphQLID) ,
name: type: new GraphQLNonNull(keystoneTypes.Name(User.fields.name)) ,
email: keystoneTypes.Email(User.fields.email),
photo: keystoneTypes.File(User.fields.photo), ), );
const queryRootType = new GraphQLObjectType( name: 'Query', fields:
users:
type: new GraphQLList(userType),
resolve: () =>
User.model.find().exec(),
,
user:
type: userType,
args:
id:
description: 'id of the user',
type: new GraphQLNonNull(GraphQLID),
,
,
resolve: (_, args) => User.model.findById(args.id).exec(),
, , );
export default new GraphQLSchema( query: queryRootType, );
以及包含在分叉 keystone-graphql 包中的字段文件实现:
'use strict';
const GraphQL = require('graphql');
const KeystoneFileType = new GraphQL.GraphQLObjectType(
name: 'KeystoneFile',
fields:
size: type: GraphQL.GraphQLInt ,
mimetype: type: GraphQL.GraphQLString ,
filename: type: GraphQL.GraphQLString ,
,
);
module.exports = (field) => KeystoneFileType;
我想用 File 字段向 keystone-graphql 包发出拉取请求,当它可以正常工作时。目前我有来自 GraphQL 的错误:
Error: User.photo field type must be Output Type but got: undefined.
at invariant (/Users/Vadim/Dropbox/WebStormProjects/mulibwanji/node_modules/graphql/jsutils/invariant.js:19:11)
at /Users/Vadim/Dropbox/WebStormProjects/mulibwanji/node_modules/graphql/type/definition.js:335:5
at Array.forEach (native)
at defineFieldMap (/Users/Vadim/Dropbox/WebStormProjects/mulibwanji/node_modules/graphql/type/definition.js:326:14)
at GraphQLObjectType.getFields (/Users/Vadim/Dropbox/WebStormProjects/mulibwanji/node_modules/graphql/type/definition.js:284:44)
at typeMapReducer (/Users/Vadim/Dropbox/WebStormProjects/mulibwanji/node_modules/graphql/type/schema.js:206:25)
at typeMapReducer (/Users/Vadim/Dropbox/WebStormProjects/mulibwanji/node_modules/graphql/type/schema.js:187:12)
at /Users/Vadim/Dropbox/WebStormProjects/mulibwanji/node_modules/graphql/type/schema.js:216:20
at Array.forEach (native)
at typeMapReducer (/Users/Vadim/Dropbox/WebStormProjects/mulibwanji/node_modules/graphql/type/schema.js:207:27)
at Array.reduce (native)
at new GraphQLSchema (/Users/Vadim/Dropbox/WebStormProjects/mulibwanji/node_modules/graphql/type/schema.js:95:34)
at Object.<anonymous> (/Users/Vadim/Dropbox/WebStormProjects/mulibwanji/graphql/Schema.js:58:16)
【问题讨论】:
【参考方案1】:此时我绕过了这个:
export const File = new GraphQLObjectType(
name: 'KeystoneFileType',
fields:
size: type: GraphQLInt ,
filename: type: GraphQLString ,
mimetype: type: GraphQLString ,
,
);
所以此时它必须没有 (field) => FileType,如我所见,但如果有人能找出真正的问题,我会很高兴。
【讨论】:
以上是关于带有 Keystone 的 GraphQL 需要字段类型必须是输出类型,但得到:未定义的主要内容,如果未能解决你的问题,请参考以下文章
无法理解使用带有 graphql-ruby gem 的 ActionCable 的 GraphQL 订阅
如何将图像上传到 KeystoneJS GraphQL 端点?
如何在 Android 中发送带有片段的 graphql 查询
如何在 neo4j-graphql-js 端点中使用带有 nestjs 框架的拦截器?