Git Product home page Git Product logo

graphql-fragments's Introduction

GraphQL Fragments

NOTE

This package is now deprecated, it's functionality is available in graphql-anywhere and graphql-tag.

Migrating

Should be fairly straightforward.

Instead of using

const fragment = new Fragment(`fragment ...`);

you can just use

import gql from 'graphql-tag';

const fragment = gql`fragment ...`;

No need to pass X.fragments() around in a fragments option to watchQuery, graphql() etc.

Instead, embed the fragment in your query document directly with:

import gql from 'graphql-tag';
const query = gql`
  query {
    field {
      ...MyFragmentName
    }
  }
  ${fragment}
`;

Fragment utilities now come a functions from graphql-anywhere

So it's

import gql from 'graphql-tag';
import { filter, check, propType } from 'graphql-anywhere';

const fragment = gql`fragment ...`;
const doc = { some: data };

filter(fragment, doc);
check(fragment, doc);

X.propTypes = {
  // Note you probably want to use `isRequired`
  name: propType(fragment).isRequired,
}

See also http://dev.apollodata.com/react/fragments.html


Tools for dealing with GraphQL fragments.

Build Status npm version

Installing

npm install -S graphql-fragments

Usage

import Fragment from 'graphql-fragments';
import gql from 'grapqhl-tag';

const fragmentDoc = gql`
  fragment Foo on Bar {
    field1
    field2
    ...SubFragment
  }
`;

// subFragment is the Fragment which defines `SubFragment` above
const fragment = new Fragment(fragmentDoc, subFragment);

API

fragment.fragments()

Return a list of fragment documents, suitable to be passed into client.watchQuery

fragment.filter(data);

Assuming that data is of the type this fragment applies to, and is the result of a query that includes this fragment, filter out only the fields of data that are defined by this fragment.

fragment.check(data)

Throw an error if data does not include all the fields that this fragment defines.

fragment.propType

A function, much like fragment.check, suitable to be used by in a React propTypes check.

Caveats

As we cannot know if a conditional sub-fragment should apply, we take a conservative approach:

  • when checking we do not error if fields from a conditional sub-fragment are missing
  • when filtering, we take sub fields from conditional sub-fragments where possible.

graphql-fragments's People

Contributors

tmeasday avatar

Watchers

 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.