1.3.0 • Published 7 months ago

graphql-import-files v1.3.0

Weekly downloads
478
License
MIT
Repository
github
Last release
7 months ago

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}`)
})
1.3.0

7 months ago

1.2.19

1 year ago

1.2.18

2 years ago

1.1.18

2 years ago

1.0.18

2 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago