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 GraphQL parent context within Parse.Cloud.define() #7621

Open
3 tasks done
richardguerre opened this issue Oct 13, 2021 · 2 comments
Open
3 tasks done

Add GraphQL parent context within Parse.Cloud.define() #7621

richardguerre opened this issue Oct 13, 2021 · 2 comments
Labels
type:feature New feature or improvement of existing feature

Comments

@richardguerre
Copy link

richardguerre commented Oct 13, 2021

New Feature / Enhancement Checklist

Current Limitation

It is currently not possible to get context of the "GraphQL parent". So it is not extend the GraphQL schema like so:

extend type MyParseClass {
  myParseClassCustomField: Any! @resolve(to: "myParseClassCustomField")
}

and query like this:

query GetMyParseClassAndCalculatedField($myParseClassId: ID!) {
  myParseClass(id: $myParseClassId) {
    objectId
    myParseClassCustomField
  }
}

But instead, we have to do extend it like this:

extend type Query {
  myParseClassCustomField(myParseClassId: ID!): Any! @resolve(to: "myParseClassCustomField")
}

and query like this:

query GetMyParseClassAndCalculatedField($myParseClassId: ID!) {
  myParseClass(id: $myParseClassId) {
    objectId
  }

  myParseClassCustomField(myParseClassId: $myParseClassId)
}

The above assumes there is a MyParseClass parse class defined like so:

type MyParseClass {
  objectId: ID!
  # ... other properties of MyParseClass
}

The above is a very simple example, but you can see how it gets really complicated to run cloud code functions within GraphQL when doing nested queries, like myParseClasses.edges.node.myParseClassCustomField.

This also means doing the waterfall approach when the objectId is not defined, where the query for the customField has to wait until you get the objectId from first query, then query for custom field using that id.

Feature / Enhancement Description

Add parent GraphQL context to Parse.Cloud.define request argument.

interface FunctionRequest<T extends Params = Params> {
  installationId?: string | undefined;
  master?: boolean | undefined;
  params: T;
  user?: User | undefined;
  parent: {
    typename?: string | undefined;
    objectId?: string | undefined;
  }
}

It should at minimum contain the typename and objectId of the parent, so that we can easily query using that objectId:

Parse.Cloud.define('myParseClassCustomField', (request) => {
  if (request.parent.typename === 'MyParseClass') {
    return calculateUsingObjectId(request.parent.objectId);
  } else {
    throw 'This function can only be called from within MyParseClass';
  }
});

Example Use Case

Functionality explained above.

Alternatives / Workarounds

Current workaround is to extend type Query instead of the actual class we want to extend and add all required params (objectId or other) to filter/get the class instance we want within the Parse.Cloud.define definition.

@parse-github-assistant
Copy link

parse-github-assistant bot commented Oct 13, 2021

Thanks for opening this issue!

  • 🎉 We are excited about your ideas for improvement!

@mtrezza mtrezza added the type:feature New feature or improvement of existing feature label Oct 13, 2021
@richardguerre
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or improvement of existing feature
Projects
None yet
Development

No branches or pull requests

2 participants