Coder Social home page Coder Social logo

graphql-js-schema's Introduction

Travis

graphql-js-schema

Transforms the JSON representation of a GraphQL schema into a set of ES6 type modules.

Table Of Contents

Installation

With Yarn:

$ yarn global add graphql-js-schema

With NPM:

$ npm install -g graphql-js-schema

Examples

To transform a GraphQL schema file (as json) into a set of ES6 consumable modules, run the following command.

graphql-js-schema --schema-file ./schema.json --outdir schema --schema-bundle-name="Types"

This will create a directory called schema, and a root module called Schema in the file schema/types.js. It will also collect all the non-scalar types in schema/types/, and export them. The top level bundle exists for convenience, but you can consume these modules however you like.

API

Exports one function that transforms a schema object into a list of files and their associated bodies.

import graphqlJsSchema from 'graphql-js-schema';

graphqlJsSchema(schemaHash, "BundleName").then((files) => {
  // Do stuff with hashes in the format:
  // {
  //   path: 'types/product.js',
  //   body: '...'
  // }
});

Schema Modules

import Schema from 'schema/schema';

Schema.Product.name // => Product
Schema.Product.implementsNode // => true
Schema.Product.kind // => OBJECT

// All type strings returned through `fieldBaseTypes` are available in the
// schema for further exploration.

Schema.Product.fieldBaseTypes.id // => ID
Schema.Product.fieldBaseTypes.handle // => String
Schema.Product.fieldBaseTypes.images // => Image
Schema.Product.fieldBaseTypes.options // => ProductOption
Schema.Product.fieldBaseTypes.productType // => String
Schema.Product.fieldBaseTypes.publishedAt // => DateTime
Schema.Product.fieldBaseTypes.tags // => String
Schema.Product.fieldBaseTypes.vendor // => String
Schema.Product.fieldBaseTypes.collections // => CollectionConnection
Schema.Product.fieldBaseTypes.variants // => ProductVariantConnection
Schema.Product.fieldBaseTypes.createdAt // => DateTime
Schema.Product.fieldBaseTypes.updatedAt // => DateTime

License

MIT, see LICENSE.md for details.

graphql-js-schema's People

Contributors

jamesmacaulay avatar jzjiang avatar lshapton avatar minasmart avatar shopify-admins avatar shopify-services avatar swalkinshaw avatar

Stargazers

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

Watchers

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

graphql-js-schema's Issues

On optimizing type bundle size

Problem

Right now, we're repeating a lot of data. A lot of similar information appears
on the field itself, and on the type's definition. There are also a couple other
things we could blow away.

  • isList: while this is convenient to have, it's only used during
    deserialization, and we can look at the payload to see if a thing is a list.
  • connection key: There's a spec on type name for this.
  • kind on field: We have the type name, look it up there.
  • args: Do we need them? What do we need?

Proposal

We could probably reduce things to the following:

const ProductConnection = {
  objects: {
    edges: 'ProductEdge',
    pageInfo: 'PageInfo'
  }
}

const QueryRoot = {
  objects: { ... },
  interfaces: ['Node'],
}

const Shop = {
  scalars: {
    createdAt: 'DateTime',
    countryCode: 'String',
    someOtherKey: 'Int',
  },
  objects: {
    products: 'ProductConnection',
    images: 'Image'
  }
}

With this subset, we'd only be able to deduce OBJECT or INTERFACE from
SCALAR from inference. If the type isn't on the bundle, it's a scalar.

Comments and discussion welcome!

Update public package publish config

👋 Shipit has recently been updated to support private npm package publishing. The @shopify scope for npm packages in Shipit now references our internal PackageCloud registry by default. You can read more about this here.

This library, however, uses a Shipit deploy override in shipit.yml which prevents Shipit from automating the public/private registry selection.

Since this library is public, please update the package.json by specifying the public registry in the publishConfig setting.

"publishConfig": {
  "access": "public",
  "@shopify:registry": "https://registry.npmjs.org/"
}

If possible, we also recommend removing the shipit.yml deploy override to leverage Shipit’s default deployment steps.

TypeError: Cannot read property '__schema' of undefined

When running

graphql-js-schema --schema-file ./types/types.json --outdir schema --schema-bundle-name="Types"

in the console, it returns
TypeError: Cannot read property '__schema' of undefined at generateSchemaModules (/usr/local/lib/node_modules/graphql-js-schema/lib/index.js:136:45) at runCli (/usr/local/lib/node_modules/graphql-js-schema/lib/cli.js:55:39) at Object.<anonymous> (/usr/local/lib/node_modules/graphql-js-schema/lib/cli.js:63:1) at Module._compile (internal/modules/cjs/loader.js:945:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10) at Module.load (internal/modules/cjs/loader.js:798:32) at Function.Module._load (internal/modules/cjs/loader.js:711:12) at Function.Module.runMain (internal/modules/cjs/loader.js:1014:10) at internal/main/run_main_module.js:17:11

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.