Title
m

max

11/03/2017, 4:21 PM
Has anyone had the following error on a function activated following a create subscription?
You are referencing a node that does not exist.
It's almost like it's too quick and the node doesn't yet exist. As way of checking I completed my mutation in the playground and it worked.
n

nilan

11/03/2017, 4:27 PM
what does your function do?
m

max

11/03/2017, 4:34 PM
Create the equivalent of DocumentAccess as referenced in this post https://www.graph.cool/forum/t/help-on-permission-filters/1191/12
n

nilan

11/03/2017, 4:35 PM
what would that look like in very specific terms?
m

max

11/03/2017, 4:35 PM
It links a newly created Node (referencing the ID supplied by the create subscription)
n

nilan

11/03/2017, 4:36 PM
and that is in a subscription function?
m

max

11/03/2017, 4:36 PM
subscription {
  Document(filter: {
    mutation_in: [CREATED]
  }) {
    updatedFields
    node {
      id
      name
      owner {
        id
        name
      }
    }
  }
}
It takes the id of the created node and the id of the user from the owner. Then it creates a DocumentAccess node
'use latest'

// on subscription to CREATED document

const { fromEvent } = require('graphcool-lib')

// replace with your root token and project id
const rootToken = process.env['ROOT_TOKEN'];
const serviceId = process.env['SERVICE_ID'];

export default (event) => {
  // augment event context with project id and root token
  event.context = {
    graphcool: {
      rootToken,
      serviceId
    }
  }
  
  // create simple api client
  const api = fromEvent(event).api('simple/v1');
  
  // graphQL mutation
  const createDocumentAccess = `
    mutation createDocumentAccess(
      $access: DocumentAccessType!
      $documentId: ID!
      $userId: ID!
    ) {
      createDocumentAccess(
        access: $access
        documentId: $documentId
        userId: $userId
      ) {
        id
        access
        document {
          id
          name
        }
        user {
          id
          name
        }
      }
    }
  `;
  
  const variables = {
    access: 'ADMIN',
    documentId: event.data.Document.node.id,
    userId: event.data.Document.node.owner.id
  }

  // Return a Promise to wait for the mutation to complete
  return new Promise((resolve,reject) => {
  	api.request(createDocumentAccess, variables)
  	  .then(data => resolve({ data: event.data }))
      .catch(err => resolve({ error: err}))
    })
}
n

nilan

11/03/2017, 4:43 PM
thanks, I agree, this looks like a timing issue. is it correct that this only happens sometimes, not always?
please create a new issue on Github: github.com/graphcool/framework 🙂
m

max

11/03/2017, 4:45 PM
I just switched from the GC GUI to the CLI. It used to always work under the GUI function and now it doesn't work. I've only tried it today. Maybe I could try setting a timeout
I tried a setTimeOut but nothing happens not even in the logs... not sure why
setTimeout( () => {
      return new Promise((resolve,reject) => {
        api.request(createFundAccess, variables)
          .then(data => resolve({ data: event.data }))
          .catch(err => resolve({ error: err}))
        })
    }, 1000);
a

angly

11/03/2017, 5:06 PM
@max What is
'use latest'
, btw?
n

nilan

11/03/2017, 5:07 PM
it's not needed for framework functions, it's somsething specific to functions in legacy services
a

angly

11/03/2017, 5:11 PM
I know it's the expression of the same kind as
'use strict'
or
'use asm'
, it's just that I can't google anything about it.
n

nilan

11/03/2017, 5:11 PM
it seems to be a webtask specific statement that enables ES6 like things
a

angly

11/03/2017, 5:23 PM
Indeed. Thanks.