Coder Social home page Coder Social logo

utils-graphql's Introduction

@jumpn/utils-graphql

GraphQL utilities

Installation

Using npm

$ npm install --save @jumpn/utils-graphql

Using yarn

$ yarn add @jumpn/utils-graphql

Types

type GqlErrorLocation = {|
  line: number,
  column: number
|};

type GqlError = {|
  message: string,
  locations?: Array<GqlErrorLocation>
|};

type GqlRequest<Variables: void | Object = void> = {|
  operation: string,
  variables?: Variables
|};

type GqlRequestCompat<Variables: void | Object = void> = {|
  query: string,
  variables?: Variables
|};

type GqlResponse<Data> = {|
  data?: Data,
  errors?: Array<GqlError>
|};

type GqlOperationType = "mutation" | "query" | "subscription";

API

errorsToString

Transforms an array of GqlError into a string.

Parameters

  • gqlErrors Array<GqlError>

Examples

const gqlRespose = {
  errors: [
    {message: "First Error", locations: [{column: 10, line: 2}]},
    {message: "Second Error", locations: [{column: 2, line: 4}]}
  ]
}

const error = errorsToString(gqlRespose.errors);
// string with the following:
// First Error (2:10)
// Second Error (4:2)

Returns string

getOperationType

Returns the type (query, mutation, or subscription) of the given operation

Parameters

Examples

const operation = `
  subscription userSubscription($userId: ID!) {
    user(userId: $userId) {
      id
      name
    }
  }
`;

const operationType = getOperationType(operation);

console.log(operationType); // "subscription"

Returns GqlOperationType

hasSubscription

Returns true if documentNode has a subscription or false otherwise

Parameters

  • documentNode DocumentNode

Returns boolean

requestFromCompat

Creates a GqlRequest using given GqlRequestCompat

Parameters

  • gqlRequestCompat GqlRequestCompat<Variables>
    • gqlRequestCompat.query
    • gqlRequestCompat.variables

Examples

const query = `
  query userQuery($userId: ID!) {
    user(userId: $userId) {
      id
      email
    }
  }
`;

console.log(requestFromCompat({query, variables: {userId: 10}}));
// {operation: "...", variables: {userId: 10}}

Returns GqlRequest<Variables>

requestToCompat

Creates a GqlRequest using given GqlRequestCompat

Parameters

  • gqlRequest GqlRequest<Variables>
    • gqlRequest.operation
    • gqlRequest.variables

Examples

const operation = `
  query userQuery($userId: ID!) {
    user(userId: $userId) {
      id
      email
    }
  }
`;

console.log(requestToCompat({operation, variables: {userId: 10}}));
// {query: "...", variables: {userId: 10}}

Returns GqlRequestCompat<Variables>

License

MIT Šī¸ Jumpn Limited

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.