Giter VIP home page Giter VIP logo

polaris's Introduction

logo

Polaris

Build Status NPM version

A node.js express engine for creating GraphQL server easily, with base standardization and common types

Getting Started

This module exposes 2 fields: RunGraphQLServer method and the CommonEntities object. Every "polaris" entity is an object with the following fields:

  • typeDefs: string representation of the type definitions required by this entity.
    • for example, a Book entity definition will be supplied to typeDefs. type Book { id: string, name: string }
  • resolvers: an object that holds the resolver functions for this entity, as defined in GraphQL-Tools.
  • schemaDirectives: an object that holds the directives functions for this entity, as defined in GraphQL-Tools. The schema is a "polaris" entity by itself.

In order to run the server, you can use the following code:

const {RunGraphQLServer} = require('@enigmatis/polaris');
const Schema = require('./schema/schema');
RunGraphQLServer(Schema, 3000);

Where schema is your executable schema as defined by GraphQL-Tools.

The RunGraphQLServer function's arguments are the executable schema and the port you want to run your service on.

Attention - do not execute makeExecutableSchema(schema) - We will do this for you. You should supply the schema before the making.

An example of a schema

The schema.js file will look like this:

const {merge} = require('lodash');

// Get the Query Root object
const Query = require('./entities/query/rootQuery');

// Get the Mutation Root object
const Mutation = require('./entities/mutation/rootMutation');

// Create the schema definition
const SchemaDefinition = `schema {query: Query, mutation: Mutation}`;

// Create the schema mutationResolvers
const resolvers = merge(Query.resolvers, Mutation.resolvers);

// Export an executable schema
module.exports = {
    // The schema is a combination of the schema definition, the Query types and the Mutation types
    typeDefs: [SchemaDefinition, ...Query.typeDefs, ...Mutation.typeDefs],
    resolvers,
    schemaDirectives: Query.schemaDirectives
};

We are using the lodash merge function in order to merge our objects (such as resolvers).

The queryRoot.js file will look like this:

// We use book in our Query object
const Book = require('./../output/book');
const {merge} = require('lodash');
// Get the Query's mutationResolvers
const resolvers = require('../../resolvers/queryResolvers');
// Create the type Query's schema
const Query = `type Query { books: [Book] }`;

module.exports = {
    // Combine the Query type schema with the Book types schema because we use it in the Query type
    typeDefs: [Query, ...Book.typeDefs],
    // Combine the Query mutationResolvers with the Book mutationResolvers
    resolvers: merge(resolvers, Book.resolvers),
    schemaDirectives: Book.schemaDirectives
};

And book.js:

const {CommonEntities: {commonEntityInterface, upperCaseDirective}} = require('@enigmatis/polaris');

// Define the Book type schema
const Book = `
    type Book implements CommonEntity {
        id: ID!
        creationDate: Date,
        lastUpdateDate: Date,
        dataVersion: Int!,
        title: String @upper,
        author: String
    }
`;

// Get the Book's mutationResolvers
const resolvers = require('../../resolvers/bookResolvers');

module.exports = {
    typeDefs: [Book, ...commonEntityInterface.typeDefs, ...upperCaseDirective.typeDefs],
    resolvers: resolvers,
    schemaDirectives: upperCaseDirective.schemaDirectives
};

Where CommonEntities are entities supplied by Polaris Engine and contain some default interface for entities, or common directives you can use in your GraphQL service.

polaris's People

Contributors

chenshoo avatar yarinvak avatar osher-sade avatar bikov avatar doctorvoid avatar semantic-release-bot avatar guy120494 avatar dependabot[bot] avatar

Watchers

James Cloos 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.