Giter Club home page Giter Club logo

graphql-import-files's Introduction

npm

Build Status Coverage Status GitHub code size in bytes Npm Version GitHub

graphql-import-files

Light and easy package that will load .graphql files and use them with syntax highlighting.

Install

npm i -S graphql-import-files

Usage:

Loading a single file

For example, your files and folders look like this:

root
├──src
  ├──index.js
  ├──schema
    ├──schema.graphql

Use the function loadFile that will load a single file.

// index.js
const { ApolloServer } = require('apollo-server')
const { loadFile } = require('graphql-import-files')
const resolvers = require('./resolvers')

const server = new ApolloServer({
  typeDefs: loadFile('./schema/schema.graphql'), // Always consider the path at the root of the project
  resolvers
})

server.listen().then(({ url }) => {
  console.log(`Running on ${url}`)
})

The loadFile function supports the #import annotation and this will allow you to load other files as if it were the import in a js file.

root
├──src
  ├──index.js
  ├──schema
    ├──schema.graphql
    ├──user
      ├──user.graphql
    ├──location
      ├──location.graphql

./src/schema/schema.graphql

#import './user/user.graphql'
#import './location/location.graphql'

type Query {
  user(id: ID!): User
  location(id: ID!): Location
}

./src/schema/user/user.graphql

type User {
  id: ID!
  name: String!
}

./src/schema/location/location.graphql

type Location {
  id: ID!
  name: String!
}
// index.js
// ...
const { loadFile } = require('graphql-import-files')

const server = new ApolloServer({
  typeDefs: loadFile('./schema/schema.graphql'), // Always consider the path at the root of the project
  resolvers
})
// ...

Loading multiple files

For example, your files and folders look like this:

root
├──src
  ├──index.js
  ├──schemas
    ├──query.graphql
    ├──user
      ├──user.gql
      ├──role.gql
    ├──product
      ├──product.graphql

Use the function loadFiles that will load multiple files.

// index.js
const { ApolloServer } = require('apollo-server')
const { loadFiles } = require('graphql-import-files')
const resolvers = require('./resolvers')

const server = new ApolloServer({
  typeDefs: loadFiles('**/schemas/**/*.{graphql,gql}'), // Use the glob pattern to find multiple files
  resolvers
})

server.listen().then(({ url }) => {
  console.log(`Running on ${url}`)
})

graphql-import-files's People

Contributors

dependabot[bot] avatar tiago154 avatar

Stargazers

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

Watchers

 avatar  avatar  avatar

Forkers

bhabermann

graphql-import-files's Issues

Add #import support

It could be awesome to add the supportt of #import, really useful for the fragments.
Example:

# File user.fragment.gql

fragment UserFragment on User {
  id
  email
}
# File query.user.gql

#import "./user.fragment.gql"
query user {
  user {
    ...UserFragment
  }
}

and then we just need to loadFile('query.user.gql');.

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.