1.1.0 • Published 11 months ago

@liquid-js/fragql v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
11 months ago

FraGQL

GitHub license npm scope

Another GraphQL template literal.

Installation

npm install @liquid-js/fragql

API Documentation

https://liquid-js.github.io/fragql/

Usage

With fragments

import { gql } from '@liquid-js/fragql'

const userBasic = gql`
  fragment userBasic on User {
    name
    lastname
  }
`

const query = gql`
  query user($id: String){
    user(id: $id){
      ...${userBasic}
    }
  }
`

console.log(query.toString())

Gives

query user($id: String) {
  user(id: $id) {
    ...userBasic
  }
}

fragment userBasic on User {
  name
  lastname
}

Flatten fragments

console.log(query.toString(true));

Gives

query user($id: String) {
  user(id: $id) {
    name
    lastname
  }
}

Schema validation

FraGQL can validate operations against GQL schema. To use validation, you have to pass your schema as a string to loadSchema function.

Validating agains GraphQL endpoint

The following example obtains GraphQL schema from http://example.com/graphql and validates user query against it.

Notice userBasic fragment and user query don't wait for the schema to load - upon calling loadSchema, all existing queries are validated against it.

import { gql, loadSchema } from '@liquid-js/fragql'
import { buildClientSchema, getIntrospectionQuery } from 'graphql'

fetch('http://example.com/graphql', {
    method: 'POST',
    mode: 'cors',
    cache: 'no-cache',
    credentials: 'same-origin',
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    },
    redirect: 'follow',
    referrer: 'no-referrer',
    body: JSON.stringify({ query: getIntrospectionQuery() })
})
    .then((res) => res.json())
    .then((res) => buildClientSchema(res.data))
    .then((schema) => loadSchema(schema))
    .then((errors) => {
        if (errors.length) throw errors[0]
    })

const userBasic = gql`
  fragment userBasic on User {
    name
    lastname
  }
`

const query = gql`
  query user($id: String){
    user(id: $id){
      ...${userBasic}
    }
  }
`
1.1.0

11 months ago

1.0.0

11 months ago

0.11.0

11 months ago

0.10.2

2 years ago

0.10.3

2 years ago

0.10.1

2 years ago

0.10.0

2 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.0

5 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

7 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.0

7 years ago

0.2.0

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.7

8 years ago

0.0.6

8 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago