diff --git a/apps/back-office/src/app/graphql.module.ts b/apps/back-office/src/app/graphql.module.ts index 5fddd0e4aa..9f605baa10 100644 --- a/apps/back-office/src/app/graphql.module.ts +++ b/apps/back-office/src/app/graphql.module.ts @@ -4,16 +4,13 @@ import { ApolloClientOptions, ApolloLink, InMemoryCache, - split, } from '@apollo/client/core'; import { HttpLink } from 'apollo-angular/http'; import { setContext } from '@apollo/client/link/context'; import { getMainDefinition } from '@apollo/client/utilities'; import { environment } from '../environments/environment'; -import extractFiles from 'extract-files/extractFiles.mjs'; -import isExtractableFile from 'extract-files/isExtractableFile.mjs'; -import { GraphQLWsLink } from '@apollo/client/link/subscriptions'; -import { createClient } from 'graphql-ws'; +import { buildClientSchema } from 'graphql/utilities'; +import { fetch } from 'cross-fetch'; /** * Configuration of the Apollo client. @@ -21,7 +18,9 @@ import { createClient } from 'graphql-ws'; * @param httpLink Apollo http link * @returns void */ -export const createApollo = (httpLink: HttpLink): ApolloClientOptions => { +export const createApollo = async ( + httpLink: HttpLink +): Promise> => { const basic = setContext(() => ({ headers: { // eslint-disable-next-line @typescript-eslint/naming-convention @@ -30,43 +29,53 @@ export const createApollo = (httpLink: HttpLink): ApolloClientOptions => { }, })); - const http = httpLink.create({ - uri: `${environment.apiUrl}/graphql`, - extractFiles: (body) => extractFiles(body, isExtractableFile), - }); + // Fetch the schema url from back-end + // NOT WORKING: we must be logged in and here we're not + // const schemaEndpoint = await fetch(`${environment.apiUrl}/schema/url`); + // const schemaEndpointString = await schemaEndpoint.text(); - const ws = new GraphQLWsLink( - createClient({ - url: `${environment.subscriptionApiUrl}/graphql`, - connectionParams: { - authToken: localStorage.getItem('idtoken'), - }, - }) + // Fetch the schema file from the fetched endpoint + // NOT WORKING: CORS ERROR + const response = await fetch( + 'https://oortdevstorage.blob.core.windows.net/public/introspection/schema?1715023102100' ); - - /** Definition for apollo link query definition */ - interface Definition { - kind: string; - operation?: string; - } + const schemaString = await response.text(); + const schema = buildClientSchema(JSON.parse(schemaString)); const link = ApolloLink.from([ basic, - split( - ({ query }) => { - const { kind, operation }: Definition = getMainDefinition(query); - return kind === 'OperationDefinition' && operation === 'subscription'; - }, - ws, - http - ), + httpLink.create({ + uri: `${environment.apiUrl}/graphql`, + }), ]); + // Create a link that uses the introspected schema + const schemaLink = new ApolloLink((operation, forward) => { + operation.setContext(({ graphqlContext = {} }) => ({ + graphqlContext: { + ...graphqlContext, + schema, + }, + })); + + return forward(operation); + }); + // Cache is not currently used, due to fetchPolicy values const cache = new InMemoryCache(); return { - link, + link: ApolloLink.split( + (operation) => { + const definition = getMainDefinition(operation.query); + return ( + definition.kind === 'OperationDefinition' && + definition.operation === 'subscription' + ); + }, + schemaLink.concat(link), + link + ), cache, defaultOptions: { watchQuery: { diff --git a/package.json b/package.json index 2f6f16df9f..c9149ce56e 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "cron-parser": "^4.7.0", "cron-validator": "^1.3.1", "cronstrue": "^2.20.0", + "cross-fetch": "^4.0.0", "esri-leaflet": "^3.0.10", "esri-leaflet-cluster": "^3.0.1", "esri-leaflet-geocoder": "^3.1.4", @@ -151,11 +152,11 @@ "@types/jasminewd2": "~2.0.3", "@types/jest": "28.1.1", "@types/jsonpath": "^0.2.0", - "@types/simpleheat": "^0.4.3", "@types/leaflet.heat": "^0.2.1", "@types/leaflet.markercluster": "^1.5.1", "@types/node": "18.7.1", "@types/proj4": "^2.5.2", + "@types/simpleheat": "^0.4.3", "@types/to-json-schema": "^0.2.4", "@types/uuid": "^9.0.1", "@types/ws": "^7.4.1",