0.4.0 • Published 7 years ago

graphql-mutate v0.4.0

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

graphql-mutate

Make GraphQL support nested mutations

Goal

Schema

type User {
  id: ID
  name: String
  articleCount: Int
  team: Team
  update(input: UserInput): String
  addArticle(input: ArticleInput): Article
}

update and addArticle is mutation in type User

query {
  user(id: 1) {
    id
    name
    articleCount
    team
    update(input: { name: "Justin" })
    addArticle(input: { title: "Hello"}) {
      title
    }
  }
}

We hope it first executes update and then addArticle sequentially.
After executing mutations, it will evaluate name and articleCount in parallel because update and addArticle may change value of name or articleCount.
But it can still evaluate id and team immediately before executing mutations because they won't change by mutations.

How

Use graphql-mutate!

npm install graphql-mutate
const { mutate } = require('graphql-mutate')
const { makeExecutableSchema } = require('graphql-tools')

const User = require('./User') // User type resolvers

const resolvers = {
  Query: {},
  Mutation: {},
  User: mutate(User, {
    mutations: ['update', 'addArticle'],
    dependencies: ['name', 'articleCount'],
  })
}

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
})

const User = require('./User') is normal type resolver, and you just wrap it with mutate.

  • mutations option: a array includes all mutation field names
  • dependencies option: a array includes all field names that may be affected by mutations

Example

See doc

TODO

Testing

License

graphql-mutate is available under the MIT license.