relayjs:尝试在GraphQLSegment中添加一个ID:

时间:2016-12-16 14:32:05

标签: reactjs graphql relayjs

我的App.js文件中有一个带有以下片段的react / relay应用程序:

export default Relay.createContainer(App, {
    fragments: {
        viewer: () => Relay.QL`
            fragment on User {
                accounts(first: 10) {
                    edges {
                        node {
                            id,
                            name,
                            sfid,
                            classrooms(first:10) {
                                edges {
                                    node {
                                        id,
                                        clid,
                                        course,
                                        enrollments(first:10) {
                                            edges {
                                                node {
                                                    id,
                                                    enid
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        },
                    },
                },
            }
        `,
    },
});

这是schema.js的相应部分:

var userType = new GraphQLObjectType({
  name: 'User',
  description: 'A person who uses our app',
  fields: () => ({
    id: globalIdField('User'),
    accounts: {
      type: accountConnection,
      description: 'A user\'s account connections',
      args: connectionArgs,
      resolve: (_, args) => connectionFromArray(getAccounts(), args),
    },
  }),
  interfaces: [nodeInterface],
});

var accountType = new GraphQLObjectType({
  name: 'Account',
  description: 'A school account',
  fields: () => ({
    id: globalIdField('Account'),
    name: {
      type: GraphQLString,
      description: 'The name of the account',
    },
    sfid: {
      type: GraphQLString
    },
    classrooms: {
      type: classroomConnection,
      description: 'The classrooms at an account',
      args: connectionArgs,
      resolve: (account, args) => connectionFromArray(account.classrooms.map((classroom) => getClassroom(classroom.clid)), args)
    }
  }),
  interfaces: [nodeInterface],
});

var classroomType = new GraphQLObjectType({
  name: 'Classroom',
  description: 'A classroom in a school',
  fields: () => ({
    id: globalIdField('Classroom'),
    clid: {
      type: GraphQLInt,
      description: 'id of classroom in db',
      resolve: (classroom) => classroom.clid
    },
    course: {
      type: GraphQLString,
      description: 'The course assigned to this classroom.'
    },
    enrollments: {
      type: enrollmentConnection,
      description: 'The students in a classroom',
      args: connectionArgs,
      resolve: (classroom, args) => connectionFromArray(classroom.enrollments.map((enrollment) => getEnrollment(enrollment.enid)), args)
    }
  }),
  interfaces: [nodeInterface],
});

var enrollmentType = new GraphQLObjectType({
  name: 'Enrollment',
  description: 'A student enrolled in a classroom',
  fields: () => ({
    id: globalIdField('Enrollment'),
    enid: {
      type: GraphQLInt,
      description: 'id of enrollment in db',
      resolve: (enrollment) => enrollment.enid
    }
  }),
  interfaces: [nodeInterface],
});

/**
 * Define your own connection types here
 */
var {connectionType: accountConnection} =
  connectionDefinitions({name: 'Account', nodeType: accountType});

var {connectionType: classroomConnection} =
  connectionDefinitions({name: 'Classroom', nodeType: classroomType});

var {connectionType: enrollmentConnection} =
  connectionDefinitions({name: 'Enrollment', nodeType: enrollmentType});

/**
 * This is the type that will be the root of our query,
 * and the entry point into our schema.
 */
var queryType = new GraphQLObjectType({
  name: 'Query',
  fields: () => ({
    node: nodeField,
    // Add your own root fields here
    viewer: {
      type: userType,
      resolve: () => getViewer(),
    },
  }),
});

我的database.js生成3个帐户,每个帐户有3个教室和一个随机数量的注册。为了准确,我已经对来自那里的数据进行了三次检查,这很好。虽然每个__dataID__下每个edge的{​​{1}}属性重复,但有些怎样。因此,不是三个教室,结果是每次迭代遍历地图时,它会覆盖前一个教室和阵列中的下一个教室并发出警告:classroom

0 个答案:

没有答案