or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

caching-performance.mdcontext-types.mderror-handling.mdindex.mdlogging-system.mdplugin-system.mdrequest-processing.mdresult-processing.mdschema-management.mdserver-configuration.mdsubscription-system.md
tile.json

schema-management.mddocs/

Schema Management

Schema creation and management utilities that integrate with GraphQL Tools for defining executable GraphQL schemas with resolvers, type definitions, and context integration.

Capabilities

Schema Creation

Utility function for creating executable GraphQL schemas with context integration.

/**
 * Creates an executable GraphQL schema with context integration
 * @param opts - Schema definition options from GraphQL Tools
 * @returns GraphQL schema with context type information
 */
function createSchema<TContext = {}>(
  opts: IExecutableSchemaDefinition<TContext & YogaInitialContext>
): GraphQLSchemaWithContext<TContext & YogaInitialContext>;

Schema Definition Types

Type definitions for schema configuration and context integration.

/**
 * GraphQL schema with context type information
 */
type GraphQLSchemaWithContext<TContext> = GraphQLSchema & {
  _context?: TContext;
};

/**
 * Schema definition for Yoga server - can be static schema or factory function
 */
type YogaSchemaDefinition<TServerContext, TUserContext> =
  | GraphQLSchema
  | ((context: TServerContext) => GraphQLSchema | Promise<GraphQLSchema>);

Schema Definition Options

Schema definition options from GraphQL Tools with Yoga context integration.

/**
 * Executable schema definition options (from @graphql-tools/schema)
 */
interface IExecutableSchemaDefinition<TContext> {
  /** GraphQL type definitions as string or DocumentNode */
  typeDefs: string | DocumentNode | Array<string | DocumentNode>;
  /** Resolver functions for schema fields */
  resolvers?: IResolvers<any, TContext> | Array<IResolvers<any, TContext>>;
  /** Custom scalar type implementations */
  schemaTransforms?: Array<SchemaTransform>;
  /** Custom directive implementations */
  schemaDirectives?: Record<string, typeof SchemaDirectiveVisitor>;
  /** Additional resolver options */
  resolverValidationOptions?: IResolverValidationOptions;
  /** Parse options for type definitions */
  parseOptions?: ParseOptions;
  /** Inherit resolvers from interfaces */
  inheritResolversFromInterfaces?: boolean;
}

Usage Examples:

import { createSchema, createYoga } from 'graphql-yoga';

// Basic schema creation
const schema = createSchema({
  typeDefs: `
    type Query {
      hello: String
      user(id: ID!): User
    }
    
    type User {
      id: ID!
      name: String!
      email: String!
    }
  `,
  resolvers: {
    Query: {
      hello: () => 'Hello World!',
      user: (_, { id }, context) => {
        return context.userService.findById(id);
      }
    }
  }
});

// Schema with custom context type
interface MyContext {
  user?: User;
  services: {
    userService: UserService;
    postService: PostService;
  };
}

const typedSchema = createSchema<MyContext>({
  typeDefs: `
    type Query {
      me: User
      posts: [Post!]!
    }
    
    type User {
      id: ID!
      name: String!
      posts: [Post!]!
    }
    
    type Post {
      id: ID!
      title: String!
      author: User!
    }
  `,
  resolvers: {
    Query: {
      me: (_, __, context) => context.user,
      posts: (_, __, context) => context.services.postService.findAll()
    },
    User: {
      posts: (user, _, context) => 
        context.services.postService.findByUserId(user.id)
    },
    Post: {
      author: (post, _, context) => 
        context.services.userService.findById(post.authorId)
    }
  }
});

// Using schema with Yoga server
const yoga = createYoga({
  schema: typedSchema,
  context: ({ request }) => ({
    user: getUserFromRequest(request),
    services: {
      userService: new UserService(),
      postService: new PostService()
    }
  })
});

// Dynamic schema factory
const dynamicYoga = createYoga({
  schema: (serverContext) => {
    // Schema can be determined based on server context
    if (serverContext.tenant === 'admin') {
      return adminSchema;
    }
    return userSchema;
  }
});

Schema Plugin Integration

Integration with the useSchema plugin for dynamic schema handling.

/**
 * Plugin for handling schema definition in the plugin system
 * @param schemaOrSchemaFactory - Schema or factory function
 * @returns Plugin instance for schema handling
 */
function useSchema<TServerContext, TUserContext>(
  schemaOrSchemaFactory: YogaSchemaDefinition<TServerContext, TUserContext>
): Plugin<TServerContext & TUserContext & YogaInitialContext, TServerContext, TUserContext>;

Schema Merging

Re-exported schema merging utility for combining multiple schemas.

/**
 * Merge multiple GraphQL schemas into one (from @graphql-tools/schema)
 * @param schemas - Array of schemas to merge
 * @returns Merged GraphQL schema
 */
function mergeSchemas(config: {
  schemas: GraphQLSchema[];
  typeDefs?: string | DocumentNode | Array<string | DocumentNode>;
  resolvers?: IResolvers | Array<IResolvers>;
  schemaTransforms?: Array<SchemaTransform>;
}): GraphQLSchema;

Advanced Schema Examples

// Multi-tenant schema with merging
import { mergeSchemas, createSchema } from 'graphql-yoga';

const baseSchema = createSchema({
  typeDefs: `
    type Query {
      ping: String
    }
  `,
  resolvers: {
    Query: {
      ping: () => 'pong'
    }
  }
});

const userSchema = createSchema({
  typeDefs: `
    extend type Query {
      users: [User!]!
    }
    
    type User {
      id: ID!
      name: String!
    }
  `,
  resolvers: {
    Query: {
      users: (_, __, context) => context.userService.findAll()
    }
  }
});

const mergedSchema = mergeSchemas({
  schemas: [baseSchema, userSchema]
});

// Schema with custom directives
const schemaWithDirectives = createSchema({
  typeDefs: `
    directive @auth(role: String!) on FIELD_DEFINITION
    
    type Query {
      publicData: String
      privateData: String @auth(role: "admin")
    }
  `,
  resolvers: {
    Query: {
      publicData: () => 'This is public',
      privateData: () => 'This is private'
    }
  },
  schemaDirectives: {
    auth: AuthDirective
  }
});