1.0.2 • Published 5 years ago

graphiosts-cmd v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Logo

GraphiosTs-Cmd

Commandline utility for converting GraphQl schema from server into GraphiosTs schema definition.

Instalation

Using npm:

npm install -g graphiosts-cmd

Usage

Config file

Anywhere in your project create file graphiosts.json:

{
    "Explanation-Name-of-Schema":{
        "url":"Url to the GraphQl endpoint. Introspection has to be allowed",
        "path":"Destination path. All folders has to exist",
        "interceptor":"Interceptors are functions, which are called before server is called. It can be used for authorization etc. More in Interceptor section",
        "customScalarsPath":"Json file with definition of custom Scalars. More in Scalars section",
        "headers":{
            "Authorization":"Custom headers. Can be modified in Interceptor function"
        }
    },
    "swapi":{
        "url":"https://swapi.graph.cool",
        "path":"./swapi.graphql.ts",
        "intercpetor":"./auth.interceptor.js",
        "customScalarsPath":"./scalars.json",
        "headers":{
            "Authorization":"Bearer 123"
        }
    }
}

Calling server

Then in command line go to the folder, where graphiosts.json file is located and call:

graphiosts-cmd compile

or if you want to compile only selected schema:

graphiosts-cmd compile <name>

Interceptor function

Is a *.js file with one function, which is called before server is fetched. It can be used to preflight authorization or conditional modification of config file. Path to the file is defined in graphiosts.json > interceptor

//Compiler gives a config file graphiosts.json as an argument
function interceptor(config){
    config.headers = {
        'NewHeader':'Bar'
    }
    //Compiler expects to return the config;
    return config;
}
//Alternatively you can return a Promise which will return a config file
function promiseInterceptor(config){
    return new Promise((resolve,reject)=>{
        Axios.create().request({
            //...some request
        }).then((data)=>{
            config.headers = {
                //...modification
            }
            resolve(config);
        }).catch((e)=>{
            //Error handle
            resolve(config);
        })
    })
}

module.exports = interceptor;

Scalars config

In many cases GraphQl schema defines custom scalar types, but those types does not have definition for Typescript. By default compiler will transform any unknown scalar to any type. But if you want to make a better assertion for the scalar, you can use scalar config file. It is plane *.json file. You can specify a path to it in graphiosts.json > customScalarsPath.

Example of file:

{
    "Date":"number",
    "Time":"string",
    "Currency":"In rare cases scalars can be an object > {amount:number;type:string}"
}

Schema usage

Passing schema to GraphiosTs

import schema from 'path/to/schema';
import {GraphiosTs} from 'graphios-ts';
import Axios from 'axios';

const gts = new GraphiosTs<schema>(Axios.create);

Using schema to define fragment

import schema,{namespace} from 'path/to/schema';
import {GraphiosTs} from 'graphios-ts';
import Axios from 'axios';

const fragment:GraphTsPayload<namespace.query.Film['payload']> = {
    id:true,
}

const gts = new GraphiosTs<schema>(Axios.create).create('query','Film').gql(
    {
        args:{
            id:'123'
        },
        payload:{...{name:true},...fragment}
    }
);
1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago