All files / src/definitions schema.ts

100% Statements 15/15
100% Branches 6/6
100% Functions 4/4
100% Lines 14/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67  8x 8x 8x     6x       6x 2x   6x 2x   6x 1x   6x     3x     3x     8x                                                                            
/**
 * Copyright (c) 2017-present, Graphene.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */
import {
  GraphQLObjectType,
  ExecutionResult,
  GraphQLDirective,
  GraphQLSchema,
  GraphQLNamedType,
  graphql,
  printSchema
} from "graphql";
 
import { getGraphQLType } from "../reflection";
 
// The provided configuration type when creating a new Schema.
export type SchemaConfig = {
  query: any;
  mutation?: any;
  subscription?: any;
  directives?: GraphQLDirective[];
  types?: any[];
};
 
type GraphQLSchemaConfig = {
  query: GraphQLObjectType;
  mutation?: GraphQLObjectType;
  subscription?: GraphQLObjectType;
  directives?: GraphQLDirective[];
  types?: GraphQLNamedType[];
};
 
export class Schema extends GraphQLSchema {
  constructor(config: SchemaConfig) {
    let schemaConfig: GraphQLSchemaConfig = {
      query: <GraphQLObjectType>getGraphQLType(config.query),
      directives: config.directives
    };
    if (config.mutation) {
      schemaConfig.mutation = <GraphQLObjectType>getGraphQLType(
        config.mutation
      );
    }
    if (config.subscription) {
      schemaConfig.subscription = <GraphQLObjectType>getGraphQLType(
        config.subscription
      );
    }
    if (config.types) {
      schemaConfig.types = config.types.map(
        type => <GraphQLObjectType>getGraphQLType(type)
      );
    }
    super(schemaConfig);
  }
  public execute(query: string, ...args: any[]): Promise<ExecutionResult> {
    return graphql(this, query, ...args);
  }
  public toString() {
    return printSchema(this);
  }
}