Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

one relation appears many times in beforeChange hook #1353

Closed
Olya-Yer opened this issue Jul 3, 2019 · 8 comments
Closed

one relation appears many times in beforeChange hook #1353

Olya-Yer opened this issue Jul 3, 2019 · 8 comments

Comments

@Olya-Yer
Copy link

Olya-Yer commented Jul 3, 2019

in before change hook one object is represented several times

in resolved Data

image

@jesstelford
Copy link
Contributor

jesstelford commented Jul 3, 2019

Thanks for the bug report 👍

Are you able to share your schema & the hook code so we can reproduce the issue?

@Olya-Yer
Copy link
Author

Olya-Yer commented Jul 3, 2019

keystone.createList('Field', {
  schemaDoc: 'field of the table',
  fields: {
    name: { type: Text, schemaDoc: 'this is field of ypur object'},
  },
});

keystone.createList('Object', {
  schemaDoc: 'the table structure',
  fields: {
    name: { type: Text, schemaDoc: 'This is the object/document/table', isUnique: true },
    field: { type: Relationship, ref: 'Field', many: true}
  },
  hooks: {
    beforeChange: async ({
      resolvedData,
      existingItem,
      originalInput,
      context,
      list,
    }) => {
      beforeChange({
        resolvedData,
        existingItem,
        originalInput,
        context,
        list,
      })
    }
  }
});
const beforeChange = ({
  resolvedData,
  existingItem,
  originalInput,
  context,
  list,
}) => {
  console.log('resolvedData',resolvedData)
  console.log('existingItem',existingItem)
  console.log('originalInput',originalInput)
  // console.log('context',context)
  console.log('list',list)
};

@Olya-Yer
Copy link
Author

Olya-Yer commented Jul 3, 2019

the bug appears when you add new field one by one and save after each field is added

@jesstelford
Copy link
Contributor

This looks like an issue with the way nested creates are being handled in the Admin UI.

A complete reproduction:

const { Keystone } = require('@keystone-alpha/keystone');
const { MongooseAdapter } = require('@keystone-alpha/adapter-mongoose');
const { Text, Relationship } = require('@keystone-alpha/fields');
const { GraphQLApp } = require('@keystone-alpha/app-graphql');
const { AdminUIApp } = require('@keystone-alpha/app-admin-ui');

const keystone = new Keystone({
  name: 'test-relationship-app',
  adapter: new MongooseAdapter(),
});

keystone.createList('Field', {
  fields: {
    name: { type: Text },
  },
});

keystone.createList('Object', {
  fields: {
    name: { type: Text, isUnique: true },
    field: { type: Relationship, ref: 'Field', many: true}
  },
  hooks: {
    beforeChange: ({ resolvedData, existingItem }) => {
      console.log('resolvedData',resolvedData)
      console.log('existingItem',existingItem)
    }
  }
});

module.exports = {
  keystone,
  apps: [
    new GraphQLApp(),
    new AdminUIApp({ enableDefaultRoute: true }),
  ],
};

Steps

  1. Visit http://localhost:3000/admin
  2. Create a Field item, 'Bar'
  3. Create a Object item, linked to the 'Bar' Field.
  4. Note the output:
    resolvedData { field: [ '5d1e83daaaa798258fa79c5c' ] } // the `Bar` Field
    existingItem { field: [], _id: 5d1e83d1aaa798258fa79c5b }
    
  5. Click "+" to do a nested create of a new Field item, hit "Create", then on the Object, hit "Save".
  6. Note the output:
    resolvedData { field: [ '5d1e83daaaa798258fa79c5c', '5d1e83daaaa798258fa79c5c', '5d1e83e8aaa798258fa79c5d' ] }
    existingItem { field: [ 5d1e83daaaa798258fa79c5c ], _id: 5d1e83d1aaa798258fa79c5b }
    
  7. The resolvedData now has a duplicate entry for the existing Bar Field item (id 5d1e83daaaa798258fa79c5c).
  8. Note also that the database is storing the item as a duplicate:
    > db.objects.find({})
    { "_id" : ObjectId("5d1e83d1aaa798258fa79c5b"), "field" : [ ObjectId("5d1e83daaaa798258fa79c5c"), ObjectId("5d1e83daaaa798258fa79c5c"), ObjectId("5d1e83e8aaa798258fa79c5d") ], "name" : "Foo", "__v" : 0 }
    

@jesstelford
Copy link
Contributor

May be related to #1185

@Olya-Yer
Copy link
Author

Olya-Yer commented Jul 8, 2019

if you try to delete one of the added items after the first one, the duplicates will multiply.

@Olya-Yer
Copy link
Author

Olya-Yer commented Jul 8, 2019

This seems fixed in latest release: #1366

@jesstelford
Copy link
Contributor

Confirmed, this is now fixed. Thanks for the report @Olya-Yer! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants