Skip to content

Commit 313cb3b

Browse files
authored
allow specifying headers to get introspection (#87)
* allow specifying headers to get introspection * general improvements
1 parent 01844cf commit 313cb3b

File tree

8 files changed

+64
-51
lines changed

8 files changed

+64
-51
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@0no-co/graphqlsp': minor
3+
---
4+
5+
Allow specifying headers for fetching the introspection

packages/example/src/Pokemon.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const PokemonFields = gql`
55
fragment pokemonFields on Pokemon {
66
id
77
name
8-
...someUnknownFragment
98
attacks {
109
fast {
1110
damage
@@ -18,9 +17,8 @@ export const PokemonFields = gql`
1817
export const WeakFields = gql`
1918
fragment weaknessFields on Pokemon {
2019
weaknesses
21-
someUnknownField
2220
}
23-
` as typeof import('./Pokemon.generated').PokemonFieldsFragmentDoc;
21+
` as typeof import('./Pokemon.generated').WeaknessFieldsFragmentDoc;
2422

2523
export const Pokemon = (data: any) => {
2624
const pokemon = useFragment(PokemonFields, data);

packages/example/src/index.generated.ts

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

packages/example/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ const PokemonsQuery = gql`
77
id
88
name
99
fleeRate
10-
11-
__typenam
10+
__typename
1211
}
1312
}
1413

packages/graphqlsp/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ when on a TypeScript file or adding a file like [this](https://github.com/0no-co
4343
4444
### Configuration
4545

46-
- `schema` allows you to specify a url, `.json` or `.graphql` file as your schema
46+
- `schema` allows you to specify a url, `.json` or `.graphql` file as your schema. If you need to specify headers for your introspection
47+
you can opt into the object notation i.e. `{ "schema": { "url": "x", "headers": { "Authorization": "y" } }}`
4748
- `disableTypegen` disables type-generation in general
4849
- `scalars` allows you to pass an object of scalars that we'll feed into `graphql-code-generator`
4950
- `extraTypes` allows you to specify imports or declare types to help with `scalar` definitions

packages/graphqlsp/src/graphql/getSchema.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,14 @@ import fs from 'fs';
1212
import { Logger } from '../index';
1313
import { generateBaseTypes } from './generateTypes';
1414

15+
export type SchemaOrigin = {
16+
url: string;
17+
headers: Record<string, unknown>;
18+
};
19+
1520
export const loadSchema = (
1621
root: string,
17-
schema: string,
22+
schema: SchemaOrigin | string,
1823
logger: Logger,
1924
baseTypesPath: string,
2025
shouldTypegen: boolean,
@@ -24,21 +29,34 @@ export const loadSchema = (
2429
const ref: { current: GraphQLSchema | null } = { current: null };
2530
let url: URL | undefined;
2631

32+
let isJSON = false;
33+
let config: undefined | SchemaOrigin;
34+
2735
try {
28-
url = new URL(schema);
36+
if (typeof schema === 'object') {
37+
url = new URL(schema.url);
38+
} else {
39+
url = new URL(schema);
40+
}
2941
} catch (e) {}
3042

3143
if (url) {
3244
logger(`Fetching introspection from ${url.toString()}`);
3345
fetch(url.toString(), {
3446
method: 'POST',
35-
headers: {
36-
'Content-Type': 'application/json',
37-
},
47+
headers:
48+
isJSON && config
49+
? {
50+
...(config.headers || {}),
51+
'Content-Type': 'application/json',
52+
}
53+
: {
54+
'Content-Type': 'application/json',
55+
},
3856
body: JSON.stringify({
3957
query: getIntrospectionQuery({
4058
descriptions: true,
41-
schemaDescription: true,
59+
schemaDescription: false,
4260
inputValueDeprecation: false,
4361
directiveIsRepeatable: false,
4462
specifiedByUrl: false,
@@ -51,6 +69,7 @@ export const loadSchema = (
5169
else return response.text();
5270
})
5371
.then(result => {
72+
logger(`Got result ${JSON.stringify(result)}`);
5473
if (typeof result === 'string') {
5574
logger(`Got error while fetching introspection ${result}`);
5675
} else if (result.data) {
@@ -73,7 +92,7 @@ export const loadSchema = (
7392
logger(`Got invalid response ${JSON.stringify(result)}`);
7493
}
7594
});
76-
} else {
95+
} else if (typeof schema === 'string') {
7796
const isJson = schema.endsWith('json');
7897
const resolvedPath = path.resolve(path.dirname(root), schema);
7998
logger(`Getting schema from ${resolvedPath}`);

packages/graphqlsp/src/index.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ts from 'typescript/lib/tsserverlibrary';
22

3-
import { loadSchema } from './graphql/getSchema';
3+
import { SchemaOrigin, loadSchema } from './graphql/getSchema';
44
import { getGraphQLCompletions } from './autoComplete';
55
import { getGraphQLQuickInfo } from './quickInfo';
66
import { getGraphQLDiagnostics } from './diagnostics';
@@ -20,20 +20,31 @@ function createBasicDecorator(info: ts.server.PluginCreateInfo) {
2020

2121
export type Logger = (msg: string) => void;
2222

23+
type Config = {
24+
schema: SchemaOrigin | string;
25+
template?: string;
26+
disableTypegen?: boolean;
27+
extraTypes?: string;
28+
scalars?: Record<string, unknown>;
29+
shouldCheckForColocatedFragments?: boolean;
30+
};
31+
2332
function create(info: ts.server.PluginCreateInfo) {
2433
const logger: Logger = (msg: string) =>
2534
info.project.projectService.logger.info(`[GraphQLSP] ${msg}`);
26-
logger('config: ' + JSON.stringify(info.config));
27-
if (!info.config.schema) {
35+
const config: Config = info.config;
36+
37+
logger('config: ' + JSON.stringify(config));
38+
if (!config.schema) {
2839
logger('Missing "schema" option in configuration.');
2940
throw new Error('Please provide a GraphQL Schema!');
3041
}
3142

3243
logger('Setting up the GraphQL Plugin');
3344

34-
const scalars = info.config.scalars || {};
35-
const extraTypes = info.config.extraTypes;
36-
const disableTypegen = info.config.disableTypegen;
45+
const scalars = config.scalars || {};
46+
const extraTypes = config.extraTypes || '';
47+
const disableTypegen = config.disableTypegen || false;
3748

3849
const proxy = createBasicDecorator(info);
3950

@@ -42,7 +53,7 @@ function create(info: ts.server.PluginCreateInfo) {
4253

4354
const schema = loadSchema(
4455
info.project.getProjectName(),
45-
info.config.schema,
56+
config.schema,
4657
logger,
4758
baseTypesPath,
4859
!disableTypegen,

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)