UNPKG

@gql2ts/from-query

Version:

generate typescript interfaces from a graphql schema and query

164 lines (163 loc) 4.33 kB
import { GraphQLSchema, DocumentNode, FieldNode, InlineFragmentNode, VariableNode, TypeNode, OperationTypeNode, GraphQLNamedType, GraphQLList, GraphQLInterfaceType, GraphQLType, GraphQLEnumType } from 'graphql'; /** * An internal representation for a Directive, has the name and * a map of the arguments provided */ export interface IDirective { kind: 'Directive'; name: string; arguments: { [name: string]: string; }; } /** * A map of directive name to Directive IR */ export interface IDirectiveMap { [directive: string]: IDirective; } /** * A type definition for a Scalar */ export interface ITypeDefinition { kind: 'TypeDefinition'; type: string; nullable: boolean; originalNode: TypeNode; isScalar: boolean; } /** * A type definition for a List */ export interface IListTypeDefinition { kind: 'ListTypeDefinition'; of: TypeDefinition; nullable: boolean; originalNode: GraphQLList<any>; } /** * A definition for a __typename declaration */ export interface ITypenameDefinition { kind: 'TypenameDefinition'; nullable: false; /** * This is the value of __typename */ type: string | string[]; } export interface IInterfaceTypeDefinition { kind: 'InterfaceTypeDefinition'; nullable: boolean; originalNode: GraphQLInterfaceType; } export interface IEnumTypeDefinition { kind: 'EnumTypeDefinition'; nullable: boolean; originalNode: GraphQLEnumType; type: string; values: string[]; } /** * The possible type definitions */ export declare type TypeDefinition = ITypeDefinition | IInterfaceTypeDefinition | IListTypeDefinition | ITypenameDefinition | IEnumTypeDefinition; /** * An internal representation for a FieldNode. This represents an * object, its selections and its type definition */ export interface IFieldNode { kind: 'Field'; name: string; typeDefinition: TypeDefinition; directives: IDirectiveMap; originalNode: FieldNode; selections: Selection[]; } /** * An internal representation for a LeafNode. This represents a Scalar and its * type definition */ export interface ILeafNode { kind: 'LeafNode'; name: string; typeDefinition: TypeDefinition; directives: IDirectiveMap; originalNode: FieldNode; } /** * An internal representation for a `__typename` selection */ export interface ITypenameNode { kind: 'TypenameNode'; typeDefinition: ITypenameDefinition; name: string; } /** * A possible field definition */ export declare type FieldDefinition = IFieldNode | ILeafNode | ITypenameNode; /** * An internal representation for a Fragment definition. This is used * by an {@link IInterfaceNode} object */ export interface IFragment { kind: 'Fragment'; typeDefinition: TypeDefinition; directives: IDirectiveMap; originalNode: InlineFragmentNode; selections: Selection[]; } /** * This represents a selection on an interface, it contains a list of * {@link IFragment} objects. * * EXPERIMENTAL * @TODO look at unions */ export interface IInterfaceNode { kind: 'InterfaceNode'; name: string; fragments: IFragment[]; directives: IDirectiveMap; typeDefinition: TypeDefinition; } /** * A Selection on an object */ export declare type Selection = FieldDefinition | IInterfaceNode; /** * An internal representation of a Variable */ export interface IVariable { kind: 'Variable'; name: string; type: TypeDefinition; originalNode: VariableNode; } /** * An internal representation of an operation */ export interface IOperation { kind: 'Root'; operationType: OperationTypeNode; name?: string; variables: IVariable[]; directives: IDirectiveMap; selections: Selection[]; } /** * Given a potentially wrapping type, return the unwrapped type * * @param type A GraphQLType * @returns A GraphQLNamedType (i.e. a type without List or NonNull) */ export declare const unwrapType: (type: GraphQLType) => GraphQLNamedType; /** * Given a schema and a query, return an internal representation of the query * @param schema A GraphQL Schema * @param query A GraphQL Query * @returns An internal representation of the query */ declare const convertToIr: (schema: GraphQLSchema, query: DocumentNode) => IOperation; export default convertToIr;