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

Web3ApiClient - Preload API resolvers #777

Merged
merged 19 commits into from
Apr 9, 2022
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@
"graphql": "15.5.0",
"@graphql-eslint/eslint-plugin": "1.0.1",
"eslint-plugin-json": "3.0.0"
},
"resolutions": {
"@types/react": "16.9.0",
"@types/react-dom": "16.9.0"
}
}
124 changes: 86 additions & 38 deletions packages/js/client/src/Web3ApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ import {
sanitizeUriRedirects,
sanitizeEnvs,
ClientConfig,
ResolveUriError,
UriResolutionHistory,
resolveUri,
UriToApiResolver,
GetResolversOptions,
UriResolver,
GetUriResolversOptions,
CacheResolver,
ExtendableUriResolver,
Contextualized,
ResolveUriOptions,
coreInterfaceUris,
ResolveUriErrorType,
ResolveUriResult,
} from "@web3api/core-js";
import { Tracer } from "@web3api/tracing-js";

Expand All @@ -61,7 +63,7 @@ export class Web3ApiClient implements Client {
plugins: [],
interfaces: [],
envs: [],
resolvers: [],
uriResolvers: [],
tracingEnabled: false,
};

Expand Down Expand Up @@ -89,7 +91,7 @@ export class Web3ApiClient implements Client {
interfaces: config.interfaces
? sanitizeInterfaceImplementations(config.interfaces)
: [],
resolvers: config.resolvers ?? [],
uriResolvers: config.uriResolvers ?? [],
tracingEnabled: !!config.tracingEnabled,
};
}
Expand Down Expand Up @@ -146,11 +148,11 @@ export class Web3ApiClient implements Client {
return this._getConfig(options.contextId).envs;
}

@Tracer.traceMethod("Web3ApiClient: getResolvers")
public getResolvers(
options: GetResolversOptions = {}
): readonly UriToApiResolver[] {
return this._getConfig(options.contextId).resolvers;
@Tracer.traceMethod("Web3ApiClient: getUriResolvers")
public getUriResolvers(
options: GetUriResolversOptions = {}
): readonly UriResolver[] {
return this._getConfig(options.contextId).uriResolvers;
}

@Tracer.traceMethod("Web3ApiClient: getEnvByUri")
Expand Down Expand Up @@ -435,12 +437,7 @@ export class Web3ApiClient implements Client {
public async resolveUri<TUri extends Uri | string>(
uri: TUri,
options?: ResolveUriOptions<ClientConfig>
): Promise<{
api?: Api;
uri?: Uri;
uriHistory: UriResolutionHistory;
error?: ResolveUriError;
}> {
): Promise<ResolveUriResult> {
options = options || {};

const { contextId, shouldClearContext } = this._setContext(
Expand All @@ -454,15 +451,15 @@ export class Web3ApiClient implements Client {

const client = contextualizeClient(this, contextId);

let resolvers = this.getResolvers({ contextId: contextId });
let uriResolvers = this.getUriResolvers({ contextId: contextId });

if (!cacheRead) {
resolvers = resolvers.filter((x) => x.name !== CacheResolver.name);
uriResolvers = uriResolvers.filter((x) => x.name !== CacheResolver.name);
}

const { api, uri: resolvedUri, uriHistory, error } = await resolveUri(
this._toUri(uri),
resolvers,
uriResolvers,
client,
this._apiCache
);
Expand All @@ -486,6 +483,35 @@ export class Web3ApiClient implements Client {
};
}

@Tracer.traceMethod("Web3ApiClient: loadUriResolverWrappers")
public async loadUriResolvers(): Promise<{
success: boolean;
failedUriResolvers: string[];
}> {
const extendableUriResolver = this.getUriResolvers().find(
(x) => x.name === ExtendableUriResolver.name
) as ExtendableUriResolver;

if (!extendableUriResolver) {
return {
success: true,
failedUriResolvers: [],
};
}

const uriResolverImpls = getImplementations(
coreInterfaceUris.uriResolver,
this.getInterfaces(),
this.getRedirects()
);

return extendableUriResolver.loadUriResolverWrappers(
this,
this._apiCache,
uriResolverImpls
);
}

private _addDefaultConfig() {
const defaultClientConfig = getDefaultClientConfig();

Expand All @@ -501,8 +527,8 @@ export class Web3ApiClient implements Client {
this._config.interfaces.push(...defaultClientConfig.interfaces);
}

if (defaultClientConfig.resolvers) {
this._config.resolvers.push(...defaultClientConfig.resolvers);
if (defaultClientConfig.uriResolvers) {
this._config.uriResolvers.push(...defaultClientConfig.uriResolvers);
}
}

Expand Down Expand Up @@ -647,7 +673,7 @@ export class Web3ApiClient implements Client {
? sanitizeInterfaceImplementations(context.interfaces)
: config.interfaces,
envs: context?.envs ? sanitizeEnvs(context.envs) : config.envs,
resolvers: context?.resolvers ?? config.resolvers,
uriResolvers: context?.uriResolvers ?? config.uriResolvers,
tracingEnabled: context?.tracingEnabled || config.tracingEnabled,
});

Expand All @@ -671,20 +697,41 @@ export class Web3ApiClient implements Client {
});

if (!api) {
if (error && error === ResolveUriError.InfiniteLoop) {
if (error) {
const errorMessage = error.error?.message ?? "";

switch (error.type) {
case ResolveUriErrorType.InfiniteLoop:
throw Error(
`Infinite loop while resolving URI "${uri}".\nResolution Stack: ${JSON.stringify(
uriHistory,
null,
2
)}`
);
break;
case ResolveUriErrorType.InternalResolver:
throw Error(
`URI resolution error while resolving URI "${uri}".\n${errorMessage}\nResolution Stack: ${JSON.stringify(
uriHistory,
null,
2
)}`
);
break;
default:
throw Error(`Unsupported URI resolution error type occurred`);
break;
}
} else {
throw Error(
`Infinite loop while resolving URI "${uri}".\nResolution Stack: ${JSON.stringify(
`Uknown URI resolution error while resolving URI "${uri}"\nResolution Stack: ${JSON.stringify(
uriHistory,
null,
2
)}`
);
}

throw Error(
`No Web3API found at URI: ${uri.uri}` +
`\nResolution history: ${JSON.stringify(uriHistory, null, 2)}`
);
}

return api;
Expand Down Expand Up @@ -732,8 +779,8 @@ const contextualizeClient = (
getEnvs: (options: GetEnvsOptions = {}) => {
return client.getEnvs({ ...options, contextId });
},
getResolvers: (options: GetResolversOptions = {}) => {
return client.getResolvers({ ...options, contextId });
getUriResolvers: (options: GetUriResolversOptions = {}) => {
return client.getUriResolvers({ ...options, contextId });
},
getEnvByUri: <TUri extends Uri | string>(
uri: TUri,
Expand Down Expand Up @@ -771,13 +818,14 @@ const contextualizeClient = (
resolveUri: <TUri extends Uri | string>(
uri: TUri,
options?: ResolveUriOptions<ClientConfig>
): Promise<{
api?: Api;
uri?: Uri;
uriHistory: UriResolutionHistory;
error?: ResolveUriError;
}> => {
): Promise<ResolveUriResult> => {
return client.resolveUri(uri, { ...options, contextId });
},
loadUriResolvers: (): Promise<{
success: boolean;
failedUriResolvers: string[];
}> => {
return client.loadUriResolvers();
},
}
: client;
4 changes: 4 additions & 0 deletions packages/js/client/src/__tests__/Web3ApiClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2458,6 +2458,7 @@ scalar Int32
scalar Bytes
scalar BigInt
scalar JSON
scalar Map

directive @imported(
uri: String!
Expand All @@ -2476,6 +2477,9 @@ directive @capability(
) repeatable on OBJECT

directive @enabled_interface on OBJECT

directive @annotate(type: String!) on FIELD

### Web3API Header END ###

type Query implements Logger_Query @imports(
Expand Down
Loading