Skip to content

Commit

Permalink
feat: import typescript types from external file (#39)
Browse files Browse the repository at this point in the history
* try to implement importing from a global file

* make typescript globals work

* Create twelve-spies-learn.md
  • Loading branch information
JoviDeCroock authored Apr 18, 2023
1 parent 187d603 commit d81a4b3
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 229 deletions.
6 changes: 6 additions & 0 deletions .changeset/twelve-spies-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@0no-co/graphqlsp": minor
---

only run the `typescript` plugin once to generate a set of types that we'll reference from our
`typescript-operations`, this to reduce lengthy generated files.
105 changes: 105 additions & 0 deletions packages/example/__generated__/baseGraphQLSP.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 1 addition & 106 deletions packages/example/src/Pokemon.generated.ts
Original file line number Diff line number Diff line change
@@ -1,110 +1,5 @@
import * as Types from '../__generated__/baseGraphQLSP';
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = {
[K in keyof T]: T[K];
};
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & {
[SubKey in K]?: Maybe<T[SubKey]>;
};
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & {
[SubKey in K]: Maybe<T[SubKey]>;
};
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
};

/** Elemental property associated with either a Pokémon or one of their moves. */
export type PokemonType =
| 'Grass'
| 'Poison'
| 'Fire'
| 'Flying'
| 'Water'
| 'Bug'
| 'Normal'
| 'Electric'
| 'Ground'
| 'Fairy'
| 'Fighting'
| 'Psychic'
| 'Rock'
| 'Steel'
| 'Ice'
| 'Ghost'
| 'Dragon'
| 'Dark';

/** Move a Pokémon can perform with the associated damage and type. */
export type Attack = {
__typename?: 'Attack';
name?: Maybe<Scalars['String']>;
type?: Maybe<PokemonType>;
damage?: Maybe<Scalars['Int']>;
};

/** Requirement that prevents an evolution through regular means of levelling up. */
export type EvolutionRequirement = {
__typename?: 'EvolutionRequirement';
amount?: Maybe<Scalars['Int']>;
name?: Maybe<Scalars['String']>;
};

export type PokemonDimension = {
__typename?: 'PokemonDimension';
minimum?: Maybe<Scalars['String']>;
maximum?: Maybe<Scalars['String']>;
};

export type AttacksConnection = {
__typename?: 'AttacksConnection';
fast?: Maybe<Array<Maybe<Attack>>>;
special?: Maybe<Array<Maybe<Attack>>>;
};

export type Pokemon = {
__typename?: 'Pokemon';
id: Scalars['ID'];
name: Scalars['String'];
classification?: Maybe<Scalars['String']>;
types?: Maybe<Array<Maybe<PokemonType>>>;
resistant?: Maybe<Array<Maybe<PokemonType>>>;
weaknesses?: Maybe<Array<Maybe<PokemonType>>>;
evolutionRequirements?: Maybe<Array<Maybe<EvolutionRequirement>>>;
weight?: Maybe<PokemonDimension>;
height?: Maybe<PokemonDimension>;
attacks?: Maybe<AttacksConnection>;
/** Likelihood of an attempt to catch a Pokémon to fail. */
fleeRate?: Maybe<Scalars['Float']>;
/** Maximum combat power a Pokémon may achieve at max level. */
maxCP?: Maybe<Scalars['Int']>;
/** Maximum health points a Pokémon may achieve at max level. */
maxHP?: Maybe<Scalars['Int']>;
evolutions?: Maybe<Array<Maybe<Pokemon>>>;
};

export type Query = {
__typename?: 'Query';
/** List out all Pokémon, optionally in pages */
pokemons?: Maybe<Array<Maybe<Pokemon>>>;
/** Get a single Pokémon by its ID, a three character long identifier padded with zeroes */
pokemon?: Maybe<Pokemon>;
};

export type QueryPokemonsArgs = {
limit?: InputMaybe<Scalars['Int']>;
skip?: InputMaybe<Scalars['Int']>;
};

export type QueryPokemonArgs = {
id: Scalars['ID'];
};

export type FieldsFragment = {
__typename?: 'Pokemon';
classification?: string | null;
Expand Down
113 changes: 4 additions & 109 deletions packages/example/src/index.generated.ts
Original file line number Diff line number Diff line change
@@ -1,111 +1,6 @@
import * as Types from '../__generated__/baseGraphQLSP';
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
export type Maybe<T> = T | null;
export type InputMaybe<T> = Maybe<T>;
export type Exact<T extends { [key: string]: unknown }> = {
[K in keyof T]: T[K];
};
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & {
[SubKey in K]?: Maybe<T[SubKey]>;
};
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & {
[SubKey in K]: Maybe<T[SubKey]>;
};
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
ID: string;
String: string;
Boolean: boolean;
Int: number;
Float: number;
};

/** Elemental property associated with either a Pokémon or one of their moves. */
export type PokemonType =
| 'Grass'
| 'Poison'
| 'Fire'
| 'Flying'
| 'Water'
| 'Bug'
| 'Normal'
| 'Electric'
| 'Ground'
| 'Fairy'
| 'Fighting'
| 'Psychic'
| 'Rock'
| 'Steel'
| 'Ice'
| 'Ghost'
| 'Dragon'
| 'Dark';

/** Move a Pokémon can perform with the associated damage and type. */
export type Attack = {
__typename?: 'Attack';
name?: Maybe<Scalars['String']>;
type?: Maybe<PokemonType>;
damage?: Maybe<Scalars['Int']>;
};

/** Requirement that prevents an evolution through regular means of levelling up. */
export type EvolutionRequirement = {
__typename?: 'EvolutionRequirement';
amount?: Maybe<Scalars['Int']>;
name?: Maybe<Scalars['String']>;
};

export type PokemonDimension = {
__typename?: 'PokemonDimension';
minimum?: Maybe<Scalars['String']>;
maximum?: Maybe<Scalars['String']>;
};

export type AttacksConnection = {
__typename?: 'AttacksConnection';
fast?: Maybe<Array<Maybe<Attack>>>;
special?: Maybe<Array<Maybe<Attack>>>;
};

export type Pokemon = {
__typename?: 'Pokemon';
id: Scalars['ID'];
name: Scalars['String'];
classification?: Maybe<Scalars['String']>;
types?: Maybe<Array<Maybe<PokemonType>>>;
resistant?: Maybe<Array<Maybe<PokemonType>>>;
weaknesses?: Maybe<Array<Maybe<PokemonType>>>;
evolutionRequirements?: Maybe<Array<Maybe<EvolutionRequirement>>>;
weight?: Maybe<PokemonDimension>;
height?: Maybe<PokemonDimension>;
attacks?: Maybe<AttacksConnection>;
/** Likelihood of an attempt to catch a Pokémon to fail. */
fleeRate?: Maybe<Scalars['Float']>;
/** Maximum combat power a Pokémon may achieve at max level. */
maxCP?: Maybe<Scalars['Int']>;
/** Maximum health points a Pokémon may achieve at max level. */
maxHP?: Maybe<Scalars['Int']>;
evolutions?: Maybe<Array<Maybe<Pokemon>>>;
};

export type Query = {
__typename?: 'Query';
/** List out all Pokémon, optionally in pages */
pokemons?: Maybe<Array<Maybe<Pokemon>>>;
/** Get a single Pokémon by its ID, a three character long identifier padded with zeroes */
pokemon?: Maybe<Pokemon>;
};

export type QueryPokemonsArgs = {
limit?: InputMaybe<Scalars['Int']>;
skip?: InputMaybe<Scalars['Int']>;
};

export type QueryPokemonArgs = {
id: Scalars['ID'];
};

export type PokemonsQueryVariables = Exact<{ [key: string]: never }>;
export type PokemonsQueryVariables = Types.Exact<{ [key: string]: never }>;

export type PokemonsQuery = {
__typename?: 'Query';
Expand All @@ -123,8 +18,8 @@ export type PokemonFieldsFragment = {
name: string;
};

export type PokemonQueryVariables = Exact<{
id: Scalars['ID'];
export type PokemonQueryVariables = Types.Exact<{
id: Types.Scalars['ID'];
}>;

export type PokemonQuery = {
Expand Down
1 change: 1 addition & 0 deletions packages/graphqlsp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"@graphql-codegen/add": "^4.0.1",
"@graphql-codegen/core": "^3.1.0",
"@graphql-codegen/typed-document-node": "^3.0.2",
"@graphql-codegen/typescript": "^3.0.3",
Expand Down
8 changes: 7 additions & 1 deletion packages/graphqlsp/src/getSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import path from 'path';
import fs from 'fs';

import { Logger } from './index';
import { generateBaseTypes } from './types/generate';

export const loadSchema = (
root: string,
schema: string,
logger: Logger
logger: Logger,
baseTypesPath: string,
scalars: Record<string, unknown>
): { current: GraphQLSchema | null } => {
const ref: { current: GraphQLSchema | null } = { current: null };
let url: URL | undefined;
Expand Down Expand Up @@ -54,6 +57,7 @@ export const loadSchema = (
(result as { data: IntrospectionQuery }).data
);
logger(`Got schema for ${url!.toString()}`);
generateBaseTypes(ref.current, baseTypesPath, scalars);
} catch (e: any) {
logger(`Got schema error for ${e.message}`);
}
Expand All @@ -70,11 +74,13 @@ export const loadSchema = (
ref.current = isJson
? buildClientSchema(JSON.parse(contents))
: buildSchema(contents);
generateBaseTypes(ref.current, baseTypesPath, scalars);
});

ref.current = isJson
? buildClientSchema(JSON.parse(contents))
: buildSchema(contents);
generateBaseTypes(ref.current, baseTypesPath, scalars);
logger(`Got schema and initialized watcher for ${schema}`);
}

Expand Down
Loading

0 comments on commit d81a4b3

Please sign in to comment.