New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

normalize-graph-ql

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

normalize-graph-ql

normalize graphql schema

latest
npmnpm
Version
1.0.1
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

normalize-graph-ql

Uses the normalizr library to normalize a GraphQL query response.

Example of use:

  • Get your GraphQL schema from the server using the query:
query IntrospectionQuery {
    __schema {
        queryType { name }
        mutationType { name }
        subscriptionType { name }
        types { ...FullType }
        directives {
            name
            description
            locations
            args { ...InputValue }
        }
    }
}
fragment FullType on __Type {
    kind
    name
    description
    fields(includeDeprecated: true) {
        name
        description
        args { ...InputValue }
        type { ...TypeRef }
        isDeprecated
        deprecationReason
    }
    inputFields { ...InputValue }
    interfaces { ...TypeRef }
    enumValues(includeDeprecated: true) {
        name
        description
        isDeprecated
        deprecationReason
    }
    possibleTypes { ...TypeRef }
}
fragment InputValue on __InputValue {
    name
    description
    type { ...TypeRef }
    defaultValue
}
fragment TypeRef on __Type {
    kind
    name
    ofType {
        kind
        name
        ofType {
            kind
            name
            ofType {
                kind
                name
                ofType {
                    kind
                    name
                    ofType {
                        kind
                        name
                        ofType {
                            kind
                            name
                            ofType {
                                kind
                                name
                            }
                        }
                    }
                }
            }
        }
    }
}
  • Save response to a file schema.json.
  • Get a response to your request from the server and save it to a file response.json.
  • Using previous files, normalize our query:
import { normalize } from 'normalizr';
import schema from './schema.json';
import response from './response.json';
import querySchema from 'normalize-graph-ql';

const query = querySchema(schema, `
query { 
	account(id: 1) {
        id
        email
        lastName
        middleName
        firstName
        webinars {
            url
            webinar {
                id
                title
                ticket {
                    id
                    price
                }
            }
        }
    }
}
`);

var result = normalize(response, query.schema);

Result:

{
    "entities": {
        "Ticket": {
            "1": {
                "id": 1,
                "price": 500
            }
        },
        "Webinar": {
            "1": {
                "id": 1,
                "title": "some title",
                "ticket": 1
            }
        },
        "Account": {
            "1": {
                "id": 1,
                "email": "some@email.com",
                "lastName": "some lastName",
                "middleName": "some middleName",
                "firstName": "some firstName",
                "webinars": [
                    {
                        "url": "some URL",
                        "webinar": 1
                    }
                ]
            }
        }
    },
    "result": {
        "data": {
            "account": 1
        }
    }
}

Keywords

graphql

FAQs

Package last updated on 17 Dec 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts