0.10.7 • Published 4 years ago

@graphql-toolkit/graphql-file-loader v0.10.7

Weekly downloads
194,568
License
MIT
Repository
github
Last release
4 years ago

tool

Discord Chat

A set of utils for faster development of GraphQL tools. Use these utils if you are creating a tool that loads schema/documents, merges schemas, scan for schema/documents/resolvers files.

Included tools:

Schema Loading

These utils are useful for scanning, loading and building a GraphQL schema from any input.

You can specify a GraphQL endpoint, local introspection JSON file, code file that exports a GraphQLSchema, AST string and .graphql files (with support for glob expression).

All found schema files can be merged into a complete schema. There is support for #import syntax (using graphql-import).

The user is given the option of implementing their own loader (implement the interface SchemaLoader).

The schema loading util is using loaders, and implemented using chain-of-responsibility pattern.

Specifiying the loader is not necessary. The user need only provide the inputs. The utils will detect it automatically.

Usage:

import { loadSchema } from 'graphql-toolkit';

const schema1 = loadSchema('type A { foo: String }'); // load from string
const schema2 = loadSchema('http://localhost:3000/graphql'); // load from endpoint
const schema3 = loadSchema('./schema.json'); // load from local json file
const schema4 = loadSchema('schema.graphql'); // load from a single schema file
const schema5 = loadSchema('./src/**/*.graphql'); // load from multiple files using glob

Documents Loading

Similar to schema loading - but meant to use for GraphQL documents (query/mutation/subscription/fragment).

Any input provided as a source will be recognized by utils automatically.

It also extracts usages of gql from code files using graphql-tag-pluck.

Usage:

import { loadDocuments } from 'graphql-toolkit';

const document1 = loadDocuments('query { f }'); // load from string
const document2 = loadDocuments('./users.query.graphql'); // load from a single file 
const document3 = loadDocuments('./src/**/*.graphql'); // load from multiple files using glob
const document4 = loadDocuments('./src/my-component.ts'); // load from code file

Epoxy

Originally implemented in graphql-modules. This tools merged GraphQL type definitions and schema. It aims to merge all possible types, interfaces, enums and unions, without conflicts.

Resolvers are merged using deep-merge. Resolver implementations can be separated across multiple objects and then merged into a single resolvers object.

# a1.graphql
type A {
    f1: String
}

# a2.graphql
type A {
    f2: String
}

Will result:

type A {
    f1: String
    f2: String
}

Sonar

Sonar is a small util that scans you file-system and find GraphQL files (.graphql) and resolvers files (.js/.ts) and loads them (using readFile for GraphQL files and require for resolvers files).

Other Utils

There are some more utils under utils directory:

validateGraphQlDocuments

A tool for validating GraphQL documents (query/mutation/subscription/fragment) against the schema.

It uses the original validation logic from graphql package, but suspressing some validation rules, and focuses on validating the fields and the structure of the documents.

extractDocumentStringFromCodeFile

This method gets a source code file, and using graphql-tag-pluck tries to extract GraphQL AST from it.

getDirectives

This method accepts GraphQLSchema and any AST node, and tries to extract the GraphQL directives of the AST node.

It returnes a Map of the directive name and the arguments of it.

getFieldsWithDirectives

This method accepts a GraphQL DocumentNode of a GraphQL types definition and creates a map between GraphQL type.field and the directives it has.

It's useful for getting an easy-to-use structure of the directives that are decorating the schema.

getImplementingTypes

This method accepts GraphQLSchema object and a name of a GraphQL interface, and returns an array of all the GraphQL types that are implementing the GraphQL interface.

getSchemaDirectiveFromDirectiveResolver

This method accepts a name of a GraphQL Directive and its resolver function; using this method you can easily generate SchemaDirective with a single resolver function.

composeResolvers

This method accepts IResolvers object and mappings for composition functions that would be run before resolver itself.

Instead of doing this,

const resolvers ={
    Query: {
        myQuery: (root, args, context) => {
            // Make sure that the user is authenticated
            if (!context.currentUser) {
                throw new Error('You are not authenticated!');
            }

            // Make sure that the user has the correct roles
            if (!context.currentUser.roles || context.currentUser.roles.includes('EDITOR')) {
                throw new Error('You are not authorized!');
            }

            // Business logic
            if (args.something === '1') {
                return true;
            }

            return false;
        },
    },
};

You can do;

const resolvers ={
    Query: {
        myQuery: (root, args, context) => {
            if (args.something === '1') {
                return true;
            }

            return false;
        },
    },
};

const isAuthenticated = () => next => async (root, args, context, info) => {
    if (!context.currentUser) {
        throw new Error('You are not authenticated!');
    }

    return next(root, args, context, info);
};

const hasRole = (role: string) => next => async (root, args, context, info) => {
    if (!context.currentUser.roles || context.currentUser.roles.includes(role)) {
        throw new Error('You are not authorized!');
    }

    return next(root, args, context, info);
};

const resolversComposition = {
    'Query.myQuery': [isAuthenticated(), hasRole('EDITOR')],
};

const composedResolvers = composeResolvers(resolvers, resolversComposition);

extractResolversFromSchema

This methods accepts GraphQLSchema object, and returns a map with field resolver functions of all types inside the schema as in IResolvers interface of graphql-tools.

extractFieldResolversFromObjectType

This methods accepts GraphQLObjectType or GraphQLInterfaceType object, and returns a map with field resolvers of given type.

0.10.7

4 years ago

0.10.6

4 years ago

0.10.5

4 years ago

0.10.4

4 years ago

0.10.3

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.9.12

4 years ago

0.9.11

4 years ago

0.9.10

4 years ago

0.9.9

4 years ago

0.9.8

4 years ago

0.9.7

4 years ago

0.9.6

4 years ago

0.9.5

4 years ago

0.9.4

4 years ago

0.9.3

4 years ago

0.9.2

4 years ago

0.9.1

4 years ago

0.9.0

4 years ago

0.8.1

4 years ago

0.8.0

4 years ago

0.7.5

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.7.1

4 years ago

0.7.0

4 years ago

0.6.8

4 years ago

0.6.7

4 years ago

0.6.6

4 years ago

0.6.5

4 years ago

0.6.5-alpha.0

4 years ago

0.6.4

4 years ago

0.6.4-alpha.0

4 years ago

0.6.3

4 years ago

0.6.3-alpha.6

4 years ago

0.6.3-alpha.5

4 years ago

0.6.3-alpha.4

4 years ago

0.6.3-alpha.3

4 years ago

0.6.3-alpha.2

4 years ago

0.6.3-alpha.1

4 years ago

0.6.3-alpha.0

4 years ago

0.6.2

4 years ago

0.6.2-alpha.0

4 years ago

0.6.1-alpha.1

4 years ago

0.6.1-alpha.0

4 years ago

0.6.0

4 years ago

0.5.19-beta.0

4 years ago