Skip to content

Commit fc73ed3

Browse files
authored
Change default recommended configuration file format to json. (#209)
1 parent c2351d2 commit fc73ed3

8 files changed

Lines changed: 119 additions & 55 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vscode-apollo": minor
3+
---
4+
5+
Change default recommended configuration file format to `json`.

README.md

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,35 @@ The Apollo GraphQL extension for VS Code brings an all-in-one tooling experience
2626

2727
<h2 id="getting-started">Getting started</h2>
2828

29-
To get all the benefits of the VS Code experience, it's best to link the schema that is being developed against before installing the extension. The best way to do that is by [publishing a schema](https://www.apollographql.com/docs/graphos/delivery/publishing-schemas/) to the Apollo schema registry. After that's done:
29+
For the VS Code plugin to know how to find the schema, it needs to be linked to either a published schema or a local one.
3030

31-
1. Create an `apollo.config.js` file at the root of the project.
32-
Alternatively, you can create a `cjs`, `mjs`, or `ts` file with the same configuration.
33-
2. Obtain a [Personal API key](https://www.apollographql.com/docs/graphos/api-keys) from GraphOS Studio.
31+
First, create an `apollo.config.json` file at the root of the project.
32+
Alternatively, you can create a `yaml`, `cjs`, `mjs`, or `ts` file with the same configuration.
3433

35-
<h3 id="apollo-config">Setting up an Apollo config</h3>
34+
For the contents of this configuration file, select one of these options:
3635

37-
For the VS Code plugin to know how to find the schema, it needs to be linked to either a published schema or a local one. To link a project to a published schema, edit the `apollo.config.js` file to look like this:
36+
<details>
37+
<summary>
38+
<h3>Configure extension for schemas published to Apollo GraphOS</h3>
39+
</summary>
3840

39-
```js
40-
module.exports = {
41-
client: {
42-
service: "my-graphql-app",
43-
},
44-
};
41+
To get all the benefits of the VS Code experience, it's best to link the schema that is being developed against before installing the extension. The best way to do that is by [publishing a schema](https://www.apollographql.com/docs/graphos/delivery/publishing-schemas/) to the Apollo schema registry.
42+
43+
After that's done, edit the `apollo.config.json` file to look like this:
44+
45+
```json
46+
{
47+
"client": {
48+
"service": "graphos-graph-name"
49+
}
50+
}
4551
```
4652

4753
The `service` name is the name of the graph you've created in [GraphOS Studio](https://studio.apollographql.com).
4854

4955
See [additional configuration options](#additional-apollo-config-options).
5056

51-
<h3 id="api-key">Setting up the <code>.env</code> file</h3>
52-
53-
To authenticate with GraphOS Studio to pull down your schema, create a `.env` file in the same directory as the `apollo.config.js` file. This should be an untracked file (that is, don't commit it to Git).
57+
To authenticate with GraphOS Studio to pull down your schema, create a `.env` file in the same directory as the `apollo.config.json` file. This should be an untracked file (that is, don't commit it to Git).
5458

5559
Then go to your [User Settings page](https://studio.apollographql.com/user-settings/api-keys?referrer=docs-content) in GraphOS Studio to create a new Personal API key.
5660

@@ -64,56 +68,68 @@ APOLLO_KEY=<enter copied key here>
6468

6569
After this is done, VS Code can be reloaded and the Apollo integration will connect to GraphOS Studio to provide autocomplete, validation, and more.
6670

67-
<h3 id="local-schemas">Local schemas</h3>
71+
</details>
72+
73+
<details>
74+
<summary>
75+
<h3 id="local-schemas">Configure extension to use introspection from a locally running service</h3>
76+
</summary>
6877

69-
Sometimes it may make sense to link the editor to a locally running version of a schema to try out new designs that are in active development. To do this, the `apollo.config.js` file can be linked to a local service definition:
78+
Sometimes it may make sense to link the editor to a locally running version of a schema to try out new designs that are in active development. To do this, the `apollo.config.json` file can be linked to a local service definition:
7079

71-
```js
72-
module.exports = {
73-
client: {
74-
service: {
75-
name: "my-graphql-app",
76-
url: "http://localhost:4000/graphql",
77-
},
78-
},
79-
};
80+
```json
81+
{
82+
"client": {
83+
"service": {
84+
"name": "my-graphql-app",
85+
"url": "http://localhost:4000/graphql"
86+
}
87+
}
88+
}
8089
```
8190

8291
Linking to the local schema won't provide all features, such as switching graph variants and performance metrics.
8392

84-
<h3 id="local-schema-files">Local schema files</h3>
93+
</details>
94+
95+
<details>
96+
<summary>
97+
<h3 id="local-schema-files">Configure extension for local schema files</h3>
98+
</summary>
8599

86100
You might not always have a running server to link to, so the extension also supports linking to a local schema file.
87101
This is useful for working on a schema in isolation or for testing out new features.
88-
To link to a local schema file, add the following to the `apollo.config.js` file:
102+
To link to a local schema file, add the following to the `apollo.config.json` file:
89103

90-
```js
91-
module.exports = {
92-
client: {
93-
service: {
104+
```json
105+
{
106+
"client": {
107+
"service": {
94108
// can be a string pointing to a single file or an array of strings
95-
localSchemaFile: "./path/to/schema.graphql",
96-
},
97-
},
98-
};
109+
"localSchemaFile": "./path/to/schema.graphql"
110+
}
111+
}
112+
}
99113
```
100114

101-
<h3 id="client-only-schemas">Client-only schemas</h3>
115+
</details>
116+
117+
<h3 id="client-only-schemas">Adding Client-only schemas</h3>
102118

103119
One of the best features of the VS Code extension is the automatic merging of remote schemas and local ones when using integrated state management with Apollo Client. This happens automatically whenever schema definitions are found within a client project. By default, the VS Code extension will look for all JavaScript, TypeScript and GraphQL files under `./src` to find both the operations and schema definitions for building a complete schema for the application.
104120

105-
Client side schema definitions can be spread throughout the client app project and will be merged together to create one single schema. The default behavior can be controlled by adding specifications to the `apollo.config.js`:
121+
Client side schema definitions can be spread throughout the client app project and will be merged together to create one single schema. The default behavior can be controlled by adding specifications to the `apollo.config.json`:
106122

107-
```js
108-
module.exports = {
109-
client: {
110-
service: "my-graphql-app",
123+
```json
124+
{
125+
"client": {
126+
// "service": <your service configuration>,
111127
// array of glob patterns
112-
includes: ["./src/**/*.js"],
128+
"includes": ["./src/**/*.js"],
113129
// array of glob patterns
114-
excludes: ["**/__tests__/**"],
115-
},
116-
};
130+
"excludes": ["**/__tests__/**"]
131+
}
132+
}
117133
```
118134

119135
<h3 id="get-the-extension">Get the extension</h3>
@@ -195,11 +211,11 @@ _Optional_ - custom tagged template literal.
195211

196212
When using GraphQL with JavaScript or TypeScript projects, it is common to use the `gql` tagged template literal to write out operations. Apollo tools look through your files for the `gql` tag to extract your queries, so if you use a different template literal, you can configure it like so:
197213

198-
```js
199-
module.exports = {
200-
client: {
201-
tagName: "graphql", // highlight-line
202-
service: ...
214+
```json
215+
{
216+
"client": {
217+
"tagName": "graphql",
218+
"service": //...
203219
}
204-
};
220+
}
205221
```

package-lock.json

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"graphql": "16.9.0",
4949
"graphql-language-service": "5.2.2",
5050
"graphql-tag": "2.12.6",
51+
"jsonc-parser": "^3.3.1",
5152
"lodash.debounce": "4.0.8",
5253
"lodash.merge": "4.6.2",
5354
"lodash.throttle": "4.1.1",
@@ -150,6 +151,12 @@
150151
"GraphQL"
151152
],
152153
"configuration": "./graphql.configuration.json"
154+
},
155+
{
156+
"id": "jsonc",
157+
"filenames": [
158+
"apollo.config.json"
159+
]
153160
}
154161
],
155162
"grammars": [
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"client": {
33
"service": {
4+
// test
45
"name": "localMultiSchema",
56
"localSchemaFile": ["./starwarsSchema.graphql", "./planets.graphql"]
67
}
78
}
8-
}
9+
}

src/build.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function main() {
2828
/* add to the end of plugins array */
2929
esbuildProblemMatcherPlugin,
3030
buildJsonSchemaPlugin,
31+
resolvePlugin,
3132
],
3233
});
3334
if (watch) {
@@ -90,6 +91,18 @@ const buildJsonSchemaPlugin = {
9091
},
9192
};
9293

94+
const resolvePlugin = {
95+
name: "resolve",
96+
setup(build) {
97+
build.onResolve(
98+
{ filter: /^jsonc-parser$/ },
99+
async ({ path, ...options }) => {
100+
return build.resolve("jsonc-parser/lib/esm/main.js", options);
101+
},
102+
);
103+
},
104+
};
105+
93106
main().catch((e) => {
94107
console.error(e);
95108
process.exit(1);

src/language-server/config/__tests__/loadConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ client:
217217

218218
it("loads config from a json file", async () => {
219219
writeFilesToDir(dir, {
220-
"apollo.config.json": `{"client": {"service": "hello"} }`,
220+
"apollo.config.json": `{"client": /* testing jsonc */ {"service": "hello"} }`,
221221
});
222222
const config = await loadConfig({ configPath: dirPath });
223223
expect(config?.client?.service).toEqual("hello");

src/language-server/config/loadConfig.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cosmiconfig, defaultLoaders } from "cosmiconfig";
1+
import { cosmiconfig, defaultLoaders, Loader } from "cosmiconfig";
22
import { dirname, resolve } from "path";
33
import { readFileSync, existsSync, lstatSync } from "fs";
44
import {
@@ -9,6 +9,7 @@ import {
99
import { getServiceFromKey } from "./utils";
1010
import { URI } from "vscode-uri";
1111
import { Debug } from "../utilities";
12+
import { ParseError, parse as parseJsonC } from "jsonc-parser";
1213
import { loadJs, loadTs } from "./loadTsConfig";
1314

1415
// config settings
@@ -42,6 +43,19 @@ export type ConfigResult<T> = {
4243

4344
// XXX load .env files automatically
4445

46+
const loadJsonc: Loader = (filename, contents) => {
47+
const errors: ParseError[] = [];
48+
try {
49+
return parseJsonC(contents, errors);
50+
} finally {
51+
if (errors.length) {
52+
Debug.error(
53+
`Error parsing JSONC file ${filename}, file might not be valid JSONC`,
54+
);
55+
}
56+
}
57+
};
58+
4559
export async function loadConfig({
4660
configPath,
4761
}: LoadConfigSettings): Promise<ApolloConfig | null> {
@@ -53,6 +67,7 @@ export async function loadConfig({
5367
".mjs": loadJs,
5468
".cjs": loadJs,
5569
".js": loadJs,
70+
".json": loadJsonc,
5671
},
5772
});
5873

0 commit comments

Comments
 (0)