具有Keystone的GraphQL希望字段类型必须是输出类型,但得到:undefined

时间:2017-04-24 16:28:16

标签: express graphql keystonejs

我不想为我的keystone.js应用程序制作GraphQL端点(通过快速中间件express-graphql)。

这是我的Keystone Schema:

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();

字段Photo的关键问题,在keystone.js中实现为文件类型。

在我的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, });

Field File实现,包含在forked 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包发出pull请求,以便它正常工作。此刻我有来自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 个答案:

答案 0 :(得分:0)

此刻我绕过了这个:

export const File = new GraphQLObjectType({
  name: 'KeystoneFileType',
  fields: {
    size: { type: GraphQLInt },
    filename: { type: GraphQLString },
    mimetype: { type: GraphQLString },
  },
});

所以此时必须没有(字段)=&gt; FileType,正如我所见,但如果有人能找到真正的问题,我会很高兴。