Skip to content

Instantly share code, notes, and snippets.

@nightroman
Last active February 19, 2023 17:16
Show Gist options
  • Save nightroman/f48f843483cd76a73f25cdd8a68fdd94 to your computer and use it in GitHub Desktop.
Save nightroman/f48f843483cd76a73f25cdd8a68fdd94 to your computer and use it in GitHub Desktop.
<#
.Synopsis
Downloads or updates GraphQL schema using local StrawberryShake.Tools.
https://gist.github.com/nightroman/f48f843483cd76a73f25cdd8a68fdd94
.Parameter Uri
The GraphQL service URL.
If it is omitted, .graphqlrc.json is used to get it.
.Parameter Path
The schema file path.
The default is 'schema.graphql'.
#>
[CmdletBinding()]
param(
[Uri]$Uri
,
[string]$Path = 'schema.graphql'
)
$Path = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Path)
$Root = [IO.Path]::GetDirectoryName($Path)
### Get Uri
if (!$Uri) {
$rc = Join-Path $Root .graphqlrc.json
if (!(Test-Path -LiteralPath $rc)) {
throw "Cannot resolve not specified Uri."
}
$data = [IO.File]::ReadAllText($rc) | ConvertFrom-Json
$Uri = $data.extensions.strawberryShake.url
if (!$Uri) {
throw "Cannot get Uri as extensions.strawberryShake.url from $rc"
}
}
Write-Host "Uri=$Uri"
### Download schema
Write-Host "Download schema $Path"
dotnet graphql download -f $Path $Uri | Out-Host
if ($LASTEXITCODE) {
throw "download error code $LASTEXITCODE"
}
$remove = @'
"The name scalar represents a valid GraphQL name as specified in the spec and can be used to refer to fields or types."
scalar Name
"Delegates a resolver to a remote schema."
directive @delegate("The path to the field on the remote schema." path: String "The name of the schema to which this field shall be delegated to." schema: Name!) on FIELD_DEFINITION
directive @computed("Specifies the fields on which a computed field is dependent on." dependantOn: [Name!]) on FIELD_DEFINITION
"Annotates the original name of a type."
directive @source("The original name of the annotated type." name: Name! "The name of the schema to which this type belongs to." schema: Name!) repeatable on ENUM | OBJECT | INTERFACE | UNION | INPUT_OBJECT | FIELD_DEFINITION | INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION | ENUM_VALUE
'@
### Fix schema
Write-Host "Fix schema"
$remove = $remove -split '\r?\n'
$lines = Get-Content -LiteralPath $Path
$(
for($$ = -1; ++$$ -lt $lines.Count) {
$line = $lines[$$]
if ($line -in $remove) {
if ($line.StartsWith('directive') -and $lines[$$ + 1] -eq '') {
++$$
}
}
else {
$line
}
}
) | Set-Content -LiteralPath $Path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment