Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix graphql global introspection caching #8740

Merged
merged 2 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/no-code/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "no-code",
"version": "4.8.3",
"private": true,
"scripts": {
"dev": "yarn vite",
"build": "tsc && vite build",
Expand Down
13 changes: 9 additions & 4 deletions packages/ra-data-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export default async (options: Options): Promise<DataProvider> => {
const client = clientObject || buildApolloClient(clientOptions);

let introspectionResults;
let introspectionResultsPromise;

const raDataProvider = new Proxy<DataProvider>(defaultDataProvider, {
get: (target, name) => {
Expand All @@ -155,10 +156,14 @@ export default async (options: Options): Promise<DataProvider> => {
const raFetchMethod = RaFetchMethodMap[name];
return async (resource, params) => {
if (introspection) {
introspectionResults = await resolveIntrospection(
client,
introspection
);
if (!introspectionResultsPromise) {
introspectionResultsPromise = resolveIntrospection(
client,
introspection
);
}

introspectionResults = await introspectionResultsPromise;
}

const buildQuery = buildQueryFactory(introspectionResults);
Expand Down
35 changes: 11 additions & 24 deletions packages/ra-data-graphql/src/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { ApolloClient, gql } from '@apollo/client';

import { ALL_TYPES } from './constants';

let introspectionPromise;

/**
* @param {ApolloClient} client The Apollo client
* @param {Object} options The introspection options
Expand All @@ -19,11 +17,17 @@ export const introspectSchema = async (
client: ApolloClient<unknown>,
options: IntrospectionOptions
) => {
if (introspectionPromise) {
return introspectionPromise;
}
introspectionPromise = runSchemaIntrospection(client, options);
return introspectionPromise;
const schema = options.schema ? options.schema : await fetchSchema(client);
const queries = getQueriesFromSchema(schema);
const types = getTypesFromSchema(schema);
const resources = getResources(types, queries, options);

return {
types,
queries,
resources,
schema,
};
};

export type IntrospectionOptions = {
Expand All @@ -45,23 +49,6 @@ export type IntrospectionResult = {
schema: IntrospectionSchema;
};

const runSchemaIntrospection = async (
client: ApolloClient<unknown>,
options: IntrospectionOptions
) => {
const schema = options.schema ? options.schema : await fetchSchema(client);
const queries = getQueriesFromSchema(schema);
const types = getTypesFromSchema(schema);
const resources = getResources(types, queries, options);

return {
types,
queries,
resources,
schema,
};
};

const fetchSchema = (
client: ApolloClient<unknown>
): Promise<IntrospectionSchema> => {
Expand Down