Mirror: TypeScript LSP plugin that finds GraphQL documents in your code and provides diagnostics, auto-complete and hover-information.

fix crash

+8
example/src/Pokemon.ts
···
···
+
import { gql } from '@urql/core';
+
+
export const PokemonFields = gql`
+
fragment pokemonFields on Pokemon {
+
id
+
name
+
}
+
` as typeof import('./Pokemon.generated').PokemonFieldsFragmentDoc
+19 -20
example/src/fragment.generated.ts example/src/Pokemon.generated.ts
···
};
/** Elemental property associated with either a Pokémon or one of their moves. */
-
export enum PokemonType {
-
Bug = 'Bug',
-
Dark = 'Dark',
-
Dragon = 'Dragon',
-
Electric = 'Electric',
-
Fairy = 'Fairy',
-
Fighting = 'Fighting',
-
Fire = 'Fire',
-
Flying = 'Flying',
-
Ghost = 'Ghost',
-
Grass = 'Grass',
-
Ground = 'Ground',
-
Ice = 'Ice',
-
Normal = 'Normal',
-
Poison = 'Poison',
-
Psychic = 'Psychic',
-
Rock = 'Rock',
-
Steel = 'Steel',
-
Water = 'Water'
-
}
export type Query = {
__typename?: 'Query';
···
};
/** Elemental property associated with either a Pokémon or one of their moves. */
+
export type PokemonType =
+
| 'Bug'
+
| 'Dark'
+
| 'Dragon'
+
| 'Electric'
+
| 'Fairy'
+
| 'Fighting'
+
| 'Fire'
+
| 'Flying'
+
| 'Ghost'
+
| 'Grass'
+
| 'Ground'
+
| 'Ice'
+
| 'Normal'
+
| 'Poison'
+
| 'Psychic'
+
| 'Rock'
+
| 'Steel'
+
| 'Water';
export type Query = {
__typename?: 'Query';
-20
example/src/fragment.ts
···
-
import { gql } from '@urql/core'
-
-
export const PokemonFields = gql`
-
fragment pokemonFields on Pokemon {
-
id
-
name
-
}
-
` as typeof import('./fragment.generated').PokemonFieldsFragmentDoc
-
// TODO: how to type
-
// export const PokemonFields = gql`
-
// fragment pokemonFields on Pokemon {
-
// id
-
// name
-
// }
-
-
// fragment morePokemonFields on Pokemon {
-
// id
-
// name
-
// }
-
// ` as typeof import('./fragment.generated').PokemonsDocument
···
+5 -11
example/src/index.generated.ts
···
export type PokemonsQueryVariables = Exact<{ [key: string]: never; }>;
-
export type PokemonsQuery = { __typename?: 'Query', pokemons?: Array<{ __typename?: 'Pokemon', id: string, name: string } | null> | null };
export type PokemonFieldsFragment = { __typename?: 'Pokemon', id: string, name: string };
-
export type PokemonQueryVariables = Exact<{ [key: string]: never; }>;
-
-
-
export type PokemonQuery = { __typename?: 'Query', pokemon?: { __typename?: 'Pokemon', id: string, name: string } | null };
-
-
export type DonkemonQueryVariables = Exact<{
id: Scalars['ID'];
}>;
-
export type DonkemonQuery = { __typename?: 'Query', pokemon?: { __typename?: 'Pokemon', id: string } | null };
export const PokemonFieldsFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"pokemonFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Pokemon"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}}]} as unknown as DocumentNode<PokemonFieldsFragment, unknown>;
-
export const PokemonsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Pokemons"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"pokemons"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}}]}}]} as unknown as DocumentNode<PokemonsQuery, PokemonsQueryVariables>;
-
export const PokemonDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Pokemon"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"pokemon"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"StringValue","value":"1","block":false}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"FragmentSpread","name":{"kind":"Name","value":"pokemonFields"}}]}}]}},...PokemonFieldsFragmentDoc.definitions]} as unknown as DocumentNode<PokemonQuery, PokemonQueryVariables>;
-
export const DonkemonDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Donkemon"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"id"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"pokemon"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"Variable","name":{"kind":"Name","value":"id"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}}]}}]}}]} as unknown as DocumentNode<DonkemonQuery, DonkemonQueryVariables>;
···
export type PokemonsQueryVariables = Exact<{ [key: string]: never; }>;
+
export type PokemonsQuery = { __typename?: 'Query', pokemons?: Array<{ __typename: 'Pokemon', id: string, name: string } | null> | null };
export type PokemonFieldsFragment = { __typename?: 'Pokemon', id: string, name: string };
+
export type PokemonQueryVariables = Exact<{
id: Scalars['ID'];
}>;
+
export type PokemonQuery = { __typename?: 'Query', pokemon?: { __typename: 'Pokemon', id: string, name: string } | null };
export const PokemonFieldsFragmentDoc = {"kind":"Document","definitions":[{"kind":"FragmentDefinition","name":{"kind":"Name","value":"pokemonFields"},"typeCondition":{"kind":"NamedType","name":{"kind":"Name","value":"Pokemon"}},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}}]}}]} as unknown as DocumentNode<PokemonFieldsFragment, unknown>;
+
export const PokemonsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Pokemons"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"pokemons"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"FragmentSpread","name":{"kind":"Name","value":"pokemonFields"}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}}]}},...PokemonFieldsFragmentDoc.definitions]} as unknown as DocumentNode<PokemonsQuery, PokemonsQueryVariables>;
+
export const PokemonDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"Pokemon"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"id"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"ID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"pokemon"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"id"},"value":{"kind":"Variable","name":{"kind":"Name","value":"id"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"__typename"}}]}}]}}]} as unknown as DocumentNode<PokemonQuery, PokemonQueryVariables>;
+20 -26
example/src/index.ts
···
-
import { createClient, gql } from '@urql/core'
-
import { PokemonFields } from './fragment'
-
-
// testing stuffzzzzzzzz
-
const Pokemons = gql`
query Pokemons {
pokemons {
id
-
name
}
}
${PokemonFields}
` as typeof import('./index.generated').PokemonsDocument
-
const Pokemon = gql`
-
query Pokemon {
-
pokemon(id: "1") {
-
id
-
name
-
...pokemonFields
-
}
-
}
-
${PokemonFields}
-
` as typeof import('./index.generated').PokemonDocument
-
const Donkemon = gql`
-
query Donkemon ($id: ID!) {
pokemon(id: $id) {
id
}
}
-
` as typeof import('./index.generated').DonkemonDocument
-
const urqlClient = createClient({
-
url: '',
-
exchanges: []
-
});
-
urqlClient.query(Pokemons).toPromise().then(result => {
-
result.data?.pokemons;
-
});
···
+
import { gql, createClient } from '@urql/core';
+
import { PokemonFields } from './Pokemon';
+
const PokemonsQuery = gql`
query Pokemons {
pokemons {
id
+
name
+
...pokemonFields
+
__typename
}
}
${PokemonFields}
` as typeof import('./index.generated').PokemonsDocument
+
const client = createClient({
+
url: '',
+
})
+
client.query(PokemonsQuery).toPromise().then(result => {
+
result.data?.pokemons;
+
})
+
const PokemonQuery = gql`
+
query Pokemon($id: ID!) {
pokemon(id: $id) {
id
+
name
+
__typename
}
}
+
` as typeof import('./index.generated').PokemonDocument
+
client.query(PokemonQuery, { id: '' }).toPromise().then(result => {
+
result.data?.pokemon;
+
})
+5 -2
src/index.ts
···
const suggestions = getAutocompleteSuggestions(schema.current, text, new Cursor(foundToken.line, foundToken.start))
-
const parsed = parse(text);
-
const fragments = parsed.definitions.filter(x => x.kind === Kind.FRAGMENT_DEFINITION) as Array<FragmentDefinitionNode>
const result: ts.WithMetadata<ts.CompletionInfo> = {
isGlobalCompletion: false,
···
const suggestions = getAutocompleteSuggestions(schema.current, text, new Cursor(foundToken.line, foundToken.start))
+
let fragments: Array<FragmentDefinitionNode> = []
+
try {
+
const parsed = parse(text);
+
fragments = parsed.definitions.filter(x => x.kind === Kind.FRAGMENT_DEFINITION) as Array<FragmentDefinitionNode>
+
} catch (e) {}
const result: ts.WithMetadata<ts.CompletionInfo> = {
isGlobalCompletion: false,