Skip to content

Commit 3c9b9da

Browse files
author
hirsch88
committed
Add mutation example
1 parent 8d7c6d0 commit 3c9b9da

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {
2+
GraphQLFieldConfig,
3+
GraphQLNonNull,
4+
GraphQLString,
5+
GraphQLInt
6+
} from 'graphql';
7+
import { plainToClass } from 'class-transformer';
8+
import { AbstractGraphQLMutation } from '../../lib/graphql/AbstractGraphQLMutation';
9+
import { PetType } from '../types/PetType';
10+
import { PetService } from '../services/PetService';
11+
import { GraphQLContext, Mutation } from '../../lib/graphql';
12+
import { Pet } from '../models/Pet';
13+
14+
interface CreatePetMutationArguments {
15+
name: string;
16+
age: number;
17+
}
18+
19+
@Mutation()
20+
export class CreatePetMutation extends AbstractGraphQLMutation<GraphQLContext<any, any>, Pet, CreatePetMutationArguments> implements GraphQLFieldConfig {
21+
public type = PetType;
22+
public args = {
23+
name: { type: new GraphQLNonNull(GraphQLString) },
24+
age: { type: new GraphQLNonNull(GraphQLInt) },
25+
};
26+
27+
public async run(root: any, args: CreatePetMutationArguments, context: GraphQLContext<any, any>): Promise<Pet> {
28+
const petService = context.container.get<PetService>(PetService);
29+
const pet = await petService.create(plainToClass(Pet, args));
30+
return pet;
31+
}
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { AbstractGraphQLHooks } from './AbstractGraphQLHooks';
2+
3+
4+
export abstract class AbstractGraphQLMutation<TContext, TResult, TArgs> extends AbstractGraphQLHooks<TContext, TResult, TArgs> {
5+
6+
/**
7+
* This will be called by graphQL and they need to have it not as a
8+
* member function of this class. We use this hook to add some more logic
9+
* to it, like permission checking and before and after hooks to alter some data.
10+
*/
11+
public resolve = async <S>(root: S, args: TArgs, context: TContext): Promise<TResult> => {
12+
// We need to store the query arguments in the context so they can be accessed by subsequent resolvers
13+
(context as any).resolveArgs = args;
14+
args = await this.before(context, args);
15+
let result = await this.run<S>(root, args, context);
16+
result = await this.after(result, context, args);
17+
return (result as TResult);
18+
}
19+
20+
}

src/loaders/graphqlLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const graphqlLoader: MicroframeworkLoader = (settings: MicroframeworkSett
1414
route: env.graphql.route,
1515
editorEnabled: env.graphql.enabled,
1616
queries: env.app.dirs.queries,
17-
mutations: env.app.dirs.queries,
17+
mutations: env.app.dirs.mutations,
1818
dataLoaders: {
1919
users: createDataLoader(UserRepository),
2020
pets: createDataLoader(Pet),

0 commit comments

Comments
 (0)