Pre-built GraphQL schema and validation utilities specifically designed for testing resolver plugins with comprehensive type coverage.
A comprehensive GraphQL schema built specifically for testing resolver plugins. Contains a wide variety of GraphQL constructs to ensure thorough testing coverage.
/**
* Pre-built GraphQL schema for testing resolver plugins
* Includes types, unions, interfaces, directives, scalars, and complex relationships
*/
const resolversTestingSchema: GraphQLSchema;Schema Definition: The schema includes comprehensive GraphQL features:
MyType, Child, MyOtherType, SomeNode, AnotherNodeWithChild, AnotherNodeWithAllNode, AnotherNode, WithChild, WithChildrenChildUnion, MyUnionMyScalar@myDirective, @authenticatedFull Schema Structure:
type MyType {
foo: String! @authenticated
otherType: MyOtherType
withArgs(arg: String, arg2: String!): String
unionChild: ChildUnion
}
type Child {
bar: String!
parent: MyType
}
type MyOtherType {
bar: String!
}
union ChildUnion = Child | MyOtherType
type Query {
something: MyType!
}
type Subscription {
somethingChanged: MyOtherType
}
interface Node {
id: ID!
}
type SomeNode implements Node {
id: ID!
}
interface AnotherNode {
id: ID!
}
interface WithChild {
unionChild: ChildUnion
node: AnotherNode
}
interface WithChildren {
unionChildren: [ChildUnion!]!
nodes: [AnotherNode!]!
}
type AnotherNodeWithChild implements AnotherNode & WithChild {
id: ID!
unionChild: ChildUnion
interfaceChild: Node
}
type AnotherNodeWithAll implements AnotherNode & WithChild & WithChildren {
id: ID!
unionChild: ChildUnion
unionChildren: [ChildUnion!]!
interfaceChild: Node
interfaceChildren: [Node!]!
}
union MyUnion = MyType | MyOtherType
scalar MyScalar
directive @myDirective(arg: Int!, arg2: String!, arg3: Boolean!) on FIELD
directive @authenticated on FIELD_DEFINITIONUsage Examples:
import { resolversTestingSchema } from "@graphql-codegen/testing";
import { plugin as resolversPlugin } from "@graphql-codegen/typescript-resolvers";
// Test resolver plugin with comprehensive schema
const result = await resolversPlugin(resolversTestingSchema, [], {
contextType: "MyContext",
mappers: {
MyType: "MyTypeModel"
}
});
// Verify generated resolver types
expect(result.content).toBeSimilarStringTo(`
export type QueryResolvers<ContextType = MyContext> = {
something?: Resolver<MyType, {}, ContextType>;
};
`);Validates plugin output by merging with TypeScript plugin output and performing comprehensive type-checking. Essential for ensuring resolver plugins generate valid TypeScript code.
/**
* Validates plugin output by merging with TypeScript plugin output and type-checking
* @param content - The plugin content to validate (string or PluginOutput object)
* @param config - Plugin configuration object (default: {})
* @param pluginSchema - GraphQL schema to use (default: resolversTestingSchema)
* @param additionalCode - Additional TypeScript code to append (default: '')
* @returns Promise resolving to the merged and validated content
* @throws Validation errors via validateTs if type-checking fails
*/
function resolversTestingValidate(
content: Types.PluginOutput,
config?: any,
pluginSchema?: GraphQLSchema,
additionalCode?: string
): Promise<string>;Validation Process:
@graphql-codegen/typescript pluginvalidateTsUsage Examples:
import {
resolversTestingValidate,
resolversTestingSchema
} from "@graphql-codegen/testing";
// Basic validation with default schema
const resolverOutput = await resolversPlugin(resolversTestingSchema, [], config);
const validatedContent = await resolversTestingValidate(resolverOutput);
// Validation with custom config
const customConfig = {
contextType: "GraphQLContext",
useIndexSignature: true
};
const result = await resolversPlugin(resolversTestingSchema, [], customConfig);
const validated = await resolversTestingValidate(result, customConfig);
// Validation with custom schema
const customSchema = buildSchema(`
type Query {
customField: String
}
`);
const customResult = await resolversPlugin(customSchema, [], config);
const validatedCustom = await resolversTestingValidate(
customResult,
config,
customSchema
);
// Validation with additional code
const additionalTypes = `
interface CustomContext {
userId: string;
permissions: string[];
}
`;
const withAdditional = await resolversTestingValidate(
resolverOutput,
config,
resolversTestingSchema,
additionalTypes
);
// Error handling
try {
await resolversTestingValidate(invalidResolverOutput);
} catch (error) {
console.error("Validation failed:", error.message);
// Handle TypeScript compilation errors
}The resolver testing utilities integrate seamlessly with GraphQL Codegen plugin development:
Plugin Test Structure:
import {
resolversTestingSchema,
resolversTestingValidate
} from "@graphql-codegen/testing";
import { plugin } from "../src/index"; // Your plugin
describe("My Resolver Plugin", () => {
it("should generate valid resolver types", async () => {
const config = { contextType: "MyContext" };
const result = await plugin(resolversTestingSchema, [], config);
// Validate generated code compiles correctly
const validatedContent = await resolversTestingValidate(result, config);
// Test specific generated content
expect(validatedContent).toBeSimilarStringTo(`
export type MyTypeResolvers<ContextType = MyContext> = {
foo?: Resolver<string, {}, ContextType>;
otherType?: Resolver<MyOtherType, {}, ContextType>;
};
`);
});
it("should handle complex interface implementations", async () => {
const result = await plugin(resolversTestingSchema, [], {
onlyResolveTypeForInterfaces: true
});
await resolversTestingValidate(result);
expect(result.content).toBeSimilarStringTo(`
export type NodeResolvers<ContextType = any> = {
__resolveType: TypeResolveFn<'SomeNode', ParentType, ContextType>;
};
`);
});
});Benefits for Plugin Testing: