Skip to content

Commit

Permalink
poll schema every minute (#112)
Browse files Browse the repository at this point in the history
* poll schema every minute

* fix
  • Loading branch information
JoviDeCroock authored Nov 28, 2023
1 parent 5d3ef64 commit 4956811
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-bugs-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphqlsp': patch
---

Poll schema every minute
93 changes: 49 additions & 44 deletions packages/graphqlsp/src/graphql/getSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,71 +31,76 @@ export const loadSchema = (
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);
Expand Down

0 comments on commit 4956811

Please sign in to comment.