Skip to content

Latest commit

 

History

History
87 lines (74 loc) · 1.94 KB

mutations.md

File metadata and controls

87 lines (74 loc) · 1.94 KB

Mutations

Expand the GraphQL-Schema

# ...
# Item input type
input ItemInput {
  id: ID!
  email: String
}

type Mutation {
  # Upsert an item
  upsertItem(input: ItemInput!): Item
  # Remove an item
  removeItem(id: ID!): Boolean
}
# ...

Implement the resolver function

export const resolvers = {
  // ...
  Mutation: {
    upsertItem: (parent, args, context, info) => context.loaders.items.upsert(args.input),
    removeItem: (parent, args, context, info) => context.loaders.items.remove(args.id),
  },
  // ...
}

const itemsStore = {}
export const loaders = {
  // ...
  items: {
    load: id => itemsStore[id],
    all: () =>  Object.values(itemsStore),
    upsert: input => itemsStore[input.id] = input,
    remove: id => delete itemsStore[id],
  },
  // ...
}

Execute a GraphQL mutation query

subkit request \
  --token eyJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImdvQHN1YmtpdC5pbyJ9.-cVh3sNNCqCZZGdS2jwL_u3aJKXZqNippsMSxj15ROk \
  --url http://localhost:8080/graphql \
  --query 'mutation upsertItem{upsertItem(input:{id:\"subkitio\",email:\"go@subkit.io\"}){id email}}'

Usage of GraphQL mutation variables

mutation upsertItem($input: ItemInput!) {
  upsertItem(input: $input) {
    id
    email
  }
}
{
  "input": {
    "id": "demo",
    "email": "demo@aol.com"
  }
}
subkit request \
  --token eyJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImdvQHN1YmtpdC5pbyJ9.-cVh3sNNCqCZZGdS2jwL_u3aJKXZqNippsMSxj15ROk \
  --url http://localhost:8080/graphql \
  --query 'mutation upsertItem($input: ItemInput!) {upsertItem(input: $input) {id email}}' \
  --variables '{"input": {"id": "demo", "email": "demo@aol.com"}}'