Skip to content

Commit d989456

Browse files
authored
fix: pre-cacheing schema bugs, new server config options (#1636)
- cache paths were not matching up b/c of last minute change - improve documentation, improve underlying interfaces - add all loadConfig options as startServer param, deprecate configDir - cleanup
1 parent 27cd185 commit d989456

File tree

11 files changed

+326
-215
lines changed

11 files changed

+326
-215
lines changed

packages/graphql-language-service-cli/README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@
22

33
> Note: As of 3.0.0, this package has been renamed from `graphql-language-service` to `graphql-language-service-cli`. please now use the `graphql-lsp` bin, instead of the `graphql` binary.
44
5-
[![NPM](https://img.shields.io/npm/v/graphql-language-service.svg)](https://npmjs.com/graphql-language-service)
6-
![npm downloads](https://img.shields.io/npm/dm/graphql-language-service?label=npm%20downloads)
5+
[![NPM](https://img.shields.io/npm/v/graphql-language-service-cli.svg)](https://npmjs.com/graphql-language-service-cli)
6+
![npm downloads](https://img.shields.io/npm/dm/graphql-language-service-vli?label=npm%20downloads)
77
![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/codemirror-graphql)
88
[![License](https://img.shields.io/npm/l/graphql-language-service.svg?style=flat-square)](LICENSE)
99

1010
_We welcome your feedback and suggestions._
1111

1212
GraphQL Language Service provides an interface for building GraphQL language services for IDEs.
1313

14-
Partial support for [Microsoft's Language Server Protocol](https://github.com/Microsoft/language-server-protocol) is in place, with more to come in the future.
14+
Almost 100% for [Microsoft's Language Server Protocol](https://github.com/Microsoft/language-server-protocol) is in place
1515

1616
Supported features include:
1717

1818
- Diagnostics (GraphQL syntax linting/validations) (**spec-compliant**)
1919
- Autocomplete suggestions (**spec-compliant**)
2020
- Hyperlink to fragment definitions and named types (type, input, enum) definitions (**spec-compliant**)
21-
- Outline view support for queries
21+
- Outline view support for queries and SDL
22+
- Symbols support across the workspace
23+
24+
see more information at [`graphql-language-service-server`](https://npmjs.com/graphql-language-service-server)
2225

2326
## Installation and Usage
2427

@@ -28,6 +31,8 @@ An LSP-compatible client with a file watcher that sends watch notifications to t
2831

2932
**DROPPED**: GraphQL Language Service no longer depends on [Watchman](https://facebook.github.io/watchman/)
3033

34+
Only node 9 or greater, and npm or yarn are required dependencies.
35+
3136
### Installation
3237

3338
with `yarn`:
@@ -42,20 +47,22 @@ with `npm`:
4247
npm i -g graphql-language-service-cli
4348
```
4449

50+
either will install the `graphql-lsp` bin globally
51+
4552
### GraphQL configuration file (`.graphqlrc.yml`)
4653

4754
Check out [graphql-config](https://graphql-config.com/docs/introduction)
4855

49-
The graphql features we support are:
56+
The custom graphql language configurations are:
5057

5158
- `customDirectives` - `['@myExampleDirective']`
52-
- `customValidationRules` - returns rules array with parameter `ValidationContext` from `graphql/validation`;
59+
- `customValidationRules` - returns rules array with parameter `ValidationContext` from `graphql/validation`
5360

5461
### Using the command-line interface
5562

56-
The node executable contains several commands: `server` and the command-line language service methods (`validate`, `autocomplete`, `outline`).
63+
`graphql-lsp server --schema=localhost:3000`
5764

58-
Improving this list is a work-in-progress.
65+
The node executable contains several commands: `server` and the command-line language service methods (`validate`, `autocomplete`, `outline`).
5966

6067
```
6168

packages/graphql-language-service-cli/src/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import yargs from 'yargs';
1111
import client from './client';
1212

1313
import { Logger, startServer } from 'graphql-language-service-server';
14-
import { tmpdir } from 'os';
1514

1615
const { argv } = yargs
1716
.usage(
@@ -104,7 +103,8 @@ const { argv } = yargs
104103
const command = argv._.pop();
105104

106105
if (!command) {
107-
throw Error('no command supplied');
106+
process.stdout.write('no command supplied');
107+
process.exit(0);
108108
}
109109

110110
switch (command) {
@@ -129,7 +129,7 @@ switch (command) {
129129
try {
130130
startServer(options);
131131
} catch (error) {
132-
const logger = new Logger(tmpdir());
132+
const logger = new Logger();
133133
logger.error(error);
134134
}
135135
break;

packages/graphql-language-service-server/README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,20 @@ The library includes a node executable file which you can find in `./node_module
4242

4343
Check out [graphql-config](https://graphql-config.com/docs/introduction)
4444

45-
#### `.graphqlrc` or `.graphqlrc.yml/yaml`
45+
#### `.graphqlrc` or `.graphqlrc.yml/yaml` or `graphql.config.yml`
4646

4747
```yaml
4848
schema: 'packages/api/src/schema.graphql'
49-
documents: 'packages/app/src/components/**/*.graphql'
49+
documents: 'packages/app/src/components/**/*.{tsx,ts}'
5050
extensions:
51+
endpoints:
52+
example:
53+
url: 'http://localhost:8000'
5154
customExtension:
5255
foo: true
5356
```
5457
55-
#### `.graphqlrc` or `.graphqlrc.json`
58+
#### `.graphqlrc` or `.graphqlrc.json` or `graphql.config.json`
5659

5760
```json
5861
{ "schema": "https://localhost:8000" }
@@ -64,7 +67,7 @@ extensions:
6467
module.exports = { schema: 'https://localhost:8000' };
6568
```
6669

67-
#### custom `loadConfig`
70+
#### custom `startServer`
6871

6972
use graphql config [`loadConfig`](https://graphql-config.com/docs/load-config) for further customization:
7073

@@ -73,14 +76,20 @@ import { loadConfig } from 'graphql-config'; // 3.0.0 or later!
7376
7477
await startServer({
7578
method: 'node',
76-
config: loadConfig({
77-
// myPlatform.config.js works now!
79+
// or instead of configName, an exact path (relative from rootDir or absolute)
80+
81+
// deprecated for: loadConfigOptions.rootDir. root directory for graphql config file(s), or for relative resolution for exact `filePath`. default process.cwd()
82+
// configDir: '',
83+
loadConfigOptions: {
84+
// any of the options for graphql-config@3 `loadConfig()`
85+
86+
// rootDir is same as `configDir` before, the path where the graphql config file would be found by cosmic-config
87+
rootDir: 'config/',
88+
// or - the relative or absolute path to your file
89+
filePath: 'exact/path/to/config.js (also supports yml, json)',
90+
// myPlatform.config.js/json/yaml works now!
7891
configName: 'myPlatform',
79-
// or instead of configName, an exact path (relative from rootDir or absolute)
80-
filePath: 'exact/path/to/config.js (also supports yml, json)'
81-
// rootDir to look for config file(s), or for relative resolution for exact `filePath`. default process.cwd()
82-
rootDir: '',
83-
})
92+
},
8493
});
8594
```
8695

packages/graphql-language-service-server/src/GraphQLCache.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
import { parseDocument } from './parseDocument';
3131
import stringToHash from './stringToHash';
3232
import glob from 'glob';
33-
import { GraphQLExtensionDeclaration } from 'graphql-config/extension';
33+
import { LoadConfigOptions } from './types';
3434

3535
// Maximum files to read when processing GraphQL files.
3636
const MAX_READS = 200;
@@ -53,26 +53,26 @@ const {
5353
DIRECTIVE_DEFINITION,
5454
} = Kind;
5555

56-
export async function getGraphQLCache(
57-
configDir: Uri,
58-
parser: typeof parseDocument,
59-
extensions: GraphQLExtensionDeclaration[] = [],
60-
config?: GraphQLConfig,
61-
fileExtensions: string[] = [],
62-
// loadConfigOptions?: Parameters<typeof loadConfig>,
63-
): Promise<GraphQLCacheInterface> {
64-
const graphQLConfig =
65-
config ??
66-
(await loadConfig({
67-
rootDir: configDir,
68-
extensions,
69-
}));
70-
return new GraphQLCache(
71-
configDir,
72-
graphQLConfig as GraphQLConfig,
56+
export async function getGraphQLCache({
57+
parser,
58+
loadConfigOptions,
59+
config,
60+
}: {
61+
parser: typeof parseDocument;
62+
loadConfigOptions: LoadConfigOptions;
63+
config?: GraphQLConfig;
64+
}): Promise<GraphQLCacheInterface> {
65+
let graphQLConfig;
66+
if (config) {
67+
graphQLConfig = config;
68+
} else {
69+
graphQLConfig = await loadConfig(loadConfigOptions);
70+
}
71+
return new GraphQLCache({
72+
configDir: loadConfigOptions.rootDir as string,
73+
config: graphQLConfig as GraphQLConfig,
7374
parser,
74-
fileExtensions,
75-
);
75+
});
7676
}
7777

7878
export class GraphQLCache implements GraphQLCacheInterface {
@@ -84,23 +84,24 @@ export class GraphQLCache implements GraphQLCacheInterface {
8484
_fragmentDefinitionsCache: Map<Uri, Map<string, FragmentInfo>>;
8585
_typeDefinitionsCache: Map<Uri, Map<string, ObjectTypeInfo>>;
8686
_parser: typeof parseDocument;
87-
_fileExtensions: string[];
88-
89-
constructor(
90-
configDir: Uri,
91-
graphQLConfig: GraphQLConfig,
92-
parser: typeof parseDocument,
93-
fileExtensions: string[],
94-
) {
87+
88+
constructor({
89+
configDir,
90+
config,
91+
parser,
92+
}: {
93+
configDir: Uri;
94+
config: GraphQLConfig;
95+
parser: typeof parseDocument;
96+
}) {
9597
this._configDir = configDir;
96-
this._graphQLConfig = graphQLConfig;
98+
this._graphQLConfig = config;
9799
this._graphQLFileListCache = new Map();
98100
this._schemaMap = new Map();
99101
this._fragmentDefinitionsCache = new Map();
100102
this._typeDefinitionsCache = new Map();
101103
this._typeExtensionMap = new Map();
102104
this._parser = parser;
103-
this._fileExtensions = fileExtensions;
104105
}
105106

106107
getGraphQLConfig = (): GraphQLConfig => this._graphQLConfig;
@@ -807,7 +808,7 @@ export class GraphQLCache implements GraphQLCacheInterface {
807808
let queries: CachedContent[] = [];
808809
if (content.trim().length !== 0) {
809810
try {
810-
queries = this._parser(content, filePath, this._fileExtensions);
811+
queries = this._parser(content, filePath);
811812
if (queries.length === 0) {
812813
// still resolve with an empty ast
813814
resolve({

0 commit comments

Comments
 (0)