question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Possible to protect Prisma-style nested mutations?

See original GitHub issue

With Prisma, I can write nested mutations like so:

mutation{
  createPost(data: {
    body: "Hello World"
    tags: {
      create: [
        {name: "cats"}
      ],
    }
  }){
    body
    tags{
      name
    }
  }
}

Using Shield, I can set rules for createPost but any such rule bypasses all my rules on createTag. So basically if a user has permission to create a post, they can by extension create anything else that a post connects to. ie. Itā€™s impossible to allow a user to updatePosts without also allowing them to update any tag, user, comment etc connected to that post. I think this is an inherent limitation of graqhql middleware and Prismaā€“they are not aware of these nested mutations and thus do not hit any mutation middleware after the top level.

Am I correct or missing something? How do people handle the case where a user is connected to a post? Can anyone update that user who can update the post?

Thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
lora-reamescommented, Feb 7, 2019

@vadistic if youā€™re interested in modifying and exposing a subset of a schema as a new schema I recommend you check out https://github.com/mitoai/gintonic

1reaction
maticzavcommented, Feb 6, 2019

Hey @nolandg šŸ‘‹,

I wouldnā€™t say this is a ā€œlimitationā€ of graphql-middleware and prisma because GraphQL does not support nested mutations as such. What prisma does is use the arguments of a function to change the nodes internally. In that sense, there are no ā€œnested-mutationsā€ only the Prisma API which handles connections in such a manner.

Considering this, the majority of the systems rely on unwrapped Prisma servers, if I name them so, which means that they change the schema to their needs and use prisma only as a delegation layer which happens to use GraphQL as well. I believe this might be connected to #113. So far, I havenā€™t come across a meaningful example which would persuade me into implementing such functionality. As a note, I would say that exposing Prisma API is far too risky because you have no control over connect and create arguments, as you already mentioned, plus you usually only need about a fifth of all the exposed functionality.

Nevertheless, if you believe this is your only option, I would go about implementing it as an argument checker. Take a look at the example below.

const hasPermission = rule({ cache: 'strict' })(
  async (parent, args, ctx, info) => {
    if (args.data.tags.create !== undefined) {
      return isUserAuthenticated(ctx)
    }
    return true
  },
)

I hope this helps you solve your problem. šŸ™‚

Read more comments on GitHub >

github_iconTop Results From Across the Web

Possible to protect Prisma-style nested mutations? #279
With Prisma, I can write nested mutations like so: mutation{ createPost(data: { body: "Hello World" tags: { create: [ {name: "cats"} ]Ā ...
Read more >
Supporting opt-in nested mutations in GraphQL
Discover the benefits of offering nested mutations as an opt-in feature in your GraphQL server, including a slimmer, more manageable schema.
Read more >
Beware of GraphQL Nested Mutations! - freeCodeCamp
A reader pointed me to an issue on the GraphQL GitHub site where it was stated that the execution order of nested mutations...
Read more >
How to do a nested mutation resolver with nexus-prisma
My main question is what is the most elegant way to do either nested mutation or connect all in one. graphql Ā· prisma...
Read more >
Best practices for GraphQL mutations - Fauna
GraphQL mutations are used to modify data in your API. ... object that is nested as much as possible to include all the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found