Skip to content

Commit

Permalink
integrate new internal tools
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Apr 25, 2024
1 parent e55fe84 commit 6812d9b
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 79 deletions.
2 changes: 1 addition & 1 deletion packages/graphqlsp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@gql.tada/internal": "^0.1.2",
"@gql.tada/internal": "0.3.0-canary-3c09253dc57ece41320907f966ceceb5265b2b49",
"graphql": "^16.8.1",
"node-fetch": "^2.0.0"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/graphqlsp/src/ast/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ export const getSchemaName = (
node: ts.CallExpression,
typeChecker?: ts.TypeChecker
): string => {
if (!typeChecker) return 'defeault';
if (!typeChecker) return 'default';

const expression = ts.isPropertyAccessExpression(node.expression)
? node.expression.expression
: node.expression;
const type = typeChecker.getTypeAtLocation(expression);
if (type) {
const brandTypeSymbol = type.getProperty('__brand');
const brandTypeSymbol = type.getProperty('__name');
if (brandTypeSymbol) {
const brand = typeChecker.getTypeOfSymbol(brandTypeSymbol);
if (brand.isStringLiteral()) {
Expand Down
15 changes: 6 additions & 9 deletions packages/graphqlsp/src/autoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ export function getGraphQLCompletions(
const schemaName = getSchemaName(node, typeChecker);

const foundToken = getToken(node.arguments[0], cursorPosition);
if (!schema.current || !foundToken) return undefined;
if ((!schema.current && !schema.multi[schemaName]) || !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 =
'schemas' in schema.current
? schema.current.schemas[schemaName]
: schema.current;
schemaToUse = schema.multi[schemaName]
? schema.multi[schemaName]!.schema
: schema.current!.schema;
} else if (ts.isTaggedTemplateExpression(node)) {
const { template, tag } = node;

Expand All @@ -97,10 +97,7 @@ export function getGraphQLCompletions(

text = combinedText;
cursor = new Cursor(foundToken.line, foundToken.start - 1);
schemaToUse =
'schemas' in schema.current
? schema.current.schemas['default']
: schema.current;
schemaToUse = schema.current.schema;
} else {
return undefined;
}
Expand Down
7 changes: 3 additions & 4 deletions packages/graphqlsp/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,9 @@ const runDiagnostics = (
} catch (e) {}
}

const schemaToUse =
schema.current && 'schemas' in schema.current
? schema.current.schemas[originalNode.schema]
: schema.current;
const schemaToUse = schema.multi[originalNode.schema]
? schema.multi[originalNode.schema]!.schema
: schema.current!.schema;
const graphQLDiagnostics = getDiagnostics(
text,
schemaToUse,
Expand Down
68 changes: 27 additions & 41 deletions packages/graphqlsp/src/graphql/getSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import type { Stats, PathLike } from 'node:fs';
import fs from 'node:fs/promises';
import path from 'path';

import type { GraphQLSchema, IntrospectionQuery } from 'graphql';
import type { IntrospectionQuery } from 'graphql';

import {
type SchemaOrigin,
type SchemaLoaderResult,
load,
resolveTypeScriptRootDir,
type SchemaRef as _SchemaRef,
type GraphQLSPConfig,
loadRef,
minifyIntrospection,
outputIntrospectionFile,
resolveTypeScriptRootDir,
} from '@gql.tada/internal';

import { ts } from '../ts';
Expand Down Expand Up @@ -93,22 +94,15 @@ async function saveTadaIntrospection(
}
}

export interface SchemaRef {
current:
| GraphQLSchema
| { schemas: { [name: string]: GraphQLSchema } }
| null;
version: number;
}
export type SchemaRef = _SchemaRef<SchemaLoaderResult | null>;

export const loadSchema = (
// TODO: abstract info away
info: ts.server.PluginCreateInfo,
origin: SchemaOrigin,
origin: GraphQLSPConfig,
logger: Logger
): SchemaRef => {
let loaderResult: SchemaLoaderResult | null = null;
const ref: SchemaRef = { current: null, version: 0 };
): _SchemaRef<SchemaLoaderResult | null> => {
const ref = loadRef(origin);

(async () => {
const rootPath =
Expand All @@ -123,43 +117,35 @@ export const loadSchema = (
logger('Got root-directory to resolve schema from: ' + rootPath);
logger('Resolving schema from "schema" config: ' + JSON.stringify(origin));

const loader = load({ origin, rootPath });

try {
logger(`Loading schema from "${origin}"`);
loaderResult = await loader.load();
logger(`Loading schema...`);
await ref.load();
} catch (error) {
logger(`Failed to load schema: ${error}`);
}

// Will this just automagically give us more schemas or should we
// leverage a "forEach"
if (loaderResult) {
ref.current = loaderResult && loaderResult.schema;
ref.version++;
if (tadaOutputLocation) {
if (ref.current) {
saveTadaIntrospection(
ref.current.introspection,
tadaOutputLocation,
tadaDisablePreprocessing,
logger
);
} else if (ref.multi) {
Object.values(ref.multi).forEach(value => {
if (!value) return;

saveTadaIntrospection(
loaderResult.introspection,
tadaOutputLocation,
value.introspection,
// TODO: do we want to expose the tadaOutputLocation here
path.resolve(rootPath, value.tadaOutputLocation),
tadaDisablePreprocessing,
logger
);
}
});
}

loader.notifyOnUpdate(result => {
logger(`Got schema for origin "${origin}"`);
ref.current = (loaderResult = result).schema;
ref.version++;
if (tadaOutputLocation) {
saveTadaIntrospection(
loaderResult.introspection,
tadaOutputLocation,
tadaDisablePreprocessing,
logger
);
}
});
ref.autoupdate();
})();

return ref;
Expand Down
15 changes: 6 additions & 9 deletions packages/graphqlsp/src/quickInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ export function getGraphQLQuickInfo(
const schemaName = getSchemaName(node, typeChecker);

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

text = node.arguments[0].getText();
cursor = new Cursor(foundToken.line, foundToken.start - 1);
schemaToUse =
'schemas' in schema.current
? schema.current.schemas[schemaName]
: schema.current;
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 All @@ -78,10 +78,7 @@ export function getGraphQLQuickInfo(
foundToken.line = foundToken.line + amountOfLines;
text = combinedText;
cursor = new Cursor(foundToken.line, foundToken.start - 1);
schemaToUse =
'schemas' in schema.current
? schema.current.schemas['default']
: schema.current;
schemaToUse = schema.current.schema;
} else {
return undefined;
}
Expand Down
35 changes: 22 additions & 13 deletions pnpm-lock.yaml

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

0 comments on commit 6812d9b

Please sign in to comment.