Skip to content

Commit

Permalink
feat(graphql): generate mutation and queries resolver fields in schem…
Browse files Browse the repository at this point in the history
…a builder
  • Loading branch information
marcus-sa committed Sep 20, 2023
1 parent d5cde63 commit 3445201
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export {
Context,
ID,
} from './lib/types-builder';
export { SchemaBuilder } from './lib/schema-builder';
export { SchemaBuilder, buildSchema } from './lib/schema-builder';
export { GraphQLModule } from './lib/graphql.module';
export { GraphQLConfig } from './lib/graphql-config';
export { Driver } from './lib/driver';
Expand Down
3 changes: 3 additions & 0 deletions packages/graphql/src/lib/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ export class DeepkitGraphQLResolvers extends Set<{
export class Resolvers extends WeakMap<ClassType, Instance> {
readonly classTypes = new Set<ClassType>();

readonly instances = new Set<Instance>();

constructor(instances: readonly Instance[]) {
const entries = instances.map<readonly [ClassType, Instance]>(instance => [
instance.constructor,
instance,
]);
super(entries);
entries.forEach(([classType]) => this.classTypes.add(classType));
instances.forEach(instance => this.instances.add(instance));
}
}

Expand Down
50 changes: 28 additions & 22 deletions packages/graphql/src/lib/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ export class SchemaBuilder {
);
}

generateMutationResolverFields(): GraphQLFieldConfigMap<unknown, unknown> {
return [...this.options.resolvers.instances].reduce<GraphQLFieldConfigMap<unknown, unknown>>(
(fields, instance) => ({
// TODO: validate that fields don't override each other
...fields,
...this.typesBuilder.generateMutationResolverFields(
this.options.resolvers.get(instance),
),
}),
{},
)
}

generateQueryResolverFields(): GraphQLFieldConfigMap<unknown, unknown> {
return [...this.options.resolvers.instances].reduce<GraphQLFieldConfigMap<unknown, unknown>>(
(fields, instance) => ({
// TODO: validate that fields don't override each other
...fields,
...this.typesBuilder.generateQueryResolverFields(
this.options.resolvers.get(instance),
),
}),
{},
)
}

private buildRootMutationType(): GraphQLObjectType | undefined {
const classTypes = [...this.options.resolvers.classTypes];

Expand All @@ -53,17 +79,7 @@ export class SchemaBuilder {

return new GraphQLObjectType({
name: 'Mutation',
fields: () =>
classTypes.reduce<GraphQLFieldConfigMap<unknown, unknown>>(
(fields, classType) => ({
// TODO: validate that fields don't override each other
...fields,
...this.typesBuilder.generateMutationResolverFields(
this.options.resolvers.get(classType),
),
}),
{},
),
fields: () => this.generateMutationResolverFields(),
});
}

Expand Down Expand Up @@ -116,17 +132,7 @@ export class SchemaBuilder {

return new GraphQLObjectType({
name: 'Query',
fields: () =>
classTypes.reduce<GraphQLFieldConfigMap<unknown, unknown>>(
(fields, classType) => ({
// TODO: validate that fields don't override each other
...fields,
...this.typesBuilder.generateQueryResolverFields(
this.options.resolvers.get(classType),
),
}),
{},
),
fields: () => this.generateQueryResolverFields(),
});
}

Expand Down

0 comments on commit 3445201

Please sign in to comment.