GraphQL Language Service Server provides an interface for building GraphQL
language services for IDEs.
If you are developing a service or extension, this is the LSP language server
you want to run.
When developing vscode extensions, just the above is enough to get started for
your extension's ServerOptions.run.module
, for example.
Custom graphql-config
features
The graphql-config features we support are:
module.exports = {
extensions: {
customDirectives: ['@myExampleDirective'],
customValidationRules: require('./config/customValidationRules'),
schemaCacheTTL: 1000,
languageService: {
cacheSchemaFileForLookup: true,
enableValidation: true,
fillLeafsOnComplete: true,
locateCommand(projectName, typePath, info) {
const { path, range } = ourLookupUtility(
projectName,
typePath,
info.type.node,
);
return { uri: path, range };
},
},
},
};
or for multi-project workspaces:
export default {
projects: {
myProject: {
schema: [
'http://localhost:8080',
'./my-project/schema.graphql',
'./my-project/schema.ts',
'@customDirective(arg: String!)',
'scalar CustomScalar',
],
extensions: {
languageService: {
cacheSchemaFileForLookup: true,
enableValidation: false,
},
},
},
anotherProject: {
schema: {
'http://localhost:8081': {
customHeaders: { Authorization: 'Bearer example' },
},
},
},
},
extensions: {
languageService: {
cacheSchemaFileForLookup: false,
enableValidation: true,
},
},
};
You can specify any of these settings globally as above, or per project. Read
the graphql-config docs to learn more about this!
For secrets (headers, urls, etc), you can import dotenv()
and set a base path
as you wish in your graphql-config
file to pre-load process.env
variables.
Troubleshooting notes
- you may need to manually restart the language server for some of these
configurations to take effect
- graphql-config's multi-project support is not related to multi-root workspaces
in vscode - in fact, each workspace can have multiple graphql config projects,
which is what makes multi-root workspaces tricky to support. coming soon!
Workspace Configuration
The LSP Server reads config by sending workspace/configuration
method when it
initializes.
Note: We still do not support LSP multi-root workspaces but will tackle this
very soon!
Many LSP clients beyond vscode offer ways to set these configurations, such as
via initializationOptions
in nvim.coc. The options are mostly designed to
configure graphql-config's load parameters, the only thing we can't configure
with graphql config. The final option can be set in graphql-config
as well
Parameter | Default | Description |
---|
graphql-config.load.baseDir | workspace root or process.cwd() | the path where graphql config looks for config files |
graphql-config.load.filePath | null | exact filepath of the config file. |
graphql-config.load.configName | graphql | config name prefix instead of graphql |
graphql-config.load.legacy | true | backwards compatibility with graphql-config@2 |
graphql-config.dotEnvPath | null | backwards compatibility with graphql-config@2 |
vscode-graphql.cacheSchemaFileForLookup | true if schema contains non-SDL files or URLs | generate an SDL file based on your graphql-config schema configuration for definition lookup and other features. enabled by default when your schema config are URLs or introspection JSON, or if you have any non-local SDL files in schema |
vscode-graphql.schemaCacheTTL | 30000 | an integer value in milliseconds for the desired minimum cache lifetime for all schemas, which also causes the generated file to be re-written. set to 30s by default. effectively a "lazy" form of polling. if you are developing a schema alongside client queries, you may want to decrease this |
vscode-graphql.debug | false | show more verbose log output in the output channel |
all the graphql-config.load.*
configuration values come from static
loadConfig()
options in graphql config.
(more coming soon!)
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 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
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 | textDocument/outline |
Document Symbols | getDocumentSymbols | textDocument/symbols |
Workspace Symbols | getWorkspaceSymbols | workspace/symbols |
Go-to definition | getDefinition | textDocument/definition |
Workspace Definition | getWorkspaceDefinition | workspace/definition |
File Events | Not supported yet | didOpen/didClose/didSave/didChange events |