Giter Site home page Giter Site logo

graphql-to-js-client-builder's Introduction

graphql-to-js-client-builder

This is a generic function that will take in a GraphQL AST, and produce a javascript AST or code representing the query builder syntax used by graphql-js-client.

Table Of Contents

Installation

$ yarn add graphql-to-js-client-builder

Usage

There are two ways to use this package. The simplest way is to call the default export from the main package file. It receives GraphQL code, and variable names, and returns javascript. More complex use cases exist, such as integrating the generated code into other code. This requires a function that returns a javascript AST. See both examples below.

Generating Code

The following example takes GraphQL code, and returns javascript, utilizing graphql-js-client's query builder syntax.

import transform from 'graphql-to-js-client-builder';

const code = transform(`
query ($id: ID!) {
  node(id: $id) {
    ... on Product {
      name
      price
    }
  }
}`, 'client', 'document', 'spreads');

The above code will generate the following javascript:

const document = client.document();

document.addQuery([client.variable('id', 'ID!')], root => {
  root.add("node", {
    args: {
      id: client.variable("ID")
    }
  }, node => {
    node.addFragment('Product', Product => {
      Product.add('name');
      Product.add('price');
    });
  });
});

Generating an AST

The following example takes GraphQL code, and returns a babel AST, utilizing graphql-js-client's query builder syntax.

import {transformToAst} from 'graphql-to-js-client-builder';

const ast = transformToAst(`
query ($id: ID!) {
  node(id: $id) {
    ... on Product {
      name
      price
    }
  }
}`, {
  clientVar: 'client',
  documentVar: 'document',
  spreandsVar: 'spreads',
  variablesVar: 'variables'
}

The above code would return an AST representing the same javascript code that was shown in the previous example.

License

MIT, see [LICENSE.md](http://github.com/Shopify/graphql-to js-client-builder/blob/master/LICENSE.md) for details.

graphql-to-js-client-builder's People

Contributors

minasmart avatar

Watchers

James Cloos avatar Stephen Richardson  avatar  avatar

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.