amshalem / graphql-import

Import & export definitions in GraphQL SDL

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphql-import

Discord Chat

Install

yarn add graphql-import@beta

Usage

import { importSchema } from 'graphql-import'
import { makeExecutableSchema } from 'graphql-tools'

async function start() {
  const typeDefs = await importSchema('schema.graphql'); // or .gql or glob pattern like **/*.graphql
  const resolvers = {};

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

main().catch(err => console.error(err));

Assume the following directory structure:

.
├── schema.graphql
├── posts.graphql
└── comments.graphql

schema.graphql

# import Post from "posts.graphql"

type Query {
  posts: [Post]
}

posts.graphql

# import Comment from 'comments.graphql'

type Post {
  comments: [Comment]
  id: ID!
  text: String!
  tags: [String]
}

comments.graphql

type Comment {
  id: ID!
  text: String!
}

Running importSchema('schema.graphql') produces the following output:

type Query {
  posts: [Post]
}

type Post {
  comments: [Comment]
  id: ID!
  text: String!
  tags: [String]
}

type Comment {
  id: ID!
  text: String!
}

Updating from 0.7.x

Install the new version as in Install step and after that update your code as in Usage step because importSchema is not sync anymore and returns promise,. We recommend you to use async/await to make the migration simple. The second parameter is now options. If you want to provide preresolved type definitions as in 0.7.x, use the method below; Before

const finalSchema = importSchema('somePointer', {
  'mySchema': `
      type Query {
        foo: String
      }
    `
})

After

const finalSchema = await importSchema('somePointer', {
  cache: {
    'mySchema': {
      rawSDL: `
        type Query {
          foo: String
        }
      `
    }
  }
})

About

Import & export definitions in GraphQL SDL

License:MIT License


Languages

Language:TypeScript 86.6%Language:JavaScript 13.4%