Code Monkey home page Code Monkey logo

apollo-discover-resolvers's Introduction

apollo-discover-resolvers

An opinionated helper to automagically import your GraphQL resolvers and build your resolver object for an Apollo Server.

Apollo Discover Resolvers

Install

yarn add apollo-discover-resolvers

or

npm i apollo-discover-resolvers --save

Usage

Apollo discover resolvers recursively searches through the directory passed into the function.

import { ApolloServer, gql } from 'apollo-server';
import { discoverResolvers } from 'apollo-discover-resolvers';

// A map of functions which return data for the schema.
const resolvers = discoverResolvers('absolute/path/to/resolvers/directory');

// The GraphQL schema
const typeDefs = gql`
  type CraftBeer {
    name: String!
    abv: Int!
  }

  input CraftBeerInput {
    name: String
    abv: Int
  }

  type Query {
    "A simple type for getting started!"
    craftBeers: CraftBeer
  }

  type Mutation {
    updateCraftBeer(input: CraftBeerInput!): CraftBeer!
  }
`;

const server = new ApolloServer({
  typeDefs,
  resolvers,
});

Resolver Folder Structure

Apollo discover resolvers takes a single parameter directory which is the path to your resolver folder. From there, the module will perform a synchronous glob search for resolver files. The resolvers use the folder structure to build the Resolver object. Any files under queries will be set on the Query property and likewise for mutations.

.
โ”œโ”€โ”€ resolvers
|  โ”œโ”€โ”€ index.ts
|  โ”œโ”€โ”€ mutations
|     โ””โ”€โ”€  updateCraftBeer.ts
|  โ””โ”€โ”€ queries
|     โ””โ”€โ”€  craftBeers.ts

Resolvers

The module will build a Resolver object to be passed into your Apollo Server. Each resolver file should export a function named to match the query or mutation.

// ./resolvers/queries/craftBeers.ts
export const craftBeers = (parent, args, context, info) => {}

// ./resolvers/mutations/craftBeers.ts
export const updateCraftBeer = (parent, args, context, info) => {}

Resolver Object

const resolvers = {
  Mutation: {
    updateCraftBeer: (parent, args, context, info) => {},
  },
  Query: {
    craftBeers: (parent, args, context, info) => {},
  },
}

Example App

Setup

git clone https://github.com/kmills006/apollo-discover-resolvers.git
cd apollo-discover-resolvers/example

yarn install
yarn dev

Todo

  • README
  • Test Suite

apollo-discover-resolvers's People

Contributors

kmills006 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.