GraphQL mutations on nested resources

Mutations are queries for manipulating data. If so, then the tree root queryand root mutationmust look the same right? Both of them must allow nested fields (nested mutations). I played with this (using express-graphql) and it works.

Example:

// PUT /projects/:project_id/products/:id
mutation {
  findProject(id: 1) { // make sure that project exists and we can access it before mutating data
    updateProduct(id: 1, name: "Foo") { // the resolve function receives a valid `project` as the first argument
      id
    }
  } 
}

Is this really an example? Should mutations be similar? If not, how do I handle nested resources? I can not find a real example that mutates nested resources. All examples define mutations only at the first level (fields on the root mutation).

+4
source share
1 answer

, , .

mutation {
  updateProduct(id: 1, name: "Foo") {
    id
  }
} 

, , . , , - :

resolve({ id }, { user }) {
  authorize(user, 'project', Product.find(id).project) // or whatever

  ... // update
}

:

, , .

, , , , .

updateProduct:

mutation {
  updateProduct(projectId: 1, id: 1, name: "Foo") {
    id
  }
} 

.


, , , , . , resolve , . , , , - , , .

, ( ), , , : , , . , , ( , ).

0
source

Source: https://habr.com/ru/post/1614708/


All Articles