GraphQLRange找不到具有光标的段

时间:2016-07-08 21:02:53

标签: graphql relayjs relay graphql-js

我在保存评论变异后收到以下错误

GraphQLRange找不到具有光标的段:MTQ2NzkxNTE5NA ==

这只发生在我在添加新评论之前要求更多评论之后

这是我的createContainer代码:

export default Relay.createContainer(CommentsDetails, {
  initialVariables: {
    count: 10,
  },

  fragments: {
    post: () => Relay.QL`
      fragment on Post {
        id,
        comments(first: $count) {
          edges {
            cursor,
            node {
              createdAt,
              ${Comment.getFragment('comment')},
            }
          },
          pageInfo {
            hasNextPage,
            hasPreviousPage,
            startCursor,
            endCursor,
          },
        }
      },
    `,
  },
});

这是我的保存评论变异

import Relay from 'react-relay';

export default class SaveCommentMutation extends Relay.Mutation {
  getMutation() {
    return Relay.QL `mutation {
      saveComment
    }`;
  }

  getVariables() {
    const { post, body } = this.props;
    console.log('variables: ', post, body, this.props);
    return {
      post_id: post.id,
      body,
    }
  }

  getFatQuery() {
    return Relay.QL`
      fragment on saveCommentPayload {
        commentEdge,
        post {
          id,
          comments,
        },
        error,
      }
    `;
  }
  getConfigs() {
    return [
      {
        type: 'FIELDS_CHANGE',
        fieldIDs: {
          post: this.props.post.id,
        },
      },
      {
        type: 'RANGE_ADD',
        parentName: 'post',
        parentID: this.props.post.id,
        connectionName: 'comments',
        edgeName: 'commentEdge',
        rangeBehaviors: {
          '': 'append',
          'orderby(createdAt)': 'append',
      },
    }];
  }

  getOptimisticResponse() {
    const { post, body } = this.props;

    return {
      post: {
        id: post.id,
      },
      commentEdge: {
        node: {
          body: body,
          createdAt: new Date().toUTCString(),
          user: {
            name: 'anonymous',
          }
        }
      }
    };
  }
}

这是生成的graphql查询,如果我尝试使用graphql

,它就可以工作
mutation SaveCommentMutation($input_0:saveCommentInput!) {
  saveComment(input:$input_0) {
    clientMutationId,
    ...F5,
    ...F6
  }
}
fragment F0 on Post {
  id
}
fragment F1 on Comment {
  id,
  body,
  createdAt,
  user {
    username,
    name,
    id
  }
}
fragment F2 on Post {
  id,
  _comments2g8zcs:comments(first:10) {
    edges {
      cursor,
      node {
        createdAt,
        id,
        ...F1
      }
    },
    pageInfo {
      hasNextPage,
      hasPreviousPage,
      startCursor,
      endCursor
    }
  }
}
fragment F3 on Post {
  id,
  _comments3Cdb4n:comments(after:"MTQ2NzkyMDExNQ==",first:10) {
    edges {
      cursor,
      node {
        createdAt,
        id,
        ...F1
      }
    },
    pageInfo {
      hasNextPage,
      hasPreviousPage,
      startCursor,
      endCursor
    }
  }
}
fragment F4 on Post {
  id,
  _comments1Z6zk7:comments(after:"MTQ2NzkxNTE5NA==",first:10) {
    edges {
      cursor,
      node {
        createdAt,
        id,
        ...F1
      }
    },
    pageInfo {
      hasNextPage,
      hasPreviousPage,
      startCursor,
      endCursor
    }
  }
}
fragment F5 on saveCommentPayload {
  post {
    id,
    ...F0,
    id,
    id,
    ...F2,
    ...F3,
    ...F4
  }
}
fragment F6 on saveCommentPayload {
  commentEdge {
    cursor,
    __typename,
    node {
      createdAt,
      id,
      body,
      user {
        username,
        name,
        id
      }
    }
  },
  post {
    id,
    ...F0,
    id,
    id
  }
}

我认为发生这种情况是因为它无法找到应该附加新创建的评论的正确评论列表

0 个答案:

没有答案