Skip to content

Commit

Permalink
fix base caes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Apr 25, 2024
1 parent 0377fc3 commit cafbe5c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 33 deletions.
1 change: 0 additions & 1 deletion packages/example-tada/introspection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* instead save to a .ts instead of a .d.ts file.
*/
export type introspection = {
name: 'pokemons';
query: 'Query';
mutation: never;
subscription: never;
Expand Down
9 changes: 2 additions & 7 deletions packages/example-tada/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
"plugins": [
{
"name": "@0no-co/graphqlsp",
"schemas": [
{
"name": "pokemons",
"schema": "./schema.graphql",
"tadaOutputLocation": "./introspection.d.ts"
}
]
"schema": "./schema.graphql",
"tadaOutputLocation": "./introspection.d.ts"
}
],
"jsx": "react-jsx",
Expand Down
19 changes: 11 additions & 8 deletions packages/graphqlsp/src/ast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ export function unrollTadaFragments(
export const getSchemaName = (
node: ts.CallExpression,
typeChecker?: ts.TypeChecker
): string => {
if (!typeChecker) return 'default';
): string | null => {
if (!typeChecker) return null;

const expression = ts.isPropertyAccessExpression(node.expression)
? node.expression.expression
Expand All @@ -140,28 +140,31 @@ export const getSchemaName = (
const brand = typeChecker.getTypeOfSymbol(brandTypeSymbol);
if (brand.isUnionOrIntersection()) {
const found = brand.types.find(x => x.isStringLiteral());
return found && found.isStringLiteral() ? found.value : 'default';
return found && found.isStringLiteral() ? found.value : null;
} else if (brand.isStringLiteral()) {
return brand.value;
}
}
}

return 'default';
return null;
};

export function findAllCallExpressions(
sourceFile: ts.SourceFile,
info: ts.server.PluginCreateInfo,
shouldSearchFragments: boolean = true
): {
nodes: Array<{ node: ts.NoSubstitutionTemplateLiteral; schema: string }>;
nodes: Array<{
node: ts.NoSubstitutionTemplateLiteral;
schema: string | null;
}>;
fragments: Array<FragmentDefinitionNode>;
} {
const typeChecker = info.languageService.getProgram()?.getTypeChecker();
const result: Array<{
node: ts.NoSubstitutionTemplateLiteral;
schema: string;
schema: string | null;
}> = [];
let fragments: Array<FragmentDefinitionNode> = [];
let hasTriedToFindFragments = shouldSearchFragments ? false : true;
Expand Down Expand Up @@ -206,8 +209,8 @@ export function findAllCallExpressions(
export function findAllPersistedCallExpressions(
sourceFile: ts.SourceFile,
info: ts.server.PluginCreateInfo
): Array<{ node: ts.CallExpression; schema: string }> {
const result: Array<{ node: ts.CallExpression; schema: string }> = [];
): Array<{ node: ts.CallExpression; schema: string | null }> {
const result: Array<{ node: ts.CallExpression; schema: string | null }> = [];
const typeChecker = info.languageService.getProgram()?.getTypeChecker();
function find(node: ts.Node) {
if (ts.isCallExpression(node)) {
Expand Down
13 changes: 7 additions & 6 deletions packages/graphqlsp/src/autoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function getGraphQLCompletions(
? bubbleUpCallExpression(node)
: bubbleUpTemplate(node);

let text, cursor, schemaToUse: GraphQLSchema;
let text, cursor, schemaToUse: GraphQLSchema | undefined;
if (
ts.isCallExpression(node) &&
isCallExpression &&
Expand All @@ -59,18 +59,19 @@ export function getGraphQLCompletions(
const typeChecker = info.languageService.getProgram()?.getTypeChecker();
const schemaName = getSchemaName(node, typeChecker);

schemaToUse =
schemaName && schema.multi[schemaName]
? schema.multi[schemaName]?.schema
: schema.current?.schema;

const foundToken = getToken(node.arguments[0], cursorPosition);
if ((!schema.current && !schema.multi[schemaName]) || !foundToken)
return undefined;
if (!schemaToUse || !foundToken) return undefined;

const queryText = node.arguments[0].getText().slice(1, -1);
const fragments = getAllFragments(filename, node, info);

text = `${queryText}\n${fragments.map(x => print(x)).join('\n')}`;
cursor = new Cursor(foundToken.line, foundToken.start - 1);
schemaToUse = schema.multi[schemaName]
? schema.multi[schemaName]!.schema
: schema.current!.schema;
} else if (ts.isTaggedTemplateExpression(node)) {
const { template, tag } = node;

Expand Down
16 changes: 11 additions & 5 deletions packages/graphqlsp/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function getGraphQLDiagnostics(
let fragments: Array<FragmentDefinitionNode> = [],
nodes: {
node: ts.NoSubstitutionTemplateLiteral | ts.TaggedTemplateExpression;
schema: string;
schema: string | null;
}[];
if (isCallExpression) {
const result = findAllCallExpressions(source, info);
Expand Down Expand Up @@ -339,7 +339,7 @@ const runDiagnostics = (
}: {
nodes: {
node: ts.TaggedTemplateExpression | ts.NoSubstitutionTemplateLiteral;
schema: string;
schema: string | null;
}[];
fragments: FragmentDefinitionNode[];
},
Expand Down Expand Up @@ -406,9 +406,15 @@ const runDiagnostics = (
} catch (e) {}
}

const schemaToUse = schema.multi[originalNode.schema]
? schema.multi[originalNode.schema]!.schema
: schema.current!.schema;
const schemaToUse =
originalNode.schema && schema.multi[originalNode.schema]
? schema.multi[originalNode.schema]?.schema
: schema.current?.schema;

if (!schemaToUse) {
return undefined;
}

const graphQLDiagnostics = getDiagnostics(
text,
schemaToUse,
Expand Down
13 changes: 7 additions & 6 deletions packages/graphqlsp/src/quickInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function getGraphQLQuickInfo(
? bubbleUpCallExpression(node)
: bubbleUpTemplate(node);

let cursor, text, schemaToUse: GraphQLSchema;
let cursor, text, schemaToUse: GraphQLSchema | undefined;
if (
ts.isCallExpression(node) &&
isCallExpression &&
Expand All @@ -44,15 +44,16 @@ export function getGraphQLQuickInfo(
const typeChecker = info.languageService.getProgram()?.getTypeChecker();
const schemaName = getSchemaName(node, typeChecker);

schemaToUse =
schemaName && schema.multi[schemaName]
? schema.multi[schemaName]?.schema
: schema.current?.schema;

const foundToken = getToken(node.arguments[0], cursorPosition);
if ((!schema.current && !schema.multi[schemaName]) || !foundToken)
return undefined;
if (!schemaToUse || !foundToken) return undefined;

text = node.arguments[0].getText();
cursor = new Cursor(foundToken.line, foundToken.start - 1);
schemaToUse = schema.multi[schemaName]
? schema.multi[schemaName]!.schema
: schema.current!.schema;
} else if (ts.isTaggedTemplateExpression(node)) {
const { template, tag } = node;
if (!ts.isIdentifier(tag) || !templates.has(tag.text)) return undefined;
Expand Down

0 comments on commit cafbe5c

Please sign in to comment.