Skip to content

Commit 114927e

Browse files
authored
Merge pull request #20 from thefrontside/dl/post-transform-v2
Add postTransform option to allow apply custom transformations after schema mapping
2 parents 871511d + d35d38a commit 114927e

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

.changeset/clean-badgers-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@frontside/hydraphql": patch
3+
---
4+
5+
Add postTransform option to allow apply custom transformations after schema mapping

src/transformSchema.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,17 @@ export function transformSchema(
1111
additionalModules: (GraphQLModule | Module)[] = [],
1212
{ generateOpaqueTypes }: { generateOpaqueTypes?: boolean } = {},
1313
) {
14+
const postTransformers: GraphQLModule["postTransform"][] = [];
1415
const modules = [CoreSync(), ...additionalModules];
1516
const directiveMappers: Record<string, FieldDirectiveMapper> = {};
1617
const typeDefs: DocumentNode[] = modules.flatMap((m) => {
17-
const { module: gqlModule, mappers = {} } = "id" in m ? { module: m } : m;
18+
const {
19+
module: gqlModule,
20+
mappers = {},
21+
postTransform = undefined,
22+
} = "id" in m ? { module: m } : m;
1823
const documents = gqlModule.typeDefs;
24+
postTransformers.push(postTransform);
1925
documents.forEach((document) => {
2026
document.definitions.forEach((definition) => {
2127
if (definition.kind !== Kind.DIRECTIVE_DEFINITION) return;
@@ -36,10 +42,15 @@ export function transformSchema(
3642
generateOpaqueTypes,
3743
});
3844

39-
const errors = validateSchema(schema);
45+
const postSchema = postTransformers.reduce(
46+
(s, transform) => transform?.(s) ?? s,
47+
schema,
48+
);
49+
50+
const errors = validateSchema(postSchema);
4051

4152
if (errors.length > 0) {
4253
throw new Error(errors.map((e) => e.message).join("\n"));
4354
}
44-
return schema;
55+
return postSchema;
4556
}

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type {
44
GraphQLFieldConfig,
55
GraphQLNamedType,
66
GraphQLObjectType,
7+
GraphQLSchema,
78
} from "graphql";
89
import type { Application, Module } from "graphql-modules";
910

@@ -64,5 +65,6 @@ export interface NamedType {
6465

6566
export interface GraphQLModule {
6667
mappers?: Record<string, FieldDirectiveMapper>;
68+
postTransform?: (schema: GraphQLSchema) => GraphQLSchema;
6769
module: Module;
6870
}

0 commit comments

Comments
 (0)