Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-gql-schema-composer

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-gql-schema-composer

GraphQL schema composer from different chunks with 0.1 dependencies. Small and tiny, exactly what you are looking for

  • 1.0.12
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
16
decreased by-23.81%
Maintainers
1
Weekly downloads
 
Created
Source

🟢 NodeJS GraphQL Composer

A tiny and minimalist, with minimum dependencies tool, which allows you to use, split and organize the native GQL files in your NodeJs project.





How it works?

You can use native gql files in your nodejs/graphql project. You can split and organize it according to your needs. You can call it as you want, you can nest it as you need. Keep your gql files small and simple.

Example:

Using promise chain:

const express = require('express')
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
const { composeSchema } = require('node-gql-schema-composer')

const app = express()
composeSchema('./gql')
  .then(schema => {
    app.use('/graphql', graphqlHTTP({
      schema: buildSchema(schema),
      rootValue: { hello: () => { return 'Hello world!' }}
      graphiql: true,
    }))
  })
  .then(() => app
    .listen(4000, () => console.log('Now browse to localhost:4000/graphql')))

Using async/await syntax:

const express = require('express')
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
const { composeSchema, dumpToFile, readFolder } = require('node-gql-schema-composer')

const start = async function () {

  const app = express()
  const schema = await composeSchema('./gql')

  app.use('/graphql', graphqlHTTP({
    schema: buildSchema(schema),
    rootValue: { hello: () => { return 'Hello world!' }}
    graphiql: true,
  }))

  app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'))
}


start()

With Apollo Server:

const express = require('express')
const { composeSchema, dumpToFile } = require('node-gql-schema-composer')
const  { ApolloServer } = require( 'apollo-server-express')
const { ApolloServerPluginDrainHttpServer } = require( 'apollo-server-core')
const http = require( 'http')
const Query = {
  hello: () => {
    return 'Hello world!'
  },
}


async function startApolloServer(resolvers) {
  const app = express()
  const typeDefs = await composeSchema('./gql')
  const httpServer = http.createServer(app)
  const server = new ApolloServer({
    typeDefs,
    resolvers,
    plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
  })

  await server.start()
  server.applyMiddleware({ app })
  await new Promise(resolve => httpServer.listen({ port: 4000 }, resolve))
  console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
}

startApolloServer({ Query })
 

Dump to the schema file:

const express = require('express')
const { graphqlHTTP } = require('express-graphql');
const { buildSchema } = require('graphql');
const { composeSchema, dumpToFile } = require('node-gql-schema-composer')

const start = async function () {

  const app = express()
  const schema = await composeSchema('./gql')

  app.use('/graphql', graphqlHTTP({
    schema: buildSchema(schema),
    rootValue: { hello: () => { return 'Hello world!' }}
    graphiql: true,
  }))

  app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'))
}

dumpToFile('./gql')  // it will create a composed schema file in the root folder

start()



Auto restart with nodemon:

You can the files extension to the nodemon and enjoy the auto-restarting behaviors on each change in gql files. Just add the following tweak to your script in the package.json file:

 "start": "nodemon --watch **/*.gql index.js"

and now your nodemon will be also sensitive to the changes in your .gql files:

❯ yarn start
yarn run v1.22.17
$ nodemon --watch **/*.gql index.js
[nodemon] 2.0.14
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): gql/index.gql
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
Now browse to localhost:4000/graphql

[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
[nodemon] restarting due to changes...
[nodemon] starting `node index.js`
Now browse to localhost:4000/graphql

Inside

The module provides 3 simple functions:

  function composeSchema( path ) {...}

  /*  async function which will compose on-the-fly all your chunks .gql files
  *   into one schema used by graphql
  *   
  *   PARAMS: 
  *     < path:string > the place where you will have all your .gql files/folders 
  */
  function dumpToFile( path, name ) {...}

  /*  a simple dump function which will compose all your chunks .gql files
  *   into one schema and will write it into one single file in the root folder, according to the name, which is has a default *   name "schema"
  *   
  *   PARAMS: 
  *     < path:string > the place where you will have all your .gql files/folders 
  *     < name:string | optional> the name of dump file ( default: "schema.gql")
  */
  function readFolder( path, name ) {...}

  /*  async internal function which will read and find all your .gql|.graphql files under provided path 
  */

Made with  ❤️   by Nudelman Alex

Keywords

FAQs

Package last updated on 09 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc