Giter VIP home page Giter VIP logo

graphql-fragment-mask's Introduction

graphql-fragment-mask

CI Coverage Status npm LICENSE

Mask GraphQL query result with Fragment (graphql-anywhere alternative).

Usage

import gql from "graphql-tag";
// Import generated types and generated `TypedDocumentNode` objects of the fragment.
import { PostHeaderFragmentDoc, GetPostDocument } from "./__generated__/Post.generated";

const _POST_HEADER = gql`
  fragment PostHeader on Post {
    title
    author {
      fullName
      avatarUrl
    }
  }
`;

const _GET_GREETING = gql`
  query GetPost($postId: !String) {
    postById(postId: $postId) {
      ...PostHeader
      // and more fields...
    }
  }
`;

const [data] = useQuery(GetPostDocument, { variables: { postId: "123" } });


// Use typed document node generated by `@graphql-codegen/typed-document-node` (RECOMMENDED).
const header = maskWithFragment(PostHeaderFragmentDoc, data.postById);
// {
//   __typename: "Post",
//   title: "Hello, GraphQL!",
//   author: {
//     __typename: "User",
//     fullName: "Masayuki Izumi",
//     avatarUrl: "https://example.com/users/izumin5210/avatar",
//   }
// }

// Alternatively, you can use the document node defined by `graphql-tag`.
// const header = maskWithFragment(_POST_HEADER, data.postById);

Dependencies

Install this library with graphql-js.

yarn add graphql graphql-fragment-mask

If you want to use it with TypedDocumentNode, setup TypedDocumentNode plugin with graphql-code-generator (RECOMMENDED, please refer TypedDocumentNode's instruction).

yarn add --dev \
  @graphql-codegen/cli \
  @graphql-codegen/typescript \
  @graphql-codegen/typescript-operations \
  @graphql-codegen/typed-document-node
yarn add @graphql-typed-document-node/core

graphql-fragment-mask's People

Contributors

github-actions[bot] avatar izumin5210 avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

graphql-fragment-mask's Issues

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.