【问题标题】:My query is failing in relay and I don't know why?我的查询在中继中失败,我不知道为什么?
【发布时间】:2017-07-02 01:14:22
【问题描述】:

我有一个简单的查询,它在我的 Graphql 中运行良好,但我无法使用中继将数据传递给组件,我不知道为什么:(

{
  todolist { // todolist returns array of objects of todo
    id
    text
    done
  }
}

这是我尝试使用中继在组件中传递数据的代码:

class TodoList extends React.Component {
  render() {
    return <ul>
      {this.props.todos.todolist.map((todo) => {
        <Todo todo={todo} />
      })}
    </ul>;
  }
}
export default Relay.createContainer(TodoList, {
  fragments: {
    todos: () => Relay.QL`
      fragment on Query {
        todolist {
          id
          text
          done
        } 
      }
    `,
  },
});

最后是我的架构

const Todo = new GraphQLObjectType({
    name: 'Todo',
    description: 'This contains list of todos which belong to its\' (Persons)users',
    fields: () => {
        return {
            id: {
                type: GraphQLInt,
                resolve: (todo) => {
                    return todo.id;
                }
            },
            text: {
                type: GraphQLString,
                resolve: (todo) => {
                    return todo.text;
                }
            },
            done: {
                type: GraphQLBoolean,
                resolve: (todo) => {
                    return todo.done;
                }
            },
        }
    }
});

const Query = new GraphQLObjectType({
    name: 'Query',
    description: 'This is the root query',
    fields: () => {
        return {
            todolist: {
                type: new GraphQLList(Todo),
                resolve: (root, args) => {
                    return Conn.models.todo.findAll({ where: args})
                }
            }
        }
    }
});

这段代码看起来很简单,我看不出为什么它不起作用,我有这个错误Uncaught TypeError: Cannot read property 'todolist' of undefined,但是我配置了 todolist,我可以在我的 graphql 中查询,你可以看到查询的结构是一样的,我不知道为什么这不起作用?

【问题讨论】:

    标签: reactjs relayjs graphql-js


    【解决方案1】:

    todolist 应该是Query 上的连接类型。此外,您的 id 应该是 Relay 全局 ID。您将无法访问 Relay 中对象的原始原生 id 字段。

    import {
      connectionArgs,
      connectionDefinitions,
      globalIdField,
    } from 'graphql-relay';
    
    // I'm renaming Todo to TodoType
    const TodoType = new GraphQLObjectType({
      ...,
      fields: {
        id: uidGlobalIdField('Todo'),
        ...
      },
    });
    
    const {
      connectionType: TodoConnection,
    } = connectionDefinitions({ name: 'Todo', nodeType: TodoType });
    
    // Also renaming Query to QueryType
    const QueryType = new GraphQLObjectType({
      ...,
      fields: {
        id: globalIdField('Query', $queryId), // hard-code queryId if you only have one Query concept (Facebook thinks of this top level field as being a user, so the $queryId would be the user id in their world)
        todos: { // Better than todoList; generally if it's plural in Relay it's assumed to be a connection or list
          type: TodoConnection,
          args: connectionArgs,
        },
      },
    });
    
    // Now, to be able to query off of QueryType
    const viewerDefaultField = {
      query: { // Normally this is called `viewer`, but `query` is ok (I think)
        query: QueryType,
        resolve: () => ({}),
        description: 'The entry point into the graph',
      }
    };
    
    export { viewerDefaultField };
    

    以上内容并不完整(您可能还需要在一种或多种类型上设置节点接口,这需要节点定义),但它应该回答您的基本问题并帮助您入门。

    学习是一个巨大的痛苦,但是一旦你努力克服它,它就会开始变得有意义,并且你会开始喜欢它而不是 RESTful 调用。

    【讨论】:

    • 我看不懂你的大部分代码,你能给我推荐一些资源来阅读吗?官方教程太难了:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2014-05-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    相关资源
    最近更新 更多