中继setVariables函数失败

时间:2016-09-03 07:44:38

标签: javascript graphql relayjs relay

首先,这是我的片段:

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  node(id:$ id_0){  ^^^
  2. 在浏览器控制台中。我认为它可能与架构有关。但不确定是什么,所以这是我的架构:

    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;

1 个答案:

答案 0 :(得分: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;
    }
  }
);

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