GraphQL is a different type of REST request you can use on FlexDeploy servers. It’s main advantage over traditional REST Endpoints is its ability for a user to define exactly the data they want in a request. The request will only retrieve the data selected in a request. This can lead to better response times and more customizable data retrieval from an a server. To get started, a tool is needed in in order to send requests to your FlexDeploy server. Several options exist like curl or other REST clients, but we will be using a Postman in order to send out GraphQL requests in this example.
Table of Contents | ||||
---|---|---|---|---|
|
...
First, there are some steps in postman Postman that need to be done before writing a request.
Create an HTTP Request
Make sure the Request is a POST request
Set the URL to the format below, replacing the FLEXDEPLOY_SERVER_HOSTNAME and FLEXDEPLOY_SERVER_PORT with your FlexDeploy server’s hostname and port respectively
http(s)://{{FLEXDEPLOY_SERVER_HOSTNAME}}:{{FLEXDEPLOY_SERVER_PORT}}/flexdeploy/rest/v2/graphql
Set up security information under the “Authorization” tab. Either Basic Auth (username and password) or a an API token can be used here
Under the “Body” tab of the request, switch the body format to GraphQL by clicking on the dot next to “GraphQL” radio button
...
Fetching Schema and Introspection
Anchor | ||||
---|---|---|---|---|
|
A GraphQL schema is a complete outline of what data is available when sending a query to the server. In order to learn what data is available when making a request, the schema can be fetched from the server. Once the URL and authentication are provided, Postman will automatically fetch the schema from the server and use it in autocomplete to help when with writing queries. Otherwise, an Introspection Query can provide a list of queries accessible in our schema if this Autoauto-fetch feature is unavailable .
...
Code Block | ||
---|---|---|
| ||
query envState($where: [WhereInput], $sort: [SortInput], $page: PageInput) { reportEnvironmentState(where: $where, sort: $sort, page: $page) { items { endTime environmentName executionStatus externalTicket instanceName objectPath packageName partialDeployments projectName projectVersionName projectWorkflowType relName relSnapshot scmRevision startTime streamName workflowExecutionId workflowRequestId } } } |
We can see that we are getting information about running the reportEnvironmentState query (Environment State Report) in this query. This query has three variables (where, sort, and page) and is selecting quite a few fields in the “items” section to return when it executes.
...
Here is where we can control what is being returned in the query. GraphQL will only return fields present in the subselection. For example, If if I only wanted Package Name, Project Name, and Project Version Name to be returned, I could alter our previous example to something like this:
Code Block |
---|
query envState($where: [WhereInput], $sort: [SortInput], $page: PageInput) { reportEnvironmentState(where: $where, sort: $sort, page: $page) { items { packageName projectName projectVersionName } } } |
Response
Code Block |
---|
{
"data": {
"reportEnvironmentState": {
"items": [
{
"packageName": null,
"projectName": "RESTDeployProject1",
"projectVersionName": "1.0.30566"
},
{
"packageName": null,
"projectName": "RESTDeployProject1",
"projectVersionName": "1.0.30577"
},
]
}
}
} |
Variables
There are three main variables to use with FlexDeploy GraphQL queries: where, sort, and page.
...
Variable Name
...
Description
...
Object Definition
...
where: [WhereInput]
...
This allows you to filter the data like a where clause in an SQL query. Where is an array so multiple individual WhereInput objects can be linked together to filter the query.
field: GraphQL field being filtered
type: The comparison being preformed.
eq - equal
ne - not equal
eqi - equal (Ignores Case)
gt - greater than
lt - less than
inc - includes
inci - includes (Ignores Case)
ninc - does not include
empty - empty
nempty - not empty
innerWhere: Similar to a subselect in SQL where prefiltering of a query could be done
value: The value you are filtering by
...
Code Block | ||
---|---|---|
| ||
input WhereInput {
field: String!
type: WhereTypeEnum!
innerWhere: [WhereInput!]
value: String
}
enum WhereTypeEnum {
eq
ne
eqi
gt
lt
inc
inci
ninc
empty
nempty
} |
...
sort: [SortInput]
...
This allows you to sort the data like a order by clause in an SQL query. Sort is an array so multiple individual SortInput objects can be linked together to sort the query.
field: GraphQL field being sorted
direction: the direction of sort being preformed with asc meaing ascending order and desc meaning descending order.
Code Block | ||
---|---|---|
| ||
input SortInput {
field: String!
direction: SortEnum
}
enum SortEnum {
asc
desc
} |
...
page: PageInput
...
Page input contains extra options for the block of data that is returned by the query.
limit: The number of items in the block being returned. By default, a limit of 50 items are returned by the query. This can be overridden by the page limit to return a different amount of items.
offset: Offsets the block of data being returned by the set value. For example, a limit of 20 and an offset of 4 would return items 4 through 23.
Code Block | ||
---|---|---|
| ||
input PageInput {
limit: Int
offset: Int
} |
...
Variables
There are three main variables to use with FlexDeploy GraphQL queries: where, sort, and page.
Variable Name | Description | Object Definition | |||||
---|---|---|---|---|---|---|---|
where: [WhereInput] | This allows you to filter the data like a where clause in an SQL query. Where is an array so multiple individual WhereInput objects can be linked together to filter the query. field: Name of the GraphQL field being filtered type: The comparison being performed eq - equal ne - not equal eqi - equal (Ignores Case) gt - greater than lt - less than inc - includes inci - includes (Ignores Case) ninc - does not include empty - empty nempty - not empty innerWhere: Similar to a subselect in SQL where prefiltering of a query could be done value: The value you are filtering by |
| |||||
sort: [SortInput] | This allows you to sort the data like a order by clause in an SQL query. Sort is an array so multiple individual SortInput objects can be linked together to sort the query. field: GraphQL field being sorted direction: the direction of sort being preformed with asc meaing ascending order and desc meaning descending order. |
| |||||
page: PageInput | Page input contains extra options for the block of data that is returned by the query. limit: The number of items in the block being returned. By default, a limit of 50 items are returned by the query. This can be overridden by the page limit to return a different amount of items. offset: Offsets the block of data being returned by the set value. For example, a limit of 20 and an offset of 4 would return items 4 through 23. |
|
All of the above variables object above are then bundled together in one variable object. It is not required to always include all three variables in the object. For example, if you wanted to only filter and not sort, you would only need to include the where variable. Below is an example of the combined variable object with all three variables used.
Code Block |
---|
{
"sort": [
{
"field": "projectName",
"direction": "asc"
}
],
"where": [
{
"field": "environmentName",
"type": "eqi",
"value": "QA"
},
{
"field": "projectWorkflowType",
"type": "eq",
"value": "DEPLOY"
}
],
"page": {
"limit": 20
}
} |
Executing A Query
Combining everything we talked about this far (creating a HTTP Request, queries, and variables), we are now able to execute a query.
...
Once a query is configured inside your software of choice to make HTTP requests, just press the “Send” or “Execute” button to send out your HTTP Request.
Response
Code Block |
---|
{ "sort"data": { "reportEnvironmentState": { "items": [ { "fieldpackageName": "projectName",null, "directionprojectName": "ascRESTDeployProject1", } ], "whereprojectVersionName": ["1.0.30566" { }, "field": "environmentName", { "type": "eqi", "value": "QA" }"packageName": null, { "fieldprojectName": "projectWorkflowTypeRESTDeployProject1", "type": "eq", "projectVersionName": "1.0.30577" "value": "DEPLOY" } , ], "page": { ] "limit": 20 } } } |
...
Combing everything we talked about this far (creating a HTTP Request, queries, and variables), we are now able to execute a query.
...
Once a query is configured inside your software of choice to make HTTP requests, just press the “Send” or “Execute” button to send out your HTTP Request.
Example Queries
Here are are few more examples of GraphQL queries that you can use with FlexDeploy. See “Fetching Fetching Schema and Introspection” Introspection above for information on how to get our full schema.
...