npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apollo-server-autoloader

v2.1.2

Published

auto loading schemas, resolvers and datasources

Downloads

5

Readme

apollo-server-autoloader - Autoload your typeDefs, resolvers and datasources

This package enables autoloading of schemas, resolvers and datasources based on configurable naming conventions.

Install

npm i apollo-server-autoloader
# or
yarn add apollo-server-autoloader

Usage

The following example asumes, that you define your types, resolvers and datasources in the api folder of your project.

import * as path from 'path'
import { buildFederatedSchema } from '@apollo/federation'
import { ApolloServer } from '...'
import { Autoloader } from 'apollo-server-autoloader'

const autoloaderConfig = {
  searchPaths: [path.resolve(__dirname, 'api')]
}

const createServer = async () => {
  import autoloader = Autoloader(autoloaderConfig)
  const typeDefs = await autoloader.getTypeDefs()
  const resolvers = await autoloader.getResolvers()
  const datasources = await autoloader.getDatasources()

  return new ApolloServer({
    dataSources,
    schema: buildFederatedSchema([{ typeDefs, resolvers }])
    ...
  });
}

Autoloader config

interface AutoloaderConfig {
  // (required) search paths
  searchPaths: string[]
  // naming conventions
  conventions?: FileNamingConventions
  // which file extensions should be considered
  fileExtensions?: string[]
  // which files should be excluded (regex)
  exclude: string[]
}

interface FileNamingConventions {
  // naming conventions for schema files
  types?: NamingConvention
  // naming conventions for resolver files
  resolvers?: NamingConvention
  // naming conventions for datasource files
  datasources?: NamingConvention
}

interface NamingConvention {
  // The "key" is the pattern for the file name
  // The ExportPattern is the pattern for the export variable
  //  If your schemas for example are located in files
  // which are named "schema.ts" and they provide an export 
  // variable named "typeDef" the naming convention would look like follows:
  // { schema: 'typeDef' }
  [key: string]: ExportPattern
}

Example Config

Asume your project structure looks like follows:

|- /api
|- |- schemaFile.ts
|- |- queriesFile.ts
|- |- mutationsFile.ty
|- |- DatasourceFile.ts
|- |- /subolder
|- |- |- schemaFile.ts
...

Your file exports look like follows:

// schemaFile.ts
export const myTypeDef = gql`...`
// queriesFile.ts
export const myQueries = { ... }
// mutationsFile.ts
export const myMutations = { ... }
// DatasourceFile.ts
export class MyDatasource { ... }

In this case the corresponding config for the autoloader should look like follows:

const config = {
  searchPaths: [path.resolve(__dirname, 'api')],
  conventions: {
    types: {
      schemaFile: 'myTypeDef',
    },
    resolvers: {
      queriesFile: 'myQueries',
      mutationsFile: 'myMutations',
    },
    datasources: {
      DatasourceFile: 'MyDatasource',
    }
  },
  fileExtensions: ['.ts'],
  exclude: ['[^\/]+\.(spec|test)', '__tests__'],
}

Default config

The default config for the autoloader looks like follows:

export const defaultConfig: AutoloaderConfig = {
  searchPaths: ['.'],
  conventions: {
    types: { schema: 'typeDef' },
    resolvers: { queries: 'queries', mutations: 'mutations' },
    datasources: { Datasource: 'Datasource' },
  },
  fileExtensions: ['ts']
}

If you don't change the config, this means you must name your schema files schema.ts, your resolver files queries.ts or mutations.ts and your datasource files Datasource.ts. The export for schema files must be named typeDef, for resolver files queries or mutations and Datasource for datasource files.