Skip to content

Instantly share code, notes, and snippets.

@SachaG
Created February 18, 2018 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SachaG/ee595912e687bf5e77af905edea65076 to your computer and use it in GitHub Desktop.
Save SachaG/ee595912e687bf5e77af905edea65076 to your computer and use it in GitHub Desktop.

Using Variables (devel branch)

Field-specific queries work by adding “extra” query parts to a specialy created formNewExtraQuery HoC when inserting new documents; or to the withDocument HoC when editing an existing document. In either case, the main query will be called with the following extraTerms argument:

const extraTerms = {
  documentId,
  currentUser,
  view: `${collectionName}ExtraQueryView` // where collectionName is the collection the form belongs to
}

You can use this extraTerms object for more complex data loading strategies. For example, let's imagine you want to load the list of all Photos taken by a user that are associated with the current Articles document.

Since our extraTerms object is hard-coded to expect an ArticlesExtraQueryView view, let's create it. It will take in the extraTerms object and return the appropriate database selector:

Photos.addView('ArticlesExtraQueryView', extraTerms => ({
  selector: {
    userId: extraTerms.currentUser && extraTerms.currentUser._id,
    articleId: extraTerms.documentId,
  }
}));

Note that we add the view to the Photos collection, because that's what we want it to return, but the view is named ArticlesExtraQueryView because the form is for editing a document from the Articles collection.

With the view defined, we can pass extraTerms to our default PhotosList resolver as the terms argument:

featuredPhotoId: {
  type: String,
  control: 'select',
  optional: true,
  insertableBy: ['members'],
  editableBy: ['members'],
  viewableBy: ['guests'],
  query: `
    PhotosList(terms: $extraTerms){
      _id
      url
      height
      width
      credit
    }
  `,
  options: props => props.data.PhotosList.map(photo => ({
      value: photo._id,
      label: photo.name,
  })),
  resolveAs: ...
}

Note that because all extra queries share the same extraTerms object, it's currently not possible to load two different datasets for the same collection inside forms unless you create your own custom resolvers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment