【问题标题】:Relay setVariables function fail继电器 setVariables 功能失败
【发布时间】:2017-01-11 06:52:57
【问题描述】:

首先是我的片段:

initialVariables: {
    limit: 3,
  },
  fragments: {
    app: () => Relay.QL`
      fragment on App {
        id
        personnels(first: $limit) {
          pageInfo {
            hasNextPage
            hasPreviousPage
          }
          edges {
            cursor
            node {
              name
            }
          }
        }
      }
    `
   }
  }

从服务器的初始读取工作正常,但是当我打电话时 this.props.relay.setVariables,并尝试设置限制变量我总是得到:

查询App_AppRelayQL的服务器请求失败,原因如下:

  1. globalId 未定义 节点(id:$id_0){ ^^^

在浏览器控制台中。我认为这可能与架构有关。但不确定是什么,所以这是我的架构:

import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLList,
  GraphQLID,
  GraphQLNonNull
} from 'graphql';

import {
  nodeDefinitions,
  fromGlobalId,
  globalIdField,
  connectionDefinitions,
  connectionFromArray,
  connectionArgs,
  mutationWithClientMutationId
} from 'graphql-relay';

class App {};
class Personnel {};
let app = new App();
let Personnels = [];

(() => {
  let Jason = new Personnel();
  let John = new Personnel();

  Jason.name = 'Jason';
  Jason.id = 1;
  John.name = 'John';
  John.id = 2;

  personnels.push(YangGuoRong);
  personnels.push(DengLiFang);
})();


let {nodeInterface, nodeField} = nodeDefinitions(
  (gloablId) => {
    const {type} = fromGlobalId(globalId);

    switch(type) {
      case 'App':
        return app;
      default:
        return null;
    }
  },
  (obj) => {
    if (obj instanceof App) {
      return appType;
    } else if (obj instanceof Personnel) {
      return personnelType;
    } else {
      return null;
    }
  }
);


let getPersonnel = (id) => personnels[id];
let getPersonnels = () => personnels;

let appType = new GraphQLObjectType({
  name: 'App',
  fields: () => ({
    id: globalIdField('App'),
    personnels: {
      type: personnelConnection.connectionType,
      args: connectionArgs,
      resolve: (_, args) => connectionFromArray(personnels, args)
    }
  }),
  interfaces: [nodeInterface]
});

let personnelType = new GraphQLObjectType({
  name: 'Personnel',
  fields: () => ({
    id: {
      type: new GraphQLNonNull(GraphQLID),
      resolve: (obj) => obj.id
    },
    name: {type: GraphQLString},
  }),
});


let personnelConnection = connectionDefinitions({
  name: 'Personnel',
  nodeType: personnelType
});

new GraphQLObjectType({
    name: 'Query',
    fields: {
      node: nodeField,
      app: {
        type: appType,
        resolve: () => app
      },
    }
  }),
});

export default schema;

【问题讨论】:

    标签: javascript graphql relayjs relay


    【解决方案1】:

    您在节点定义中犯了拼写错误(您在第二行写了 gloablId 而不是 globalId)。这就是未定义 globalId 的原因。

    let {nodeInterface, nodeField} = nodeDefinitions(
      (gloablId) => {
        const {type} = fromGlobalId(globalId);
    
        switch(type) {
          case 'App':
            return app;
          default:
            return null;
        }
      },
      (obj) => {
        if (obj instanceof App) {
          return appType;
        } else if (obj instanceof Personnel) {
          return personnelType;
        } else {
          return null;
        }
      }
    );
    

    当这些错误出现时,我总是尝试通过在我的代码中搜索错误中命名的变量来确定错误。这主要有帮助

    【讨论】:

    • 哇,非常感谢。不敢相信我是多么粗心。非常感谢。我认为 relay 和 graphql 都有很多行话,你需要准确输入,如果它们能让这些 API 更灵活,那就太好了。
    • 不客气。我也遇到过很多情况,其中一些继电器或 graphql 的拼写错误会导致错误。所以我认为你是对的。但在这种情况下 globalId 只是一个变量名。你也可以称之为 gloablId ;)
    • 我敢打赌,如果有人可以实现一种智能感知,将所有这些行话整理出来,很多人都会使用它。
    猜你喜欢
    • 2016-11-19
    • 2021-10-05
    • 2018-12-06
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 2017-12-25
    相关资源
    最近更新 更多