normalize-graph-ql
v1.0.1
Published
normalize graphql schema
Downloads
1
Readme
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": "[email protected]",
"lastName": "some lastName",
"middleName": "some middleName",
"firstName": "some firstName",
"webinars": [
{
"url": "some URL",
"webinar": 1
}
]
}
}
},
"result": {
"data": {
"account": 1
}
}
}