Keystone: Create many mutation not connecting relationships

Created on 18 Jun 2019  路  5Comments  路  Source: keystonejs/keystone

When I run a createMany mutation with multiple objects connecting to the same user only one of the objects is added to the users relationship.

All of the posts however have a relationship to the user.

I would expect the user to relate to all of the posts not just one.

   const userId = existingItem._id.toString();

    const posts = postsData.map(post => ({
        data: {
          author: { connect: { id: userId } }
        }
     }));

    await actions.query(
      `mutation CreatePosts($posts: [PostsCreateInput]) {
        createPosts(data: $posts) {
          id
          author {
            id
          }
        }
      }`,
      { variables: { posts } }
    );

hypothesis: this is occurring because the connect runs in parallel and so the last post overrides the previous connect attempts.

This issue should not occur if I was to run multiple createSingle mutations in a loop.

medium 1 Relationship bug graphql needs-review

Most helpful comment

Here are the schemas

const User = {
  fields: {
    name: { type: Text },
    posts: {
      type: Relationship,
      ref: 'Posts.author',
      many: true
    },
  }
}
const Post = {
  fields: {
    name: { type: Text },
    author: {
      type: Relationship,
      ref: 'User.posts',
    },
  }
}

All 5 comments

Are you able to share the schema of the two lists?

Here are the schemas

const User = {
  fields: {
    name: { type: Text },
    posts: {
      type: Relationship,
      ref: 'Posts.author',
      many: true
    },
  }
}
const Post = {
  fields: {
    name: { type: Text },
    author: {
      type: Relationship,
      ref: 'User.posts',
    },
  }
}

Conceptually there's one relationship here but Keystone treats and stores it as two. The expectations around if/when these two sets of relational data are kept in sync isn't clear (to me at least). There are other issues exploring relation aspects of this (eg. #305) but I think the underlying problem is this disconnect between the developer intent and how the relationship is conceptualised by Keystone. See #1925 for more of my thoughts on the matter.

It looks like there hasn't been any activity here in over 6 months. Sorry about that! We've flagged this issue for special attention. It wil be manually reviewed by maintainers, not automatically closed. If you have any additional information please leave us a comment. It really helps! Thank you for you contribution. :)

I have run some experiments and confirmed that the Arcade release has resolved this issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

molomby picture molomby  路  11Comments

jesstelford picture jesstelford  路  19Comments

justinmoon picture justinmoon  路  13Comments

bothwellw picture bothwellw  路  18Comments

thekevinbrown picture thekevinbrown  路  31Comments