首先,这是我的片段:



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的请求失败,原因如下:


没有定义globalId
 node(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;

最佳答案

您在节点定义中犯了拼写错误(您在第二行中写了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;
    }
  }
);


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

09-25 17:39