Skip to content

feat/AB#91885_Store-GraphQL-schema-in-public-blob-storage-file #2513

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

Closed
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
73 changes: 41 additions & 32 deletions apps/back-office/src/app/graphql.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ 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.
*
* @param httpLink Apollo http link
* @returns void
*/
export const createApollo = (httpLink: HttpLink): ApolloClientOptions<any> => {
export const createApollo = async (
httpLink: HttpLink
): Promise<ApolloClientOptions<any>> => {
const basic = setContext(() => ({
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -30,43 +29,53 @@ export const createApollo = (httpLink: HttpLink): ApolloClientOptions<any> => {
},
}));

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: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down