Skip to content

Commit

Permalink
Add generated resolversMap to typescript-resolvers plugin meta (#10007)
Browse files Browse the repository at this point in the history
* Add generated resolversMap to ts-resolvers plugin meta

* Add changeset
  • Loading branch information
eddeee888 authored Jun 17, 2024
1 parent cac19e5 commit 808ada5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changeset/forty-cooks-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': minor
'@graphql-codegen/typescript-resolvers': minor
---

Add generated resolvers map type name to typescript-resolvers plugin meta
Original file line number Diff line number Diff line change
Expand Up @@ -1264,12 +1264,18 @@ export class BaseResolversVisitor<
return this._hasFederation;
}

public getRootResolver(): { content: string; generatedResolverTypes: Record<string, { name: string }> } {
public getRootResolver(): {
content: string;
generatedResolverTypes: {
resolversMap: { name: string };
userDefined: Record<string, { name: string }>;
};
} {
const name = this.convertName(this.config.allResolversTypeName);
const declarationKind = 'type';
const contextType = `<ContextType = ${this.config.contextType.type}>`;

const generatedResolverTypes: Record<string, { name: string }> = {};
const userDefinedTypes: Record<string, { name: string }> = {};
const content = [
new DeclarationBlock(this._declarationBlockConfig)
.export()
Expand All @@ -1281,7 +1287,7 @@ export class BaseResolversVisitor<
const resolverType = this._collectedResolvers[schemaTypeName];

if (resolverType.baseGeneratedTypename) {
generatedResolverTypes[schemaTypeName] = { name: resolverType.baseGeneratedTypename };
userDefinedTypes[schemaTypeName] = { name: resolverType.baseGeneratedTypename };
}

return indent(this.formatRootResolver(schemaTypeName, resolverType.typename, declarationKind));
Expand All @@ -1292,7 +1298,10 @@ export class BaseResolversVisitor<

return {
content,
generatedResolverTypes,
generatedResolverTypes: {
resolversMap: { name },
userDefined: userDefinedTypes,
},
};
}

Expand Down
7 changes: 6 additions & 1 deletion packages/plugins/typescript/resolvers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ const capitalize = (s: string): string => s.charAt(0).toUpperCase() + s.slice(1)

export const plugin: PluginFunction<
TypeScriptResolversPluginConfig,
Types.ComplexPluginOutput<{ generatedResolverTypes: Record<string, { name: string }> }>
Types.ComplexPluginOutput<{
generatedResolverTypes: {
resolversMap: { name: string };
userDefined: Record<string, { name: string }>;
};
}>
> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: TypeScriptResolversPluginConfig) => {
const imports = [];
if (!config.customResolveInfo) {
Expand Down
67 changes: 42 additions & 25 deletions packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3092,6 +3092,18 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
{ outputFile: '' }
);

expect(result.content).toBeSimilarStringTo(`
export type resolvers<ContextType = any> = {
Query?: query_resolvers<ContextType>;
Mutation?: mutation_resolvers<ContextType>;
Node?: node_resolvers<ContextType>;
Post?: post_resolvers<ContextType>;
User?: user_resolvers<ContextType>;
CreateUserOk?: create_user_ok_resolvers<ContextType>;
CreateUserError?: create_user_error_resolvers<ContextType>;
CreateUserPayload?: create_user_payload_resolvers<ContextType>;
ErrorType?: error_type_resolvers;
};`);
expect(result.content).toContain(`export type create_user_error_resolvers`);
expect(result.content).toContain(`export type create_user_ok_resolvers`);
expect(result.content).toContain(`export type create_user_payload_resolvers`);
Expand All @@ -3105,32 +3117,37 @@ export type ResolverFn<TResult, TParent, TContext, TArgs> = (
expect(result.meta).toMatchInlineSnapshot(`
Object {
"generatedResolverTypes": Object {
"CreateUserError": Object {
"name": "create_user_error_resolvers",
},
"CreateUserOk": Object {
"name": "create_user_ok_resolvers",
},
"CreateUserPayload": Object {
"name": "create_user_payload_resolvers",
},
"ErrorType": Object {
"name": "error_type_resolvers",
},
"Mutation": Object {
"name": "mutation_resolvers",
},
"Node": Object {
"name": "node_resolvers",
},
"Post": Object {
"name": "post_resolvers",
},
"Query": Object {
"name": "query_resolvers",
"resolversMap": Object {
"name": "resolvers",
},
"User": Object {
"name": "user_resolvers",
"userDefined": Object {
"CreateUserError": Object {
"name": "create_user_error_resolvers",
},
"CreateUserOk": Object {
"name": "create_user_ok_resolvers",
},
"CreateUserPayload": Object {
"name": "create_user_payload_resolvers",
},
"ErrorType": Object {
"name": "error_type_resolvers",
},
"Mutation": Object {
"name": "mutation_resolvers",
},
"Node": Object {
"name": "node_resolvers",
},
"Post": Object {
"name": "post_resolvers",
},
"Query": Object {
"name": "query_resolvers",
},
"User": Object {
"name": "user_resolvers",
},
},
},
}
Expand Down

0 comments on commit 808ada5

Please sign in to comment.