simply10w / graphql-to-normalizr

POC Convert graphql schema string into normalizr schema

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Graphql schema to normalizr schema

Helper functions that allow you to build normalizr schemas from graphql schemas.

A minimum TypeScript app with Parcel Bundler.

For more details, see src/.

Usage

To play around StackBlitz.

Example

import { normalize } from "normalizr";
import { buildNormalizrSchemas, generateRelationsString } from "./lib";

const graphqlSchema = `
    type User {
        id: String
        name: String
        parents: [User]
    }

    type Home {
        guid: String
        address: String
        tenants: [User]
    }
`;

const relationsGraphString = generateRelationsString(graphqlSchema);
const schemas = buildNormalizrSchemas<'User'|'Home'>(relationsGraphString, {
    User: 'id',
    Home: 'guid'
});

const home = {
    guid: 'home1',
    address: 'Home Address',
    tenants: [
        { id: 'user1', name: 'User 1', parents: [
            { id: 'user2', name: 'User 2' },
            { id: 'user4', name: 'User 4' }
        ] },
        { id: 'user3', name: 'User 3', parents: [
            { id: 'user2', name: 'User 2' },
            { id: 'user5' }
        ] },
    ] 
}

const normalized = normalize(home, schemas.Home);

The output will be:

{
    entities: {
        Home: {
            'home1': { guid: 'home1', address: 'Home Address', tenants: ['user1', 'user3']}
        },
        User: {
            'user1': { id: 'user1', name: 'User 1', parents: ['user2', 'user4'] }
            'user2': { id: 'user2', name: 'User 2', }
            'user3': { id: 'user3', name: 'User 3', parents: ['user2', 'user5'] }
            'user4': { id: 'user4', name: 'User 4' }
            'user5': { id: 'user5',  }

        }
    }
}

Installation

yarn install

Development

Launch development server.

yarn start

Author

License

MIT © simply10w

About

POC Convert graphql schema string into normalizr schema

License:MIT License


Languages

Language:TypeScript 96.5%Language:HTML 3.5%