diff --git a/docs/reference/README.md b/docs/reference/README.md index e3ec6d6c..2db698fa 100644 --- a/docs/reference/README.md +++ b/docs/reference/README.md @@ -27,6 +27,7 @@ - [gcp/secretmanager](./gcp/secretmanager.md) - Google Cloud Secret Manager - [git](./git.md) - Git operations - [go](./go.md) - Go build operations +- [graphql](./graphql.md) - - - [http](./http.md) - - - [io](./io.md) - IO operations - [java/maven](./java/maven.md) - Maven is a build automation tool for Java diff --git a/docs/reference/graphql.md b/docs/reference/graphql.md new file mode 100644 index 00000000..5aebd055 --- /dev/null +++ b/docs/reference/graphql.md @@ -0,0 +1,23 @@ +--- +sidebar_label: graphql +--- + +# alpha.dagger.io/graphql + +```cue +import "alpha.dagger.io/graphql" +``` + +## graphql.#Query + +### graphql.#Query Inputs + +_No input._ + +### graphql.#Query Outputs + +| Name | Type | Description | +| ------------- |:-------------: |:-------------: | +|*post.statusCode* | `string` |- | +|*post.body* | `string` |- | +|*data* | `_\|_` |- | diff --git a/stdlib/.dagger/env/graphql/.gitignore b/stdlib/.dagger/env/graphql/.gitignore new file mode 100644 index 00000000..01ec19b0 --- /dev/null +++ b/stdlib/.dagger/env/graphql/.gitignore @@ -0,0 +1,2 @@ +# dagger state +state/** diff --git a/stdlib/.dagger/env/graphql/values.yaml b/stdlib/.dagger/env/graphql/values.yaml new file mode 100644 index 00000000..1b4f3520 --- /dev/null +++ b/stdlib/.dagger/env/graphql/values.yaml @@ -0,0 +1,27 @@ +plan: + package: ./graphql/tests +name: graphql +inputs: + TestDockersocket: + socket: + unix: /var/run/docker.sock +sops: + kms: [] + gcp_kms: [] + azure_kv: [] + hc_vault: [] + age: + - recipient: age1gxwmtwahzwdmrskhf90ppwlnze30lgpm056kuesrxzeuyclrwvpsupwtpk + enc: | + -----BEGIN AGE ENCRYPTED FILE----- + YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSAyRUk0Q3BNOFBRUEl0amtZ + QS9ZeVdPaTZ5aE5Nbk90eC80aURGZytxQ2g0CjNZSVVvcnVldzNzN3VybjRXMy9T + Sml1QmV6YW1tVlVKUEI5aE5XWTcrY00KLS0tIE05ZllhMkppSFRyN2ZjdUlvMktV + V0J1ZC9BVEJCb1pWcWdsc0lkWDdmYWcKFA7YrasrdKXAnbv4xtebelPia4GREZa7 + lv1rAmsTc7w4MER5h+syxHHr2C0fsysR6WZHNS+lRPH+b4eZCvgHvg== + -----END AGE ENCRYPTED FILE----- + lastmodified: "2021-11-25T13:14:38Z" + mac: ENC[AES256_GCM,data:EuIpdctm28LZwIgdoq+73l1VzG2198s4Kb1qQSeGE5kqscPIFskh1c+DsP5VDtDCExRC5sAVHnDIwWst+Zh5Q9TzbLtMg/+5bYYtBWtf/XZldb1aaGRHdl/g4mILvmZmxdCDmU8BFv7snmIswsrP8X3dY3ItOG85hAJndoY6irA=,iv:UfFVg/gs3qEn3xDQv6g9UqLw4josOCl1Xjl3RsH+9k0=,tag:fUcFZFHdd6Mod+RXdf7sUg==,type:str] + pgp: [] + encrypted_suffix: secret + version: 3.7.1 diff --git a/stdlib/graphql/graphql.cue b/stdlib/graphql/graphql.cue new file mode 100644 index 00000000..60ad3faa --- /dev/null +++ b/stdlib/graphql/graphql.cue @@ -0,0 +1,39 @@ +package graphql + +import ( + "encoding/json" + + "alpha.dagger.io/dagger" + "alpha.dagger.io/http" +) + +#Query: { + // Contents of the graphql query + query: string + // graphql variables + variable: [key=string]: _ + // graphql url + url: string + //API Token + token: dagger.#Input & dagger.#Secret | *null + + post: http.#Post & { + "url": url + request: { + body: json.Marshal({ + "query": query + variables: json.Marshal(variable) + }) + header: "Content-Type": "application/json" + token: token + } + } + + payload: { + data: {...} + errors?: {...} + } + payload: json.Unmarshal(post.response.body) + data: payload.data @dagger(output) + errors?: payload.errors @dagger(output) +} diff --git a/stdlib/graphql/tests/graphql.cue b/stdlib/graphql/tests/graphql.cue new file mode 100644 index 00000000..f4ffb916 --- /dev/null +++ b/stdlib/graphql/tests/graphql.cue @@ -0,0 +1,55 @@ +package graphql + +import ( + "encoding/json" + + "alpha.dagger.io/alpine" + "alpha.dagger.io/dagger" + "alpha.dagger.io/docker" + "alpha.dagger.io/os" + "alpha.dagger.io/http" +) + +TestDockersocket: dagger.#Stream & dagger.#Input + +TestQuery: { + + run: docker.#Run & { + name: "graphql-faker" + ref: "apisguru/graphql-faker" + socket: TestDockersocket + ports: ["8080:9002"] + } + + // Waits for TestRun to finish initializing + Testhealth: http.#Wait & { + url: "http://localhost:8080/graphql?query={%7BallCompanies%20%7B%0A%20%20%20%20id%0A%20%20%7D%0A%7D}" + } + + queryWithoutToken: #Query & { + url: Testhealth.url + query: #""" + { + company(id: "NjExNjAwMjE5Nw==") { + id + } + } + """# + } + + testRaw: os.#Container & { + image: alpine.#Image & { + package: jq: "~=1.6" + } + env: STATUS: "\(queryWithoutToken.post.response.statusCode)" + shell: args: ["--noprofile", "--norc", "-eo", "pipefail", "-c"] + files: "/content.json": { + content: json.Marshal(queryWithoutToken.data) + mode: 0o500 + } + command: #""" + test "$STATUS" = "200" + test "$(cat /content.json | jq '.allCompanies | length')" -gt 1 + """# + } +} diff --git a/stdlib/http/http.cue b/stdlib/http/http.cue index 2628324f..00d006a5 100644 --- a/stdlib/http/http.cue +++ b/stdlib/http/http.cue @@ -44,7 +44,7 @@ import ( } command: #""" curlArgs=( - "$URL" + "$URL" -L --fail --silent --show-error --write-out "%{http_code}" -X "$METHOD" @@ -60,7 +60,6 @@ import ( if [ -e /token ]; then curlArgs+=("-H" "Authorization: bearer $(cat /token)") fi - curl "${curlArgs[@]}" > /status """# }