Seamlessly Minimize Unnecessary Data Fetching with GraphQL Queries

How to use GraphQL with JFrog Artifactory to retrieve real-time and reliable metadata.

GraphQL and JFrog Artifactory

Many processes in our daily software development chain require data analysis in real-time. Ensuring the reliability of the data is vital, since multiple processes may rely on it. While taking into account the performance and fetching only the relevant data that is required for our specific use case.

What is GraphQL?

GraphQL is a query language and runtime for APIs. It allows clients to request specific data from servers in a flexible and efficient manner.

GraphQL is a powerful tool for developers because it simplifies how they interact with APIs and get the data they need to build applications. Unlike traditional RESTful APIs, which require multiple requests from the client side for retrieving related data, GraphQL enables clients to make one single request and receive all the related data in one response. This reduces network latency and speeds up development time since there is no need for additional requests.

Using the GraphQL Query Output

What can we do with the information returned from a custom query?

GraphQL queries allow us to retrieve metadata about packages hosted in JFrog Artifactory, using customizable queries. Compared to using JFrog APIs, GraphQL queries return only the information that is requested in the custom query. Common use cases include:

  • Retrieving the number of versions for a specific package and information about each version
  • Scanning for security vulnerabilities and their severity level for a specific package
  • Creating cleanup processes

Using GraphQL with JFrog Artifactory

GraphQL enhances how you retrieve data from Artifactory. Using GraphQL, users can send queries specifying the exact data structure they need, minimizing over-fetching or under-fetching of data while using a single endpoint for their requests and simplifying the API architecture.

GraphiQL

JFrog offers a simple version of GraphiQL which is a GraphQL IDE to test your GraphQL queries and can be used to learn the GraphQL metadata schema and as your playground while testing the queries and the capabilities of the GraphQL queries. It can be accessed by using the <JFrog Platform URL>/metadata/api/v1/query/graphiql endpoint.

GraphiQL Example query and output
GraphiQL Example query and output

GraphQL using cURL

GraphQL can be used with the /metadata/api/v1/query endpoint, and example of use with cURL client:

curl -H "Authorization: Bearer " -XPOST /metadata/api/v1/query -d '{"query":"..." }'

Note: To authenticate you will need to generate an Access Token both for GraphiQL and GraphQL.

GraphQL Example Query

The following example query will retrieve:

  1. Packages that contain a “hello” string in their name
  2. Package types: Pypi, NPM, Docker
  3. The first 4 results by name
  4. The package name, package type, description, created time, last modified time, version count, version name, and size

Query request:

curl -H "Authorization: Bearer " -XPOST /metadata/api/v1/query -d '{"query": "{ packages( filter: { name:  \"*hello*\", packageTypeIn: [PYPI, NPM, DOCKER]  },first: 4, orderBy: { field: NAME, direction: DESC   }) { edges { node { name packageType description created modified versionsCount versions{ name size }}}}}"}' | jq .

Sample Output:

{
  "data": {
    "packages": {
      "edges": [
        {
          "node": {
            "name": "hello-world",
            "packageType": "npm",
            "description": "my first teleport app.",
            "created": "2023-06-26T08:29:53.974Z",
            "modified": "2018-05-27T03:39:41Z",
            "versionsCount": 1,
            "versions": [
              {
                "name": "0.0.2",
                "size": "1198"
              }
            ]
          }
        },
        {
          "node": {
            "name": "hello-world",
            "packageType": "pypi",
            "description": "UNKNOWN",
            "created": "2023-07-03T12:31:34.309Z",
            "modified": "2023-04-11T05:47:41Z",
            "versionsCount": 2,
            "versions": [
              {
                "name": "0.2",
                "size": "665"
              },
              {
                "name": "0.1",
                "size": "645"
              }
            ]
          }
        },
        {
          "node": {
            "name": "hello-world",
            "packageType": "docker",
            "description": null,
            "created": "2022-07-13T13:51:45.111Z",
            "modified": "2023-03-20T13:25:32.754Z",
            "versionsCount": 1,
            "versions": [
              {
                "name": "latest",
                "size": "3602"
              }
            ]
          }
        }
      ]
    }
  }
}

That’s it, try it out for yourself!