diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 39735af9e44..7a423a1d778 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,5 @@ name: Node.JS CI -on: [push] +on: [push, pull_request] jobs: test: name: lint & test diff --git a/packages/graphql-language-service-server/README.md b/packages/graphql-language-service-server/README.md index f12a62ede62..01c8da53ddd 100644 --- a/packages/graphql-language-service-server/README.md +++ b/packages/graphql-language-service-server/README.md @@ -5,3 +5,102 @@ [![License](https://img.shields.io/npm/l/graphql-language-service-server.svg?style=flat-square)](LICENSE) Server process backing the [GraphQL Language Service](https://github.com/graphql/graphiql/tree/master/packages/graphql-language-service). + +GraphQL Language Service Server provides an interface for building GraphQL language services for IDEs. + +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. + +Supported features include: + +- Diagnostics (GraphQL syntax linting/validations) (**spec-compliant**) +- Autocomplete suggestions (**spec-compliant**) +- Hyperlink to fragment definitions and named types (type, input, enum) definitions (**spec-compliant**) +- Outline view support for queries + +## Installation and Usage + +### Dependencies + +An LSP compatible client with it's own file watcher, that sends watch notifications to the server. + +**DROPPED**: GraphQL Language Service no longer depends on [Watchman](https://facebook.github.io/watchman/) + +### Installation + +``` +git clone git@github.com:graphql/graphql-language-servic-server.git +cd {path/to/your/repo} +npm install ../graphql-language-service-server +``` + +After pulling the latest changes from this repo, be sure to run `yarn run build` to transform the `src/` directory and generate the `dist/` directory. + +The library includes a node executable file which you can find in `./node_modules/.bin/graphql.js` after installation. + +### GraphQL configuration file (`.graphqlrc.yml`) + +Check out [graphql-config](https://graphql-config.com/docs/introduction) + +The graphql features we support are: + +- `customDirectives` - `['@myExampleDirective']` +- `customValidationRules` - returns rules array with parameter `ValidationContext` from `graphql/validation`; + +## Architectural Overview + +GraphQL Language Service currently communicates via Stream transport with the IDE server. GraphQL server will receive/send RPC messages to perform language service features, while caching the necessary GraphQL artifacts such as fragment definitions, GraphQL schemas etc. More about the server interface and RPC message format below. + +The IDE server should launch a separate GraphQL server with its own child process for each `.graphqlrc.yml` file the IDE finds (using the nearest ancestor directory relative to the file currently being edited): + +``` +./application + + ./productA + .graphqlrc.yml + ProductAQuery.graphql + ProductASchema.graphql + + ./productB + .graphqlrc.yml + ProductBQuery.graphql + ProductBSchema.graphql +``` + +A separate GraphQL server should be instantiated for `ProductA` and `ProductB`, each with its own `.graphqlrc.yml` file, as illustrated in the directory structure above. + +The IDE server should manage the lifecycle of the GraphQL server. Ideally, the IDE server should spawn a child process for each of the GraphQL Language Service processes necessary, and gracefully exit the processes as the IDE closes. In case of errors or a sudden halt the GraphQL Language Service will close as the stream from the IDE closes. + +### Server Interface + +GraphQL Language Server uses [JSON-RPC](http://www.jsonrpc.org/specification) to communicate with the IDE servers. Microsoft's language server currently supports two communication transports: Stream (stdio) and IPC. For IPC transport, the reference guide to be used for development is [the language server protocol](https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md) documentation. + +For each transport, there is a slight difference in JSON message format, especially in how the methods to be invoked are defined - below are the currently supported methods for each transport (will be updated as progress is made): + +| | Stream | IPC | +| ---------------: | ---------------------------- | ------------------------------------------- | +| Diagnostics | `getDiagnostics` | `textDocument/publishDiagnostics` | +| Autocompletion | `getAutocompleteSuggestions` | `textDocument/completion` | +| Outline | `getOutline` | Not supported yet | +| Go-to definition | `getDefinition` | Not supported yet | +| File Events | Not supported yet | `didOpen/didClose/didSave/didChange` events | + +#### startServer + +The GraphQL Language Server can be started with the following function: + +```ts +import { startServer } from 'graphql-language-service-server'; + +await startServer({ + method: 'node', +}); +``` + +`startServer` function takes the following parameters: + +| Parameter | Required | Description | +| ---------- | ------------------------------------------------- | --------------------------------------------------------------------------------- | +| port | `true` when method is `socket`, `false` otherwise | port for the LSP server to run on | +| method | `false` | socket, streams, or node (ipc) | +| configDir | `false` | the directory where graphql-config is found | +| extensions | `false` | array of functions to transform the graphql-config and add extensions dynamically | diff --git a/packages/graphql-language-service-server/src/GraphQLCache.ts b/packages/graphql-language-service-server/src/GraphQLCache.ts index 6d0b7015013..7a8159a4ba5 100644 --- a/packages/graphql-language-service-server/src/GraphQLCache.ts +++ b/packages/graphql-language-service-server/src/GraphQLCache.ts @@ -54,8 +54,14 @@ const { export async function getGraphQLCache( configDir: Uri, + extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>, ): Promise { - const graphQLConfig = await loadConfig({ rootDir: configDir }); + let graphQLConfig = await loadConfig({ rootDir: configDir }); + if (extensions && extensions.length > 0) { + for (const extension of extensions) { + graphQLConfig = await extension(graphQLConfig); + } + } return new GraphQLCache(configDir, graphQLConfig); } diff --git a/packages/graphql-language-service-server/src/MessageProcessor.ts b/packages/graphql-language-service-server/src/MessageProcessor.ts index a7f94232a09..e77e0d82fb3 100644 --- a/packages/graphql-language-service-server/src/MessageProcessor.ts +++ b/packages/graphql-language-service-server/src/MessageProcessor.ts @@ -17,6 +17,7 @@ import { Uri, FileChangeTypeKind, DefinitionQueryResult, + GraphQLConfig, } from 'graphql-language-service-types'; import { GraphQLLanguageService } from 'graphql-language-service-interface'; @@ -76,12 +77,17 @@ export class MessageProcessor { _willShutdown: boolean; _logger: Logger; + _extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>; - constructor(logger: Logger) { + constructor( + logger: Logger, + extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>, + ) { this._textDocumentCache = new Map(); this._isInitialized = false; this._willShutdown = false; this._logger = logger; + this._extensions = extensions; } async handleInitializeRequest( @@ -111,7 +117,7 @@ export class MessageProcessor { ); } - this._graphQLCache = await getGraphQLCache(rootPath); + this._graphQLCache = await getGraphQLCache(rootPath, this._extensions); this._languageService = new GraphQLLanguageService(this._graphQLCache); if (!serverCapabilities) { diff --git a/packages/graphql-language-service-server/src/__tests__/GraphQLCache-test.ts b/packages/graphql-language-service-server/src/__tests__/GraphQLCache-test.ts index a910820120f..afc9110a735 100644 --- a/packages/graphql-language-service-server/src/__tests__/GraphQLCache-test.ts +++ b/packages/graphql-language-service-server/src/__tests__/GraphQLCache-test.ts @@ -19,7 +19,7 @@ import { FragmentDefinitionNode, TypeDefinitionNode, } from 'graphql'; -import { GraphQLCache } from '../GraphQLCache'; +import { GraphQLCache, getGraphQLCache } from '../GraphQLCache'; import { getQueryAndRange } from '../MessageProcessor'; import { FragmentInfo, ObjectTypeInfo } from 'graphql-language-service-types'; @@ -43,6 +43,22 @@ describe('GraphQLCache', () => { fetchMock.restore(); }); + describe('getGraphQLCache', () => { + it('should apply extensions', async () => { + const extension = config => { + return { + ...config, + extension: 'extension-used', // Just adding a key to the config to demo extension usage + }; + }; + const extensions = [extension]; + const cacheWithExtensions = await getGraphQLCache(configDir, extensions); + const config = cacheWithExtensions.getGraphQLConfig(); + expect('extension' in config).toBe(true); + expect((config as any).extension).toBe('extension-used'); + }); + }); + describe('getSchema', () => { it('generates the schema correctly for the test app config', async () => { const schema = await cache.getSchema('testWithSchema'); diff --git a/packages/graphql-language-service-server/src/startServer.ts b/packages/graphql-language-service-server/src/startServer.ts index fac425a5030..512ed264b39 100644 --- a/packages/graphql-language-service-server/src/startServer.ts +++ b/packages/graphql-language-service-server/src/startServer.ts @@ -9,6 +9,7 @@ import * as net from 'net'; import { MessageProcessor } from './MessageProcessor'; +import { GraphQLConfig } from 'graphql-config'; import { createMessageConnection, @@ -49,7 +50,10 @@ type Options = { method?: 'socket' | 'stream' | 'node'; // the directory where graphql-config is found configDir?: string; + // array of functions to transform the graphql-config and add extensions dynamically + extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>; }; +('graphql-language-service-types'); /** * startServer - initialize LSP server with options @@ -86,7 +90,12 @@ export default async function startServer(options: Options): Promise { process.exit(0); }); const connection = createMessageConnection(reader, writer, logger); - addHandlers(connection, logger, options.configDir); + addHandlers( + connection, + logger, + options.configDir, + options.extensions, + ); connection.listen(); }) .listen(port); @@ -102,7 +111,7 @@ export default async function startServer(options: Options): Promise { break; } const connection = createMessageConnection(reader, writer, logger); - addHandlers(connection, logger, options.configDir); + addHandlers(connection, logger, options.configDir, options.extensions); connection.listen(); } } @@ -111,8 +120,9 @@ function addHandlers( connection: MessageConnection, logger: Logger, configDir?: string, + extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>, ): void { - const messageProcessor = new MessageProcessor(logger); + const messageProcessor = new MessageProcessor(logger, extensions); connection.onNotification( DidOpenTextDocumentNotification.type, async params => { diff --git a/packages/graphql-language-service/README.md b/packages/graphql-language-service/README.md index d4e40f9f5e1..3a9fdab9768 100644 --- a/packages/graphql-language-service/README.md +++ b/packages/graphql-language-service/README.md @@ -1,4 +1,6 @@ -# GraphQL Language Service +# graphql-language-service + +> Note: This package will soon be renamed to graphql-language-service-cli [![NPM](https://img.shields.io/npm/v/graphql-language-service.svg)](https://npmjs.com/graphql-language-service) ![npm downloads](https://img.shields.io/npm/dm/graphql-language-service?label=npm%20downloads) @@ -91,41 +93,3 @@ Options: At least one command is required. Commands: "server, validate, autocomplete, outline" ``` - -## Architectural Overview - -GraphQL Language Service currently communicates via Stream transport with the IDE server. GraphQL server will receive/send RPC messages to perform language service features, while caching the necessary GraphQL artifacts such as fragment definitions, GraphQL schemas etc. More about the server interface and RPC message format below. - -The IDE server should launch a separate GraphQL server with its own child process for each `.graphqlrc.yml` file the IDE finds (using the nearest ancestor directory relative to the file currently being edited): - -``` -./application - - ./productA - .graphqlrc.yml - ProductAQuery.graphql - ProductASchema.graphql - - ./productB - .graphqlrc.yml - ProductBQuery.graphql - ProductBSchema.graphql -``` - -A separate GraphQL server should be instantiated for `ProductA` and `ProductB`, each with its own `.graphqlrc.yml` file, as illustrated in the directory structure above. - -The IDE server should manage the lifecycle of the GraphQL server. Ideally, the IDE server should spawn a child process for each of the GraphQL Language Service processes necessary, and gracefully exit the processes as the IDE closes. In case of errors or a sudden halt the GraphQL Language Service will close as the stream from the IDE closes. - -### Server Interface - -GraphQL Language Server uses [JSON-RPC](http://www.jsonrpc.org/specification) to communicate with the IDE servers. Microsoft's language server currently supports two communication transports: Stream (stdio) and IPC. For IPC transport, the reference guide to be used for development is [the language server protocol](https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md) documentation. - -For each transport, there is a slight difference in JSON message format, especially in how the methods to be invoked are defined - below are the currently supported methods for each transport (will be updated as progress is made): - -| | Stream | IPC | -| ---------------: | ---------------------------- | ------------------------------------------- | -| Diagnostics | `getDiagnostics` | `textDocument/publishDiagnostics` | -| Autocompletion | `getAutocompleteSuggestions` | `textDocument/completion` | -| Outline | `getOutline` | Not supported yet | -| Go-to definition | `getDefinition` | Not supported yet | -| File Events | Not supported yet | `didOpen/didClose/didSave/didChange` events | diff --git a/yarn.lock b/yarn.lock index 5a431909106..de145a5566e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5174,11 +5174,6 @@ camelcase@^2.0.0: resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-2.1.1.tgz#7c1d16d679a1bbe59ca02cacecfb011e201f5a1f" integrity sha1-fB0W1nmhu+WcoCys7PsBHiAfWh8= -camelcase@^3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-3.0.0.tgz#32fc4b9fcdaf845fcdf7e73bb97cac2261f0ab0a" - integrity sha1-MvxLn82vhF/N9+c7uXysImHwqwo= - camelcase@^4.0.0, camelcase@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd" @@ -5484,15 +5479,6 @@ clipboardy@1.2.3: arch "^2.1.0" execa "^0.8.0" -cliui@^3.2.0: - version "3.2.0" - resolved "https://registry.yarnpkg.com/cliui/-/cliui-3.2.0.tgz#120601537a916d29940f934da3b48d585a39213d" - integrity sha1-EgYBU3qRbSmUD5NNo7SNWFo5IT0= - dependencies: - string-width "^1.0.1" - strip-ansi "^3.0.1" - wrap-ansi "^2.0.0" - cliui@^4.0.0: version "4.1.0" resolved "https://registry.yarnpkg.com/cliui/-/cliui-4.1.0.tgz#348422dbe82d800b3022eef4f6ac10bf2e4d1b49" @@ -6520,7 +6506,7 @@ decamelize-keys@^1.0.0: decamelize "^1.1.0" map-obj "^1.0.0" -decamelize@^1.1.0, decamelize@^1.1.1, decamelize@^1.1.2, decamelize@^1.2.0: +decamelize@^1.1.0, decamelize@^1.1.2, decamelize@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290" integrity sha1-9lNNFRSCabIDUue+4m9QH5oZEpA= @@ -9565,11 +9551,6 @@ invariant@^2.2.2, invariant@^2.2.3, invariant@^2.2.4: dependencies: loose-envify "^1.0.0" -invert-kv@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6" - integrity sha1-EEqOSqym09jNFXqO+L+rLXo//bY= - invert-kv@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-2.0.0.tgz#7393f5afa59ec9ff5f67a27620d11c226e3eec02" @@ -11259,13 +11240,6 @@ lazy-universal-dotenv@^3.0.1: dotenv "^8.0.0" dotenv-expand "^5.1.0" -lcid@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/lcid/-/lcid-1.0.0.tgz#308accafa0bc483a3867b4b6f2b9506251d1b835" - integrity sha1-MIrMr6C8SDo4Z7S28rlQYlHRuDU= - dependencies: - invert-kv "^1.0.0" - lcid@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/lcid/-/lcid-2.0.0.tgz#6ef5d2df60e52f82eb228a4c373e8d1f397253cf" @@ -12942,13 +12916,6 @@ os-homedir@^1.0.0, os-homedir@^1.0.1: resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3" integrity sha1-/7xJiDNuDoM94MFox+8VISGqf7M= -os-locale@^1.4.0: - version "1.4.0" - resolved "https://registry.yarnpkg.com/os-locale/-/os-locale-1.4.0.tgz#20f9f17ae29ed345e8bde583b13d2009803c14d9" - integrity sha1-IPnxeuKe00XoveWDsT0gCYA8FNk= - dependencies: - lcid "^1.0.0" - os-locale@^3.0.0, os-locale@^3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/os-locale/-/os-locale-3.1.0.tgz#a802a6ee17f24c10483ab9935719cef4ed16bf1a" @@ -16214,7 +16181,7 @@ string-length@^3.1.0: astral-regex "^1.0.0" strip-ansi "^5.2.0" -string-width@^1.0.1, string-width@^1.0.2: +string-width@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3" integrity sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M= @@ -17775,11 +17742,6 @@ whatwg-url@^8.0.0: tr46 "^2.0.0" webidl-conversions "^5.0.0" -which-module@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/which-module/-/which-module-1.0.0.tgz#bba63ca861948994ff307736089e3b96026c2a4f" - integrity sha1-u6Y8qGGUiZT/MHc2CJ47lgJsKk8= - which-module@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/which-module/-/which-module-2.0.0.tgz#d9ef07dce77b9902b8a3a8fa4b31c3e3f7e6e87a" @@ -17996,11 +17958,6 @@ xtend@^4.0.0, xtend@~4.0.1: resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.2.tgz#bb72779f5fa465186b1f438f674fa347fdb5db54" integrity sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ== -y18n@^3.2.1: - version "3.2.1" - resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.1.tgz#6d15fba884c08679c0d77e88e7759e811e07fa41" - integrity sha1-bRX7qITAhnnA136I53WegR4H+kE= - "y18n@^3.2.1 || ^4.0.0", y18n@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/y18n/-/y18n-4.0.0.tgz#95ef94f85ecc81d007c264e190a120f0a3c8566b" @@ -18067,12 +18024,13 @@ yargs-parser@^16.1.0: camelcase "^5.0.0" decamelize "^1.2.0" -yargs-parser@^5.0.0: - version "5.0.0" - resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-5.0.0.tgz#275ecf0d7ffe05c77e64e7c86e4cd94bf0e1228a" - integrity sha1-J17PDX/+Bcd+ZOfIbkzZS/DhIoo= +yargs-parser@^18.1.0: + version "18.1.0" + resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-18.1.0.tgz#1b0ab1118ebd41f68bb30e729f4c83df36ae84c3" + integrity sha512-o/Jr6JBOv6Yx3pL+5naWSoIA2jJ+ZkMYQG/ie9qFbukBe4uzmBatlXFOiu/tNKRWEtyf+n5w7jc/O16ufqOTdQ== dependencies: - camelcase "^3.0.0" + camelcase "^5.0.0" + decamelize "^1.2.0" yargs-unparser@1.6.0: version "1.6.0" @@ -18168,24 +18126,22 @@ yargs@^15.0.0, yargs@^15.0.2: y18n "^4.0.0" yargs-parser "^16.1.0" -"yargs@^3.32.0 || ^7.0.0": - version "7.1.0" - resolved "https://registry.yarnpkg.com/yargs/-/yargs-7.1.0.tgz#6ba318eb16961727f5d284f8ea003e8d6154d0c8" - integrity sha1-a6MY6xaWFyf10oT46gA+jWFU0Mg= +yargs@^15.2.0: + version "15.3.0" + resolved "https://registry.yarnpkg.com/yargs/-/yargs-15.3.0.tgz#403af6edc75b3ae04bf66c94202228ba119f0976" + integrity sha512-g/QCnmjgOl1YJjGsnUg2SatC7NUYEiLXJqxNOQU9qSpjzGtGXda9b+OKccr1kLTy8BN9yqEyqfq5lxlwdc13TA== dependencies: - camelcase "^3.0.0" - cliui "^3.2.0" - decamelize "^1.1.1" - get-caller-file "^1.0.1" - os-locale "^1.4.0" - read-pkg-up "^1.0.1" + cliui "^6.0.0" + decamelize "^1.2.0" + find-up "^4.1.0" + get-caller-file "^2.0.1" require-directory "^2.1.1" - require-main-filename "^1.0.1" + require-main-filename "^2.0.0" set-blocking "^2.0.0" - string-width "^1.0.2" - which-module "^1.0.0" - y18n "^3.2.1" - yargs-parser "^5.0.0" + string-width "^4.2.0" + which-module "^2.0.0" + y18n "^4.0.0" + yargs-parser "^18.1.0" yauzl@2.10.0: version "2.10.0"