GithubHelp home page GithubHelp logo

gql-cf's Introduction

GraphQL Conditional Fields

If you've stumbled upon this randomly... well you're probably in the wrong neighborhood, to be honest.

In attempting to consolidate queries shared between 6 different repositories, but with very slight variations on the queries themselves (added or missing fields, etc), I came upon the idea of making these things conditional.

Now, if you're a fervent user of GraphQL you might recoil in horror at the thought of conditional queries, since the entire point of GraphQL is for queries to be individual, but, for our purposes, it works fine.

Currently not really useable

I mean, honestly, this is harcoded with a Typescript definition for the data we're sending to it, which means, you probably can't use it as-is. But... well, stay tuned for a more generic version sooner or later (probably later, lots to do lots to do!)

Usage

Start by installing this thing:

npm i gql-cf
or
yarn add gql-cf

Then, initialize it with the required values. This should be done in your index.js or app.js, as close to the top as possible, before any components using it load up.

import { ConditionalField } from 'gql-cf'

ConditionalField.init({
  web: true,
  resident: true
})

With that being done, you can now call queries in a dynamic fashion. This is done by using the cf template literal tag, and inserting functions within the template literal to conditionally add fields. I'm not sure how to actually describe this in technical terms, my brain is mush right now, but here's an example of what I mean:

import { gql } from '@apollo/client'
import { ConditionalField } from 'gql-cf'
const { cf } = ConditionalField

export const USER_FIELDS = cf`
  id
  username
  firstName
  lastName
  ${t => t.web && `
  lastLogin
  avatar
  `}
  ${t => t.mobile && `
  tinyAvatar
  lastMobileLogin
  `}
  ${t => t.manager && `
  passwordStrength
  email
  signupDate
  payment {
    method
    price
    lastPayment
  }
  `}
`

How's this useful? Well, for each of our project we just define the context, then use this context to filter out the fields we need.

Marvelous. And, again, I cannot stress this enough: probably useless to you right now and maybe ever. So... uhm... don't... use it? I guess?

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.