generated from salesforcecli/plugin-template-sf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add api request graphql command, UTs, NUTs
* feat: add api request graphql command, UTs, NUTs * test: add logging * refactor: move test files, local project * chore: populate force-app * chore: remove logging * chore: update --body flag to read file/stdin/value * chore: share flags/methods * chore: fix shared method, got method * chore: --api-version * chore: example/classes * chore: add .gitkeep in sample proj * test: add NUT with --body and directly passing in grapql --------- Co-authored-by: mshanemc <shane.mclaughlin@salesforce.com>
- Loading branch information
1 parent
86fac95
commit 04adffe
Showing
19 changed files
with
601 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# summary | ||
|
||
Execute GraphQL statements | ||
|
||
# description | ||
|
||
Run any valid GraphQL statement via the /graphql [API](https://developer.salesforce.com/docs/platform/graphql/guide/graphql-about.html) | ||
|
||
# examples | ||
|
||
- Runs the graphql query directly via the command line | ||
|
||
<%= config.bin %> <%= command.id %> --body "query accounts { uiapi { query { Account { edges { node { Id \n Name { value } } } } } } }" | ||
|
||
- Runs a mutation to create an Account, with an `example.txt` file, containing | ||
|
||
```text | ||
mutation AccountExample{ | ||
uiapi { | ||
AccountCreate(input: { | ||
Account: { | ||
Name: "Trailblazer Express" | ||
} | ||
}) { | ||
Record { | ||
Id | ||
Name { | ||
value | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
<%= config.bin %> <%= command.id %> --body example.txt | ||
|
||
will create a new account returning specified fields (Id, Name) | ||
|
||
# flags.header.summary | ||
|
||
HTTP header in "key:value" format. | ||
|
||
# flags.body.summary | ||
|
||
File or content with GraphQL statement. Specify "-" to read from standard input. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# flags.include.summary | ||
|
||
Include the HTTP response status and headers in the output. | ||
|
||
# flags.stream-to-file.summary | ||
|
||
Stream responses to a file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2023, salesforce.com, inc. | ||
* All rights reserved. | ||
* Licensed under the BSD 3-Clause license. | ||
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause | ||
*/ | ||
|
||
import fs from 'node:fs'; | ||
import * as os from 'node:os'; | ||
import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; | ||
import { Messages, Org, SFDX_HTTP_HEADERS } from '@salesforce/core'; | ||
import { ProxyAgent } from 'proxy-agent'; | ||
import { includeFlag, sendAndPrintRequest, streamToFileFlag } from '../../../shared/shared.js'; | ||
|
||
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); | ||
const messages = Messages.loadMessages('@salesforce/plugin-api', 'graphql'); | ||
|
||
export default class Graphql extends SfCommand<void> { | ||
public static readonly summary = messages.getMessage('summary'); | ||
public static readonly description = messages.getMessage('description'); | ||
public static readonly examples = messages.getMessages('examples'); | ||
public static readonly state = 'beta'; | ||
|
||
public static readonly flags = { | ||
'target-org': Flags.requiredOrg(), | ||
'api-version': Flags.orgApiVersion(), | ||
'stream-to-file': streamToFileFlag, | ||
include: includeFlag, | ||
body: Flags.string({ | ||
summary: messages.getMessage('flags.body.summary'), | ||
allowStdin: true, | ||
helpValue: 'file', | ||
required: true, | ||
}), | ||
}; | ||
|
||
public async run(): Promise<void> { | ||
const { flags } = await this.parse(Graphql); | ||
|
||
const org = flags['target-org']; | ||
const streamFile = flags['stream-to-file']; | ||
const apiVersion = flags['api-version'] ?? (await org.retrieveMaxApiVersion()); | ||
const body = `{"query":"${(fs.existsSync(flags.body) ? fs.readFileSync(flags.body, 'utf8') : flags.body) | ||
.replaceAll(os.EOL, '\\n') | ||
.replaceAll('"', '\\"')}"}`; | ||
const url = new URL(`${org.getField<string>(Org.Fields.INSTANCE_URL)}/services/data/v${apiVersion}/graphql`); | ||
|
||
await org.refreshAuth(); | ||
|
||
const options = { | ||
agent: { https: new ProxyAgent() }, | ||
headers: { | ||
...SFDX_HTTP_HEADERS, | ||
Authorization: `Bearer ${org.getConnection(apiVersion).getConnectionOptions().accessToken!}`, | ||
}, | ||
body, | ||
throwHttpErrors: false, | ||
followRedirect: false, | ||
}; | ||
|
||
await sendAndPrintRequest({ streamFile, url, options, include: flags.include, this: this }); | ||
} | ||
} |
Oops, something went wrong.