diff --git a/.changeset/odd-bugs-think.md b/.changeset/odd-bugs-think.md new file mode 100644 index 00000000..7ab34bcf --- /dev/null +++ b/.changeset/odd-bugs-think.md @@ -0,0 +1,5 @@ +--- +'@0no-co/graphqlsp': patch +--- + +Poll schema every minute diff --git a/packages/graphqlsp/src/graphql/getSchema.ts b/packages/graphqlsp/src/graphql/getSchema.ts index 94db28a8..f1b32f11 100644 --- a/packages/graphqlsp/src/graphql/getSchema.ts +++ b/packages/graphqlsp/src/graphql/getSchema.ts @@ -31,24 +31,23 @@ export const loadSchema = ( version: 0, }; let url: URL | undefined; - - let isJSON = false; - let config: undefined | SchemaOrigin; + let config: { headers: Record } | 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', @@ -56,46 +55,52 @@ export const loadSchema = ( : { '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);