错误:UserCreatePayload.viewer字段类型必须是输出类型,但得到:undefined

时间:2017-04-07 16:56:01

标签: reactjs graphql relay

当整个架构位于单个文件中时,下面的代码有效,但是当我尝试将其拆分为单个文件时,我收到了上述错误。

我导入了所有类型和功能。

我必须添加更多细节,但我不知道该说些什么。我认为这是一个排序问题,因为它在单个文件中工作,但不能分开。

非常感谢。

const UserCreateMutation = mutationWithClientMutationId({
  name: 'UserCreate',
  inputFields: {
    email: {type: new GraphQLNonNull(GraphQLString)},
    password: {type: new GraphQLNonNull(GraphQLString)}
  },
  outputFields: {
    viewer: {
      type: viewerType,
      resolve() {
        return viewerGet();
      }
    },
    field: {
      type: userType,
      resolve(node) {
        return node;
      }
    }
  },
  async mutateAndGetPayload({email, password}, {db, req}) {
export const viewerType = new GraphQLObjectType({
  name: 'Viewer',
  fields() {
    return {
      id: globalIdField('Viewer', ({_id: viewerLocalId}) => {
        return viewerLocalId;
      }),
      _id: {type: GraphQLID},
      user: {
        type: userType,
        resolve(parent, args, {req: {user}}) {
          return user || {};
        }
      },
      profile: {
        type: profileConnectionType,
        args: {
          id: {type: GraphQLID},
          searchTerm: {type: GraphQLString},
          ...connectionArgs
        },
        resolve(parent, {id: profileGlobalId, searchTerm, ...connectionArgs}, {db}) {
          const query = (() => {
            const q = {};

            if (profileGlobalId) {
              const {id: profileLocalId} = fromGlobalId(profileGlobalId);

              Object.assign(
                q,
                {_id: new ObjectID(profileLocalId)}
              );
            }

            if (searchTerm) {
              Object.assign(
                q,
                {
                  $text: {
                    $search: `\"${searchTerm}\"`
                  }
                }
              );
            }

            return q;
          })();
          const sort = {_id: -1};
          const limit = 0;

          return connectionFromPromisedArray(
            promisedArrayGet(
              query,
              sort,
              limit,
              profileCollectionName,
              db
            ),
            connectionArgs
          );
        }
      }
    };
  },
  interfaces: [nodeInterface]
});
class Viewer extends Object {}
export const viewerGet = () => {
  return Object.assign(
    new Viewer(),
    {
      _id: 'Viewer'
    }
  );
};

import {viewerType,userType,viewerGet}

1 个答案:

答案 0 :(得分:0)

不确定这是否是问题,但有时模块加载顺序是个问题。如果是问题,您可以通过使outputFields成为 thunk 来解决它,即返回对象而不是普通对象的函数。

outputFields: () => ({
  viewer: {
    type: viewerType,
    resolve() {
      return viewerGet();
    }
  },
  field: {
    type: userType,
    resolve(node) {
      return node;
    }
  }
}),