Mirror: TypeScript LSP plugin that finds GraphQL documents in your code and provides diagnostics, auto-complete and hover-information.

poll schema every minute (#112)

* poll schema every minute

* fix

Changed files
+54 -44
.changeset
packages
graphqlsp
src
graphql
+5
.changeset/odd-bugs-think.md
···
+
---
+
'@0no-co/graphqlsp': patch
+
---
+
+
Poll schema every minute
+49 -44
packages/graphqlsp/src/graphql/getSchema.ts
···
version: 0,
};
let url: URL | undefined;
-
-
let isJSON = false;
-
let config: undefined | SchemaOrigin;
+
let config: { headers: Record<string, unknown> } | undefined;
try {
if (typeof schema === 'object') {
url = new URL(schema.url);
+
config = { headers: schema.headers };
} else {
url = new URL(schema);
}
} catch (e) {}
if (url) {
-
logger(`Fetching introspection from ${url.toString()}`);
-
fetch(url.toString(), {
-
method: 'POST',
-
headers:
-
isJSON && config
+
const pollSchema = () => {
+
logger(`Fetching introspection from ${url!.toString()}`);
+
fetch(url!.toString(), {
+
method: 'POST',
+
headers: config
? {
...(config.headers || {}),
'Content-Type': 'application/json',
···
: {
'Content-Type': 'application/json',
},
-
body: JSON.stringify({
-
query: getIntrospectionQuery({
-
descriptions: true,
-
schemaDescription: false,
-
inputValueDeprecation: false,
-
directiveIsRepeatable: false,
-
specifiedByUrl: false,
+
body: JSON.stringify({
+
query: getIntrospectionQuery({
+
descriptions: true,
+
schemaDescription: false,
+
inputValueDeprecation: false,
+
directiveIsRepeatable: false,
+
specifiedByUrl: false,
+
}),
}),
-
}),
-
})
-
.then(response => {
-
logger(`Got response ${response.statusText} ${response.status}`);
-
if (response.ok) return response.json();
-
else return response.text();
})
-
.then(result => {
-
logger(`Got result ${JSON.stringify(result)}`);
-
if (typeof result === 'string') {
-
logger(`Got error while fetching introspection ${result}`);
-
} else if (result.data) {
-
try {
-
ref.current = buildClientSchema(
-
(result as { data: IntrospectionQuery }).data
-
);
-
ref.version = ref.version + 1;
-
logger(`Got schema for ${url!.toString()}`);
-
if (shouldTypegen)
-
generateBaseTypes(
-
ref.current,
-
baseTypesPath,
-
scalars,
-
extraTypes
+
.then(response => {
+
logger(`Got response ${response.statusText} ${response.status}`);
+
if (response.ok) return response.json();
+
else return response.text();
+
})
+
.then(result => {
+
logger(`Got result ${JSON.stringify(result)}`);
+
if (typeof result === 'string') {
+
logger(`Got error while fetching introspection ${result}`);
+
} else if (result.data) {
+
try {
+
ref.current = buildClientSchema(
+
(result as { data: IntrospectionQuery }).data
);
-
} catch (e: any) {
-
logger(`Got schema error for ${e.message}`);
+
ref.version = ref.version + 1;
+
logger(`Got schema for ${url!.toString()}`);
+
if (shouldTypegen)
+
generateBaseTypes(
+
ref.current,
+
baseTypesPath,
+
scalars,
+
extraTypes
+
);
+
} catch (e: any) {
+
logger(`Got schema error for ${e.message}`);
+
}
+
} else {
+
logger(`Got invalid response ${JSON.stringify(result)}`);
}
-
} else {
-
logger(`Got invalid response ${JSON.stringify(result)}`);
-
}
-
});
+
});
+
};
+
+
pollSchema();
+
setInterval(() => {
+
pollSchema();
+
}, 1000 * 60);
} else if (typeof schema === 'string') {
const isJson = schema.endsWith('json');
const resolvedPath = path.resolve(path.dirname(root), schema);