Code Monkey home page Code Monkey logo

graphqlizer's Introduction

graphqlizer

Build Status Code Climate Maintainability Get help on Codementor

Make your meteor mongo collections accessible over a graphql endpoint.

  • Generates simple type definitions + resolvers
  • Uses check and SimpleSchema to validate arguments
  • Great for prototyping and beyond

Basic Code Example

This package does not fully abstract GraphQL, so be sure to learn about it so that you have the needed vocabulary and knowledge to profit from this package. The GraphQL site has a cool text based introduction and if you're a visual kind of person like me, you can check out my introduction screencast.

If you want a simple usage example check out the test app.

How to install

# Install npm packages
meteor npm install --save apollo-client graphql-server-express express graphql graphql-tools body-parser
# Install meteor packages
meteor add apollo aldeed:simple-schema easy:graphqlizer

How to use

Graphqlizer uses SimpleSchema to generate the resolvers and type definitions. The most basic configuration requires you to pass the mongo collection and a name (TitleCase) to identify your data.

import { crud } from 'meteor/easy:graphqlizer'
import { CarCollection } from '{...}'

const alienSchema = crud('Car', CarCollection)
// or with a custom schema
const otherAlienSchema = crud('Car', CarCollection, CustomCarSimpleSchema)

You can optionally specify a custom schema as the 3rd argument. Graphqlizer uses the simple schema configuration to infer the graphql type fields (Int, Float etc) and if it's optional or required (!).

To actually have access to your data you have to use the generateTypeDefsAndResolvers function to generate the final typeDefs and resolvers to create the GraphQL schema.

import { crud, generateTypeDefsAndResolvers } from 'meteor/easy:graphqlizer'
import { createApolloServer } from 'meteor/apollo'
import { makeExecutableSchema } from 'graphql-tools'

// ...

const { typeDefs, resolvers } = generateTypeDefsAndResolvers({
  schemas: [alienSchema],
})

const schema = makeExecutableSchema({
  typeDefs,
  resolvers,
})

createApolloServer({ schema, graphiql: true })

Notice that the generateTypeDefsAndResolvers expects a list of graphqlizer schemas to generate the type definitions and resolvers.

Beyond CRUD

Anytime you need to go beyond the basic CRUD you can use the following composable methods to extend your schema:

  • resolvers(name, Collection[, CustomSimpleSchema]) => {resolverObject} CRUD resolvers
  • typeDefs(name, SimpleSchema) => [String] CRUD type definitions
  • resolver.{create|update|delete|get|list}(Collection[, CustomSimpleSchema]) => Function Specific resolver
  • typeDef.{type|input|create|update|delete|get|list}(name, SimpleSchema) => String Specific type definition
import { resolvers, typeDefs } from 'meteor/easy:graphqlizer'

export default {
  resolvers: {
    ...resolvers('Book', BookCollection),
    Book: {
      relatedMovies (root, args) {
        return /* ... */
      },
    },
  },
  typeDefs: [
    ...typeDefs('Book', BookCollection.simpleSchema()),
    `
    extend type Book {
      relatedMovies: [Movie]   
    }
    `,
  ],
}

In the example above we extend Book with an additional field called relatedMovies. Be aware though that if you want to add a new Query or Mutation you need to use a deep merge function!

If you want to go even more granular you can use the resolver and typeDef functions.

import { resolver, typeDef } from 'meteor/easy:graphqlizer'

export default {
  resolvers: {
    Query: {
      listBook: resolver.list(BookCollection),
      findBookFromFriends (root, args) {
        return /* ... */      
      },
    },
    Mutation: {
      createBook: resolver.create(BookCollection),
    },
  },
  typeDefs: [
    typeDef.type('Book', BookCollection.simpleSchema()),
    typeDef.input('Book', BookCollection.simpleSchema()),
    typeDef.list('Book'),
    typeDef.create('Book'),
    `
    extend type Query {
      findBookFromFriends: [Book]
    }
    `,
  ],
}

This way you can mix and match type definitions and resolvers and if your app grows more complex decouple it from graphqlizer completely, without having to refactor code.

graphqlizer's People

Contributors

matteodem avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

geppadee

graphqlizer's Issues

Refactor into mixins + create search mixin pkg

Allow to pass mixins (or some other naming) to extend graphqlize resolvers and type defs. Also refactor CRUD to be mixins (one for create, one for read etc).

Make the search mixin a separate package and provide some simple default mongo search query.

import { createCollectionSchema } from 'meteor/easy:graphqlizer'
import { searchMixin } from 'meteor/graphqlizer:search'

createCollectionSchema({ 
  type: 'Alien',
  collection: AlienCollection,
  schema: AlienSimpleSchema,
  mixins: [searchMixin],
})
const createMixin = {
  Mutation: {
  },
  typeDefs: `
extend type Mutation {
  ...
}
`
}

Verify if the config structure still makes sense with mixins. Probably deprecate crud config

Allow to register singular types

Something like

import moment from 'moment'
import { registerType } from 'meteor/easy:graphqlizer'

registerType(Date, {
  type: 'Date',
  resolve: field => moment(field),
})

This allows to define custom types that map a simple schema type to a graphql type.

Adjust API

So that incrementally changing from graphqlizer CRUD to more complex schemas doesn't result in unreadable code. Also eliminate graphqlContext.

https://gist.github.com/matteodem/9afd3b5401a9aa0127f3cf459a6a0b1a

TODO

  • Add tests
  • Add new API methods (crud, resolvers, resolver, typeDefs, typeDef)
  • Remove package-lock.json from repo (gitignore)
  • Throw deprecation warnings when using old methods
  • Add registerType method (see #10)
  • Create MIGRATE-1.0.md file that explains how to upgrade
  • Update docs
  • Remove old docs
  • Release v0.* with deprecating warnings
  • Update graphqlizer-test-app
  • Use new API in Sounds Social
  • Include carbon image in README and link it to github gist
  • Remove old methods / tests
  • Use simpl-schema npm package instead of atmosphere pkg
  • Release v1.0 with warnings
  • Announce release

Relationships

Describe how relationships between types can be handled

Final tests

  • Create test for generateTypeDefsAndResolvers method
  • Use in own apps
  • How to instantiate empty initial Query and Mutation?

Custom fields

Describe how to customize input / type fields for graphql resolvers + type defs

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.