relayjs: Attempted to add an ID already in GraphQLSegment:

182 Views Asked by At

I have a react/relay app with the following fragment in my App.js file:

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
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        },
                    },
                },
            }
        `,
    },
});

This is the corresponding part of 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(),
    },
  }),
});

My database.js generates 3 accounts each with 3 classrooms and a random number of enrollments. I've triple-checked the data coming from there for accuracy, and it's fine. Some how though the __dataID__ property for each edge under each classroom duplicates. So, instead of three classrooms the result is that each time it iterates through the map it overwrites the previous classroom with the next in the array and sends a warning: Attempted to add an ID already in GraphQLSegment:

0

There are 0 best solutions below