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

Add tags to post #1677

Open
mulikaminker opened this issue Jul 25, 2017 · 12 comments
Open

Add tags to post #1677

mulikaminker opened this issue Jul 25, 2017 · 12 comments
Labels

Comments

@mulikaminker
Copy link

mulikaminker commented Jul 25, 2017

Hey, I'm trying to add tags to the post. I followed these instructions but I get an error.

my code:

`import Posts from "meteor/vulcan:posts";
import Tags from 'meteor/vulcan:forms-tags';
import React from 'react';




Posts.addField(
  {
    fieldName: 'description',
    fieldSchema: {
      type: String,
      optional: true,
      max: 3000,
      viewableBy: ['guests'],
      insertableBy: ['members'],
      editableBy: ['members'],
      control: "textarea",
      order: 5
    }
  }
);

Posts.addField({
  fieldName: 'tags',
  fieldSchema: {
    type: Array,
    control: Tags,
    insertableBy: ['members'],
    afterComponent: <a target="_blank" className="suggest-category-link" href="https://github.com/SachaG/SidebarFeedback/issues/1">Suggest new categories</a>
  }
});`

the error:

Error: Type "null" not found in document.

@SachaG
Copy link
Contributor

SachaG commented Jul 25, 2017

You also need to add a tags.$ field. For example:

Posts.addField([
  {
    fieldName: 'categories',
    fieldSchema: {
      type: Array,
      control: "checkboxgroup",
      optional: true,
      insertableBy: ['members'],
      editableBy: ['members'],
      viewableBy: ['guests'],
      form: {
        noselect: true,
        type: "bootstrap-category",
        order: 50,
        options: formProps => getCategoriesAsOptions(formProps.client),
      },
      resolveAs: {
        fieldName: 'categories',
        type: '[Category]',
        resolver: async (post, args, {currentUser, Users, Categories}) => {
          if (!post.categories) return [];
          const categories = _.compact(await Categories.loader.loadMany(post.categories));
          return Users.restrictViewableFields(currentUser, Categories, categories);
        }
      }
    }
  },
  {
    fieldName: 'categories.$',
    fieldSchema: {
      type: String,
      optional: true
    }
  }
]);

@mulikaminker
Copy link
Author

@SachaG, Thanks. My intention was to add tags instead of categories, but together. I want to allow adding a tag to friends too. Currently it allows you to select an existing tag / category only

@eric-burel
Copy link
Contributor

eric-burel commented Jan 11, 2018

Edit : there seems to be working example in the movie example, so I'll check this out. Though error messages are not quite clear in my case, see below.

Hi, adding a schema with a '$' in the name throws an error when generating the GraphQL schema:

"Syntax Error GraphQL request (9:18) Cannot parse the unexpected character ".".

 8: vendingMachineIds{_id}
 9: vendingMachineIds.$
                     ^
10:       }
"

This prevents me from either adding objects or array in the schema whether I use addField or put it directly in the schema.

The tested schema (mostly copied from the example in this issue):

Tours.addField([

  {
    fieldName: 'vendingMachineIds',
    fieldSchema: {
      type: Array,
      label: 'Id des distributeur',
      control: "checkboxgroup",
      editableBy:['admins', 'managers', 'runners'],
      insertableBy:['admins', 'managers', 'runners'],
      viewableBy:['admins', 'managers', 'runners'],
      form: {
        noselect: true,
        type: "bootstrap-category",
        order: 50,
        options: formProps => []
       ,
      },
      resolveAs: {
        fieldName: 'vendingMachine',
        type: 'VendingMachine',
        resolver: (tour, args, context) => {
          if (tour.vendingMachineIds) return []
          const vendingMachines = context.VendingMachines.loader.loadMany(tour.vendingMachineIds)
          return Users.restrictViewableFields(context.currentUser, context.VendingMachines, vendingMachines)
        },
        addOriginalField: true
      }
    }
  },
  {
    fieldName: 'vendingMachineIds.$',
    fieldSchema: {
      type: String,
      optional: true,
      editableBy:['admins'],
      insertableBy:['admins'],
      viewableBy:['admins'],
    }
  },
])

@SachaG
Copy link
Contributor

SachaG commented Jan 12, 2018

That's weird, because field whose names contains $ should be excluded from the GraphQL schema generation process:

https://github.com/VulcanJS/Vulcan/blob/master/packages/vulcan-lib/lib/modules/graphql.js#L134

@eric-burel
Copy link
Contributor

eric-burel commented Jan 12, 2018

Hm thats weird, maybe that's related to the point, e.g there is some split somewhere and only the first part of the fieldName, or the fieldName is never tested ? Would need to test with breakpoints

My guess is that you somehow hop over your check when the schema is an array, or there is an error in my schema which in turn have other things to fail. But it also happens with Object so I don't know...

@SachaG
Copy link
Contributor

SachaG commented Jan 14, 2018

I think at this point the easiest thing is for you to just share your repo and specify some reproduction steps so I can try directly. It's hard to know the source of the issue otherwise.

@eric-burel
Copy link
Contributor

Yep I'll try to repro if I can't fix the schema

@eric-burel
Copy link
Contributor

eric-burel commented Jan 25, 2018

Edit: see next comment instead
Hi, I could not build a repro but here is more feedback.

The schema below does work, I used one field only instead of one field for the Array, as you do in the example-forum for comments in the Posts schema.
However, to get it working I had to do two things:

  • set viewableBy to something, even an empty array.
  • unset editableBy and insertableBy (not even an empty array, a true undefined), which is weird because, well, I'll need to build a form around this

So I guess that when viewableBy is undefined, trying to get the field lead to an error, which is reasonnable. My other guess is that when editableBy or insertableBy is set, the automated graphql mutation builder (or the defaultMutations or whatever makes the Vulcan gql magic happen) try to create a mutation and fails.

That could explain why it is unhappy about the foobar.$ when I try the 2-fields approach.

vendingMachineIds: {
        type: Array,
        optional: true,
        resolveAs: {
            fieldName: 'vendingMachines',
            type: '[VendingMachine]',
            resolver: (refill, args, context) => {

                const machines = refill.vendingMachineIds && refill.vendingMachineIds.length
                    ? context.VendingMachines.find(
                        { _id: { '$in': refill.vendingMachineIds } },
                        {
                            fields: context.VendingMachines.getViewableFields(context.currentUser, context.VendingMachines)
                        }).fetch()
                    : [];

                console.log(machines)
                return machines
            },
            addOriginalField: true,
        }
    },

I am still investigating this right now

@eric-burel
Copy link
Contributor

eric-burel commented Jan 25, 2018

God I think I finally got it!
When you don't provide fields to the SmartForm, it will create inputs for ALL fields, including the foobar.$. It also creates the corresponding gql mutation (however I provided both a mutationFragment and a queryFragment so I am not sure why).

This explains those weird issues I encountered, everything seems to work well now I specified the fields.

So the solution would be to add a small condition on the SmartForm (I can't tell exactly which underlying component is responsible for this yet) that scraps out invalid field names. Seems to be related to this function https://github.com/VulcanJS/Vulcan/blob/master/packages/vulcan-forms/lib/components/Form.jsx#L255

@SachaG
Copy link
Contributor

SachaG commented Jan 29, 2018

If you'd like me to look into this, could you provide the following?

  • A link to a gist of the full schema you're having problems with.
  • What you expect to happen.
  • What happens instead.

@SachaG
Copy link
Contributor

SachaG commented Jan 29, 2018

Also if you'd like to open a more general discussion about how nested fields should be handled, feel free to open a new issue.

@stale
Copy link

stale bot commented Nov 23, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants