1.0.1 • Published 5 years ago

normalize-graph-ql v1.0.1

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

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
        }
    }
}