CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-graphql-codegen--testing

Jest testing utilities for GraphQL Code Generator with custom matchers, TypeScript validation, and GraphQL server mocking

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

resolver-testing.mddocs/

Resolver Testing Schema

Pre-built GraphQL schema and validation utilities specifically designed for testing resolver plugins with comprehensive type coverage.

Capabilities

resolversTestingSchema Constant

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:

  • Object Types: MyType, Child, MyOtherType, SomeNode, AnotherNodeWithChild, AnotherNodeWithAll
  • Interfaces: Node, AnotherNode, WithChild, WithChildren
  • Unions: ChildUnion, MyUnion
  • Query Type: Root query operations
  • Subscription Type: Real-time data operations
  • Custom Scalar: MyScalar
  • Directives: @myDirective, @authenticated
  • Complex Relationships: Interface implementations, union members, nested types

Full 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_DEFINITION

Usage 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>;
  };
`);

resolversTestingValidate Function

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:

  1. Generates base TypeScript types using @graphql-codegen/typescript plugin
  2. Merges base types with provided plugin content
  3. Appends any additional code
  4. Validates complete merged content using validateTs
  5. Returns validated merged content

Usage 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
}

Integration with Plugin Testing

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:

  • Comprehensive Coverage: Tests against complex GraphQL schema features
  • Type Safety: Ensures generated resolvers are type-safe
  • Consistent Testing: Standardized schema across all GraphQL Codegen plugins
  • Error Detection: Catches TypeScript compilation issues early
  • Integration Validation: Verifies plugins work with base TypeScript types

docs

index.md

jest-extensions.md

monorepo-utilities.md

resolver-testing.md

server-mocking.md

typescript-validation.md

tile.json