-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(graphql-config): add graphql config extensions #1118
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e4c275e
feat(extensions): add graphql config extensions
1dd5c00
fix(extensions): use new node syntax
f4d926e
Merge branch 'master' into feat-extensions
5cdc15e
chore: merge with master
f6d8a56
fix: type of method in startServer
4d9138d
chore: add pull_request to ci trigger
1e43e06
refactor: remove duplicate extension apply code
2fe3e1d
test: add test for getGraphQLCache
f0f5af2
docs: add docs for startServer function
45c9323
chore: remove stale disabled deslint rule
9e3e973
docs: split readme of cli, server
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name: Node.JS CI | ||
on: [push] | ||
on: [push, pull_request] | ||
jobs: | ||
test: | ||
name: lint & test | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 [email protected]: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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks incorrect. I think this should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed via f6d8a56 |
||
// 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<void> { | |
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<void> { | |
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 => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@divyenduz so this is intended to re-declare
graphQLConfig
on every invocation? I'm guessing the result ofawait extension(graphQLConfig)
returns the previous config plus the extension?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, as of now, the signature of an extension is a function that takes a GraphQLConfig as input and returns a GraphQL config as its output with the modification (extension) applied.
extensions?: Array<(config: GraphQLConfig) => GraphQLConfig>
Which implies,
extension: (config: GraphQLConfig) => GraphQLConfig