Last active
October 14, 2017 02:06
-
-
Save govorov/1c827325786e03a87a4e7e2be6a89e3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// graphql/schema.ts | |
// | |
... | |
import { Mutation } from 'graphql/types/mutation'; | |
... | |
const schemaDefinition = ` | |
schema { | |
query : Query | |
# Add mutation here | |
mutation : Mutation | |
} | |
`; | |
const typeDefs = [ | |
schemaDefinition, | |
Query, | |
Mutation, // <-- And here | |
...types, | |
]; | |
// | |
// graphql/types/mutation.ts | |
// | |
... | |
export const Mutation = ` | |
type Mutation { | |
toggleCard ( | |
id: String! | |
): Card | |
} | |
`; | |
... | |
// | |
// graphql/resolvers.ts | |
// | |
export const resolvers = { | |
Query: { | |
...cardsResolver, | |
...cardResolver, | |
}, | |
// add mutations | |
Mutation: { | |
...toggleCardMutation, | |
}, | |
}; | |
// | |
// graphql/mutations/toggle-card.ts | |
// | |
import { getRepository } from 'typeorm'; | |
import { Card } from 'entities/card'; | |
export const toggleCardMutation = { | |
async toggleCard(_, { id }) { | |
const repository = getRepository(Card); | |
const card = await repository.findOne({ id }); | |
const done = !card.done; | |
const result = await repository.updateById(id, { done }); | |
return { | |
...card, | |
done, | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment