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

Create a GraphQL wrapper API for extensions #15566

Merged
merged 10 commits into from Nov 17, 2020
11 changes: 11 additions & 0 deletions client/packages/sourcegraph-extension-api/src/sourcegraph.d.ts
Expand Up @@ -1609,6 +1609,17 @@ declare module 'sourcegraph' {
export function executeCommand<T = any>(command: string, ...args: any[]): Promise<T>
}

export namespace graphQL {
/**
* Executes a Sourcegraph GraphQL API query or mutation on the associated Sourcegraph instance and returns a promise for the result.
tjkandala marked this conversation as resolved.
Show resolved Hide resolved
*
* @param query The GraphQL query
* @param variables A key/value object with variable values
* @returns Promise for the result of the GraphQL query
*/
export function queryGraphQL<T = any>(query: string, variables: { [key: string]: any }): Promise<T>
tjkandala marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* A description of the information available at a URL.
*/
Expand Down
2 changes: 2 additions & 0 deletions client/shared/src/api/extension/extensionHost.ts
Expand Up @@ -148,6 +148,7 @@ function createExtensionAPI(
commands,
search,
languages: { registerHoverProvider, registerDocumentHighlightProvider, registerDefinitionProvider },
graphQL,
} = initNewExtensionAPI(proxy, initData.initialSettings, documents)

// Expose the extension host API to the client (main thread)
Expand Down Expand Up @@ -254,6 +255,7 @@ function createExtensionAPI(

search,
commands,
graphQL,
content: {
registerLinkPreviewProvider: (urlMatchPattern: string, provider: sourcegraph.LinkPreviewProvider) =>
content.registerLinkPreviewProvider(urlMatchPattern, provider),
Expand Down
8 changes: 8 additions & 0 deletions client/shared/src/api/extension/flatExtensionApi.ts
Expand Up @@ -56,6 +56,7 @@ export interface InitResult {
typeof sourcegraph['languages'],
'registerHoverProvider' | 'registerDocumentHighlightProvider' | 'registerDefinitionProvider'
>
graphQL: typeof sourcegraph['graphQL']
}

/**
Expand Down Expand Up @@ -226,6 +227,12 @@ export const initNewExtensionAPI = (
provider: sourcegraph.DefinitionProvider
): sourcegraph.Unsubscribable => addWithRollback(state.definitionProviders, { selector, provider })

// GraphQL
const graphQL: typeof sourcegraph['graphQL'] = {
// `graphQL.queryGraphQL` is simply a discoverable wrapper over the `queryGraphQL` command.
queryGraphQL: (query, variables) => commands.executeCommand('queryGraphQL', query, variables),
tjkandala marked this conversation as resolved.
Show resolved Hide resolved
}

return {
configuration: Object.assign(configChanges.asObservable(), {
get: getConfiguration,
Expand All @@ -240,6 +247,7 @@ export const initNewExtensionAPI = (
registerDocumentHighlightProvider,
registerDefinitionProvider,
},
graphQL,
}
}

Expand Down