Skip to content

Instantly share code, notes, and snippets.

@govorov
Last active October 14, 2017 02:06
Show Gist options
  • Save govorov/1c827325786e03a87a4e7e2be6a89e3f to your computer and use it in GitHub Desktop.
Save govorov/1c827325786e03a87a4e7e2be6a89e3f to your computer and use it in GitHub Desktop.
//
// 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