Single Responsibility Principle for GraphQL API Resolvers

The Single Responsibility Principle (SRP) states that a class or module should have only one reason to change. It emphasizes the importance of keeping modules or components focused on a single task, reducing their complexity, and increasing maintainability.

SRP defined.

In GraphQL API development the importance, and need, of maintaining code quality and scalability is of utmost importance. A powerful principle that can help achieve these goals when developing your API’s resolvers is the Single Responsibility Principle (SRP). I’m not always a die hard when it comes to SRP – there are always situations that may need to skip out on some of the concept – but in general it helps tremendously over time.

By adhering the SRP coders can more easily avoid the pitfalls of large monolithic resolvers that end up doing spurious processing outside of their intended scope. Let’s explore some of the SRP and I’ll provide three practice examples of how to implement some simple SRP use with GraphQL.

When applying SRP in GraphQL the aim is to ensure that each resolver handles a specific data type or field, thereby avoiding scope bloat and convoluted resolvers that handle unrelated responsibilities.

  1. User Resolvers:
    • Imagine a scenario where a GraphQL schema includes a User type with fields like id, name, email, and posts. Instead of writing a single resolver for the User type that fetches and processes all of the data we can adopt SRP by creating separate resolvers for each field. For instance we would have resolvers.getUserById to fetch user details, resolvers.getUserName to retrieve the respective user’s name, and a resolvers.getUserPosts to fetch the user’s posts. In doing so we keep each resolver focused on a specific field and in turn keep the codebase simplified.
  2. Product Resolvers:
    • Another example might be a product object within an e-commerce application. It would contain fields like is, name, price, and reviews. With SRP we’d have resolvers for resolvers.getProductById, resolvers.getProductName, resolvers.getProductPrice, and resolvers.getProductReviews. The naming, by use of SRP, can be utilized to describe what each of these functions do and what one can expect in response. This again, makes the codebase dramatically easier to maintain over time.
  3. Comment Resolvers:
    • Last example, imagine a blog, with a comment type consisting of id, content, and author. This would break out to resolvers.getCommentContent, resolvers.getCommentAuthor, and resolvers.getCommentById. This adheres to SRP and keeps things simple, just like the previous two examples.

Prerequisite: The examples below assume the apollo-server and graphql are installed and available.

User Resolvers Example

A more thorough working example of the user resolvers described above would look something like this. I’ve included the data in a variable to act as the database, but the inferred premise there would be an underlying database should be obvious.

// Assuming you have a database or data source that provides user information
const db = {
  users: [
    { id: 1, name: "John Doe", email: "john@example.com", posts: [1, 2] },
    { id: 2, name: "Jane Smith", email: "jane@example.com", posts: [3] },
  ],
  posts: [
    { id: 1, title: "GraphQL Basics", content: "Introduction to GraphQL" },
    { id: 2, title: "GraphQL Advanced", content: "Advanced GraphQL techniques" },
    { id: 3, title: "GraphQL Best Practices", content: "Tips for GraphQL development" },
  ],
};

const resolvers = {
  Query: {
    getUserById: (_, { id }) => {
      return db.users.find((user) => user.id === id);
    },
  },
  User: {
    name: (user) => {
      return user.name;
    },
    posts: (user) => {
      return db.posts.filter((post) => user.posts.includes(post.id));
    },
  },
};

// Assuming you have a GraphQL server setup with Apollo Server
const { ApolloServer, gql } = require("apollo-server");

const typeDefs = gql`
  type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post!]!
  }

  type Post {
    id: ID!
    title: String!
    content: String!
  }

  type Query {
    getUserById(id: ID!): User
  }
`;

const server = new ApolloServer({ typeDefs, resolvers });

server.listen().then(({ url }) => {
  console.log(`Server running at ${url}`);
});

In this code we have the db object acting as our database that we’ll interact with. Then the resolvers and the GraphQL schema is included inline to show the relationship of the data and how it would love per GraphQL. For this example I’m also, for simplicity, using Apollo server to build this example.

Product Resolvers Example

I’ve also included an example of the product resolvers. Very similar, but has some minor nuance to show how it would coded up. For this example however, to draw more context I’ve added an authors table/entity, and respective fields for authors, as per related to reviews.

// Assuming you have a database or data source that provides product, review, and author information
const db = {
  products: [
    { id: 1, name: "Product 1", price: 19.99, reviews: [1, 2] },
    { id: 2, name: "Product 2", price: 29.99, reviews: [3] },
  ],
  reviews: [
    { id: 1, rating: 4, comment: "Great product!", authorId: 1 },
    { id: 2, rating: 5, comment: "Excellent quality!", authorId: 2 },
    { id: 3, rating: 3, comment: "Average product.", authorId: 1 },
  ],
  authors: [
    { id: 1, name: "John Doe", karmaPoints: 100, details: "Product enthusiast" },
    { id: 2, name: "Jane Smith", karmaPoints: 150, details: "Tech lover" },
  ],
};

const resolvers = {
  Query: {
    getProductById: (_, { id }) => {
      return db.products.find((product) => product.id === id);
    },
  },
  Product: {
    name: (product) => {
      return product.name;
    },
    price: (product) => {
      return product.price;
    },
    reviews: (product) => {
      return db.reviews.filter((review) => product.reviews.includes(review.id));
    },
  },
  Review: {
    author: (review) => {
      return db.authors.find((author) => author.id === review.authorId);
    },
  },
};

// Assuming you have a GraphQL server setup with Apollo Server
const { ApolloServer, gql } = require("apollo-server");

const typeDefs = gql`
  type Product {
    id: ID!
    name: String!
    price: Float!
    reviews: [Review!]!
  }

  type Review {
    id: ID!
    rating: Int!
    comment: String!
    author: Author!
  }

  type Author {
    id: ID!
    name: String!
    karmaPoints: Int!
    details: String!
  }

  type Query {
    getProductById(id: ID!): Product
  }
`;

const server = new ApolloServer({ typeDefs, resolvers });

server.listen().then(({ url }) => {
  console.log(`Server running at ${url}`);
});

Comment Resolvers Example

Starting right off with this example, here’s what the code would look like.

// Assuming you have a database or data source that provides comment and author information
const db = {
  comments: [
    { id: 1, content: "Great post!", authorId: 1 },
    { id: 2, content: "Nice article!", authorId: 2 },
  ],
  authors: [
    { id: 1, name: "John Doe", karmaPoints: 100, details: "Product enthusiast" },
    { id: 2, name: "Jane Smith", karmaPoints: 150, details: "Tech lover" },
  ],
};

const resolvers = {
  Query: {
    getCommentById: (_, { id }) => {
      return db.comments.find((comment) => comment.id === id);
    },
  },
  Comment: {
    content: (comment) => {
      return comment.content;
    },
    author: (comment) => {
      return db.authors.find((author) => author.id === comment.authorId);
    },
  },
};

// Assuming you have a GraphQL server setup with Apollo Server
const { ApolloServer, gql } = require("apollo-server");

const typeDefs = gql`
  type Comment {
    id: ID!
    content: String!
    author: Author!
  }

  type Author {
    id: ID!
    name: String!
    karmaPoints: Int!
    details: String!
  }

  type Query {
    getCommentById(id: ID!): Comment
  }
`;

const server = new ApolloServer({ typeDefs, resolvers });

server.listen().then(({ url }) => {
  console.log(`Server running at ${url}`);
});

The concept of the Single Responsibility Principle (SRP) and provided code examples using JavaScript demonstrate its application in GraphQL resolvers. The SRP advocates for keeping code modules focused on a specific data type or field, avoiding large and monolithic resolvers that handle multiple unrelated responsibilities. By adhering to the SRP, software developers can build better software that is modular, maintainable, and easier to understand. By dividing functionality into smaller, well-defined units, developers can enhance code reusability, improve testability, and promote better collaboration among team members. Embracing the SRP helps create codebases that are more scalable, extensible, and adaptable to changing requirements, ultimately leading to higher-quality software solutions.

GraphQL Error Handling

The following post is based on some of the common error handling techniques I’ve seen in use when implementing GraphQL APIs. The following examples include;

  1. Objects in the Response.
  2. Union Types.
  3. Middleware
  4. Custom Error Types
  5. Extensions
  6. Bubbling & Partial Results

To elaborate, a basic definition of each of these follows with a slightly deeper dive into the details of each example.

Error Objects in the Response

GraphQL allows you to define an error object structure within the response payload. When an error occurs during the execution of a GraphQL query, you can include relevant error information such as error codes, messages, and additional data in the response. This approach ensures that clients receive detailed error information and can handle errors appropriately.

It’s important to note that the approaches to error handling in GraphQL can vary depending on the specific GraphQL implementation or framework being used. These approaches are not mutually exclusive and can be combined to fit the needs of a particular application or organization. In JavaScript, an example of an error object in a GraphQL response might look like this:

{
  "data": null,
  "errors": [
    {
      "message": "Invalid argument value",
      "locations": [
        {
          "line": 3,
          "column": 7
        }
      ],
      "path": [
        "user",
        "name"
      ],
      "extensions": {
        "code": "INVALID_ARGUMENT",
        "details": {
          "minLength": 5,
          "maxLength": 20
        }
      }
    }
  ]
}

In this example, the response object has a data field set to null indicating that there was an error during the execution of the query. The errors field is an array containing an object representing the specific error that occurred.The error object includes the following properties:

  • message: A human-readable error message describing the issue.
  • locations: An array indicating the location of the error within the GraphQL query. Each location object contains the line and column where the error occurred.
  • path: An array representing the field path that caused the error. It helps identify the specific field that generated the error.
  • extensions: An optional field that can include additional information about the error. In this example, it includes the code field with a custom error code (INVALID_ARGUMENT) and a details object with specific details related to the error.

Please note that the structure and specific properties of error objects can vary depending on the GraphQL server implementation or framework being used. The example provided above showcases a common structure used to convey error information in a GraphQL response.

Union Types for Errors

GraphQL supports union types, which allow you to define a type that can represent multiple possible types. You can leverage this feature to create a union type that includes both successful responses and error responses. By defining such a type, clients can anticipate and handle errors as part of the normal response flow.

In GraphQL, a union type allows you to define a type that can represent multiple possible types. It’s a way to indicate that a field in a response can have different types of values. This concept is useful for error handling when you want to include both successful responses and error responses in the same field.To create a union type for errors, you can define a new GraphQL type that represents an error and include it as one of the possible types within the union type. This allows the field to return either a successful response or an error response, depending on the situation.Here’s an example to illustrate this concept further:

union ResponseType = SuccessResponse | ErrorResponse

type SuccessResponse {
  data: String
}

type ErrorResponse {
  error: String
}

In this example, the ResponseType is a union type that can represent either a SuccessResponse or an ErrorResponse. The SuccessResponse type has a data field that holds the successful response data, while the ErrorResponse type has an error field that contains the error message. Now, let’s say you make a GraphQL query and receive a response in JavaScript using this union type:

{
  "data": {
    "responseField": {
      "__typename": "SuccessResponse",
      "data": "Some data"
    }
  }
}

In this example response, the responseField returns a SuccessResponse object. The __typename field indicates the specific type of the returned value. Here, it is set to "SuccessResponse". Along with the data field containing the successful response data.Now, let’s consider an example where an error occurs:

{
  "data": {
    "responseField": {
      "__typename": "ErrorResponse",
      "error": "An error occurred"
    }
  }
}

In this case, the responseField returns an ErrorResponse object. The __typename field is set to "ErrorResponse", and the error field contains the error message. By utilizing a union type for errors, the client can anticipate the possible response types and handle both successful responses and error responses accordingly. It provides a unified way to structure and handle different types of responses within the same field.

Error Middleware

Middleware functions can be used in GraphQL servers to intercept and handle errors before they reach the resolver functions. Error middleware can perform tasks such as logging errors, transforming error messages, or enriching error data. It provides a centralized way to handle errors and can be customized based on specific application requirements.

Error middleware in the context of GraphQL refers to a mechanism where middleware functions are used to intercept and handle errors before they reach the resolver functions. It allows you to centralize error handling logic and perform tasks such as logging errors, transforming error messages, or enriching error data. Error middleware sits between the incoming request and the execution of the resolver functions, providing an opportunity to handle errors at a higher level.

In JavaScript, when implementing error middleware for a GraphQL server, you can use middleware functions provided by frameworks such as Express or Apollo Server. These middleware functions are executed in the order they are registered, allowing you to define custom error handling logic.Here’s an example of how error middleware could be implemented in JavaScript using Express:

const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

// Define your GraphQL schema
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Define your resolvers
const resolvers = {
  Query: {
    hello: () => {
      throw new Error('Something went wrong!');
    },
  },
};

// Create an ApolloServer instance
const server = new ApolloServer({ typeDefs, resolvers });

// Create an Express application
const app = express();

// Register error middleware
app.use((err, req, res, next) => {
  // Handle the error and send a custom error response
  res.status(500).json({ message: 'Internal Server Error' });
});

// Apply the Apollo Server middleware to the Express app
server.applyMiddleware({ app });

// Start the server
app.listen({ port: 4000 }, () => {
  console.log(`Server running at http://localhost:4000${server.graphqlPath}`);
});

In this example, we define a simple GraphQL schema with a single hello query that always throws an error. The error middleware function is registered using app.use() in Express. It takes four parameters: err, req, res, and next. When an error occurs during the execution of a resolver, the error middleware is invoked with the error object (err), the request object (req), the response object (res), and the next function.Inside the error middleware function, you can handle the error as per your requirements. In this example, we simply send a custom error response with a status code of 500 and a JSON payload containing an error message. By using error middleware, you can implement custom error handling logic, such as logging errors to a central system, translating error messages based on the client’s preferred language, or modifying the error response structure. This approach helps centralize error handling and keeps the resolver functions focused on their core responsibilities.

Custom Error Types

GraphQL allows you to define custom scalar types, object types, and enum types. Similarly, you can define custom error types that encapsulate specific error scenarios in your application. By utilizing custom error types, you can provide more structured error responses, including standardized fields like error codes, error messages, and additional metadata.

Custom error types in GraphQL refer to defining your own error-specific types that encapsulate specific error scenarios in your application. By creating custom error types, you can provide more structured error responses, including standardized fields like error codes, error messages, and additional metadata.To define a custom error type in GraphQL, you can extend the built-in Error type or create a new object type specifically for handling errors. By extending the Error type, you inherit its fields and can add custom fields and metadata specific to your application’s error handling needs. Here is an example to illustrate the concept of custom error types in GraphQL:

type CustomError implements Error {
  code: String!
  message: String!
  additionalData: JSON
}

type Query {
  getUser(id: ID!): User
}

type User {
  id: ID!
  name: String!
}

In this example, we define a custom error type called CustomError, which implements the built-in Error interface. The CustomError type includes fields such as code, message, and additionalData. These fields provide standardized information about the error, such as an error code, an error message, and any additional data that might be relevant to the error.Now, let’s consider an example of implementing a custom error type in JavaScript using a resolver function:

const { ApolloServer, gql, ApolloError } = require('apollo-server');

// Define your GraphQL schema
const typeDefs = gql`
  type Query {
    getUser(id: ID!): User
  }

  type User {
    id: ID!
    name: String!
  }
`;

// Define your resolvers
const resolvers = {
  Query: {
    getUser: (_, { id }) => {
      if (id !== '1') {
        throw new ApolloError('User not found', 'USER_NOT_FOUND', {
          invalidId: id,
        });
      }

      return { id: '1', name: 'John Doe' };
    },
  },
};

// Create an ApolloServer instance
const server = new ApolloServer({ typeDefs, resolvers });

// Start the server
server.listen().then(({ url }) => {
  console.log(`Server running at ${url}`);
});

In this example, we define a resolver for the getUser query. If the provided id is not '1', we throw an ApolloError with a custom error message and additional metadata (invalidId). The ApolloError is a pre-defined error class provided by Apollo Server that allows you to create custom errors.By throwing a custom error, we can leverage the error handling mechanisms in GraphQL and ensure that the client receives structured error responses. The client can then handle these errors based on the provided error code, message, and additional data. Using custom error types helps maintain consistency in error responses, allows for better error categorization, and provides a clear structure for conveying error information to clients consuming your GraphQL API.

Error Extensions

GraphQL allows extensions to be added to the response payload. You can leverage this feature to include additional information with error responses. For example, you can include debugging information, stack traces, or links to relevant documentation within the response extensions. This approach enhances the debugging experience and provides developers with valuable context when troubleshooting issues.

In the context of GraphQL, error extensions refer to a mechanism that allows you to include additional information or metadata with error responses. It extends the standard error response by providing a way to attach custom fields or data to the error object. Error extensions are particularly useful for enriching the error response with debugging information, stack traces, or links to relevant documentation.When an error occurs during the execution of a GraphQL query, you can include an extensions field within the error object to provide additional data specific to that error. This field can contain any JSON-serializable data, allowing you to customize the error response with relevant information for debugging or error handling purposes.Here’s an example to illustrate the concept of error extensions in GraphQL:

{
  "data": null,
  "errors": [
    {
      "message": "Invalid argument value",
      "locations": [
        {
          "line": 3,
          "column": 7
        }
      ],
      "path": [
        "user",
        "name"
      ],
      "extensions": {
        "code": "INVALID_ARGUMENT",
        "details": {
          "minLength": 5,
          "maxLength": 20
        }
      }
    }
  ]
}

In this example, the error response includes an extensions field within the error object. The extensions field contains custom data related to the error, such as an error code (code) and specific details (details) about the error, such as the minimum and maximum length allowed for the argument value.Now, let’s consider an example of implementing error extensions in JavaScript:

// Define your GraphQL schema
const typeDefs = gql`
  type Query {
    getUser(id: ID!): User
  }

  type User {
    id: ID!
    name: String!
  }
`;

// Define your resolvers
const resolvers = {
  Query: {
    getUser: (_, { id }) => {
      if (id !== '1') {
        const error = new Error('User not found');
        error.extensions = {
          code: 'USER_NOT_FOUND',
          invalidId: id,
        };
        throw error;
      }

      return { id: '1', name: 'John Doe' };
    },
  },
};

// Create an ApolloServer instance
const server = new ApolloServer({ typeDefs, resolvers });

// Start the server
server.listen().then(({ url }) => {
  console.log(`Server running at ${url}`);
});

In this example, within the resolver function for the getUser query, we create a custom error using the Error class. We then attach the error extensions by assigning a custom extensions object to the error.extensions property. In this case, the extensions include an error code (code) and the invalidId that caused the error. By utilizing error extensions, you can enrich the error response with custom fields or metadata that provides additional context to clients consuming your GraphQL API. Clients can access and utilize these extensions to enhance error handling, error logging, or for implementing specific error-related behaviors in their applications.

Error Bubbling and Partial Results

GraphQL supports error bubbling, which means that even if errors occur during the execution of a query, the server can continue executing the remaining parts of the query and return a partial result. This allows clients to receive as much data as possible while still being aware of the occurred errors. By leveraging this behavior, clients can handle partial results gracefully and make informed decisions based on the available data.

Error bubbling refers to the propagation of errors through the GraphQL resolver chain. When an error occurs in a resolver, it can be propagated up to higher-level resolvers or the root resolver. This allows for a hierarchical error handling approach, where errors can be caught and processed at different levels of the resolver hierarchy. By bubbling up errors, you can handle and modify the error response based on the specific context or requirements of each resolver.

Partial results, in the context of GraphQL, refer to the concept of returning a mixture of successfully resolved data and errors in a single response. When executing a GraphQL query, if an error occurs during the resolution of a field, it doesn’t necessarily mean the entire query execution should fail. Partial results allow you to still return the successfully resolved data while indicating the presence of errors in the response. This enables clients to process and display the available data while being aware of any errors that occurred during the query execution.Here’s an example in JavaScript to demonstrate error bubbling and partial results in GraphQL:

// Define your GraphQL schema
const typeDefs = gql`
  type Query {
    user(id: ID!): User
  }

  type User {
    id: ID!
    name: String!
    email: String!
    posts: [Post]
  }

  type Post {
    id: ID!
    title: String!
    content: String!
  }
`;

// Define your resolvers
const resolvers = {
  Query: {
    user: (_, { id }) => {
      if (id !== '1') {
        throw new Error('User not found');
      }

      return {
        id: '1',
        name: 'John Doe',
        email: 'john@example.com',
        posts: [
          { id: '1', title: 'First Post', content: 'This is the first post' },
          { id: '2', title: 'Second Post', content: 'This is the second post' },
        ],
      };
    },
  },
  User: {
    posts: (user) => {
      if (user.id !== '1') {
        throw new Error('User ID not found');
      }

      return user.posts;
    },
  },
};

// Create an ApolloServer instance
const server = new ApolloServer({ typeDefs, resolvers });

// Start the server
server.listen().then(({ url }) => {
  console.log(`Server running at ${url}`);
});

In this example, we have a GraphQL schema with a user query that retrieves user information and their associated posts. The user resolver throws an error if the provided id is not '1'. Similarly, the posts resolver for the User type throws an error if the user ID is not '1'. When executing a query like this:

query {
  user(id: "1") {
    id
    name
    email
    posts {
      id
      title
      content
    }
  }
}

The user resolver executes successfully and returns the user data along with the associated posts. However, if the id provided is not '1', an error is thrown and propagated up the resolver chain. The error is then included in the response, indicating the specific error that occurred during the resolution of the field.This demonstrates error bubbling, as the error from the inner resolver propagates up to the parent resolver and eventually to the root resolver. It allows for handling errors at different levels and providing a response that includes both successfully resolved data and error information.Partial results come into play when an error occurs during the resolution of a specific field. In the example, if the user ID is not found, the user resolver throws an error, but the response still contains the successfully resolved fields (id, name, and email), indicating a partial result. Clients can handle the available data while being aware of the error in the response.

A Hasura Quick Start with Remote Schema, Remote Joins

I’ve been building GraphQL APIs for a number of years now – of along side RESTful, gRPC, XML, and other API styles I won’t even bring up right now – and so far GraphQL APIs have been great to work with. The libraries in different languages form .NET’s Hot Chocolate, Go’s graphql-go, Apollo’s JavaScript based tooling and servers, to Java’s GraphQL for Spring have worked great.

Sometimes you’re in the fortunate situation where you’re using PostgreSQL or SQL Server, or other supported database for a tool like Hasura. Being able to get a full GraphQL (with REST options too) API running in seconds is pretty impressive. From a development perspective it is a massive boost. As Hasura adds more database connectors as they have with Snowflake and Amazon Athena, the server and tooling becomes even more powerful.

With that I wanted to show a N+1 demo where N is day 1 with Hasura. The idea is what do you do immediately after you get a sample service running with Hasura. How do you integrate it with other services, or more specifically how do you integrate your Hasura API along side APIs you’ve written yourself, such as an enterprise GraphQL for Spring based API running against Mongo or other data source? This repo is the basis for several demonstration repositories I am building that will show how you can setup – generally for local development – Hasura + X API with Y Language stack.

This is the Hasura quick start repository here, with migrations and metadata for a local setup. The first demonstration repo for a peripheral GraphQL API will be a Spring based API in this repository. The following steps will get the quick start repository up and running.

  1. Clone this repo git clone git@github.com:Adron/hasura-quick-start.git.
  2. From the root (where the docker-compose.yml file is located) execute docker compose up -d.
  3. Navigate into the hasura directory.
  4. Execute hasura metadata apply, then hasura migrate apply, and then hasura metadata apply. Just do it, it’s a strange workflow thing.
  5. Navigate now into the hasura directory and execute hasura console.

These steps are demonstrated in this video from 48 seconds.

What do you get once deployed?

The following are some of the core capabilities of Hasura and showcase what you can get up and running in a matter of seconds, even when you start from a completely empty database! First off you’ll find the database now has 3 tables along with their pertinent schema built out in PostgreSQL and available via Hasura, as shown here under the Data tab of the console.

I also created a schema diagram just to provide a visual of how these tables are designed.

For the remote schema, the Spring API, the following steps will get it cloned and running locally.

  1. Clone this repo git clone git@github.com:Adron/hasura-spring-boot-graphql.git.
  2. Execute ./gradlew build to get the jar file build. It will then be located in the build/libs directory of the project.
  3. Next build the docker image with docker build -t adron/hasura-spring-boot-graphql . to build the docker image locally.
  4. Now you can either start this container with docker compose up -d using the docker-compose.yml in the project or you can run the image with Docker specifically with docker run -p 8081:8080 adron/hasura-spring-boot-graphql.

For a walkthrough of getting the Spring API running, check out 2:28 onward in this video.

Now both of these instances are running locally and you can test each out respectively, but not specifically together. I’ll have probably write up another post on how to get services that spin up separately to run together for localized development. However, with the way things are setup in the two repos, it’s as if one team is the Hasura team building a GraphQL API and another is a Spring Java GraphQL API team, and they’re working autonomously of each other just based on contract of the APIs themselves.

Remote Schema

With that being the scenario, I’ve deployed the Spring API out remotely so that I could show how to put together a remote schema connection and then a remote join query, i.e. nested query in GraphQL speak, across these two APIs.

To add the remote schema, click on the remote schemas tab on the console. Add a name (1), then the URI (2), and optionally if needed add appropriate headers (3) or forward all headers from client requests.

Once that’s added, navigate to the relationships tab of the new remote schema and click on add. Then for this example, select remote database (1), then add a name (4) (Customer in the example) and then for type choose object (3) (per the example).

Then scroll down on that console screen and choose sales_data (1) and default, public, and users (2) under the reference database, schema, and table. Next up choose the source field (3) and reference column (4).

Once added it will look like this in the console.

This creates a relationship to be able to make nested queries against these sources with GraphQL. If it were a single contiguous database the schema would look like this. I’ve color coded the sales_data table as red, to signify it is the table we know is in another database (or, specifically, provided via another hosted API). However, as stated, in a single database the relationships would now look like this. The relationship however, isn’t in a database, but stored in the Hasura metadata between users and sales_data.

Now writing a query across this data would shape up like this. Because of the way the relationship was drawn via the remote schema, the path to get the nested object Customer (2) for the sales data is to start with the sales_data (1) entity. As shown.

sales_data {
  sales_number
  updated_at
  Customer {
    name
  }
}

Now we want to add more details about the particular customer like their email and details. To do this we’ll utilize another nesting level within this query that delves into relationships that are in the PostgreSQL database itself.

sales_data {
  sales_number
  updated_at
  Customer {
    name
    emails {
      email
    }
    details {
      details
    }
  }
}

With this the nested details email (3) and details (4) will be provided, which is foreign key relationships to the primary key table users in the underlying database, made available by Hasura’s relationships in metadata.

Boom! That’s it. Pretty easy setup if the databases and APIs have Hasura available to connect them in this way. Otherwise, this is a huge challenge to develop against if you’re just using solely a tech stack like Apollo, Spring Boot, or Hot Chocolate. Often something along federation and more complexities would come into play. But more on that later, I’ve got a piece coming on federation, stitching, remote schemas, and gateway – among various ways – to get multiple GraphQL, or GraphQL and RESTful APIs together into a singular, or singularly managed, API end point.

Hope that was useful, if you’ve got comments, questions, or curiosities let me know in the comments here, or pop over to the video and leave a comment there.

References:

The full video of setup and how the remote schema & joins work in Hasura.

Gradle Build Tool

A few helpful links and details to where information is on the Gradle Build Tool.

Installation

Via SDKMAN sdk install gradle x.y.z where x.y.z is the version, like 8.0.2.

Via Brew with brew install gradle.

Manually check out the instructions here.

Building a Java Library (or application, Gradle plugin, etc)

Using the init task. From inside a directory with the pertinent project.

gradle init

You’ll be prompted for options.

With the project initialized this is what that initialized folder structure looks like.

At this point add the Java code for the library, similar to this example, and execute a build like this.

./gradlew build

Build Collateral

View the test report via the HTML output file at lib/build/reports/tests/test/index.html.

The JAR file is available in lib/build/libs with the name lib.jar. Verify the archive is valid with jar tf lib/build/libs/lib.jar.

Add the version by setting the version = '0.1.1' in the build.gradle file.

Run the jar task ./gradlew jar and the build will create a lib/build/libs/lib-0.1.1.jar with the expected version.

Add all this to the build by adding the following to the build.gradle file:

tasks.named('jar') {
    manifest {
        attributes('Implementation-Title': project.name,
                   'Implementation-Version': project.version)
    }
}

Verifying this all works, execute a ./gradlew jar and then extract the MANIFEST.MF via jar xf lib/build/libs/lib-0.1.0.jar META-INF/MANIFEST.MF.

Adding API Docs

In the */Library.java file, replace the / in the comment by / * so that we get javadoc markup.

Run the ./gradlew javadoc task. The generated javadoc files are located at lib/build/docs/javadoc/index.html.

To add this as a build task, in build.gradle add a section with the following:

java {
    withJavadocJar()
}

Publish a Build Scan

Execute a build scan with ./gradlew build --scan.

Common Issues + Tips n’ Tricks

gradlew – Permission Denied issue

Let’s say you execute Gradle with ./gradlew with whatever parameter and immediately get a response of “Permission Denied”. The most common solution, especially for included gradlew executables included in repositories, is to just give the executable permission to execute. This is done with a simple addition chmod +x gradelw and you should now be ready to execute!

Interviewing AI. AI has thus answered the questions on AI in Software Development!

Me: Describe to me how AI is changing software development and the work developers do daily.

The AI: AI is transforming software development in various ways, ranging from automating repetitive tasks to enhancing software performance and user experience. Here are some specific ways that AI is changing software development and the work developers do daily:

  1. Automating mundane tasks: AI has enabled developers to automate various repetitive tasks that were previously done manually. For instance, using machine learning algorithms, AI can automate code review and testing, freeing up developers’ time to focus on more complex tasks.
  2. Enhancing software performance: With AI, developers can create more robust and efficient software. AI algorithms can analyze code and identify performance issues, potential bugs, and other areas that can be improved. This makes it easier for developers to optimize code and improve overall software performance.
  3. Improving user experience: AI can be used to enhance user experience in various ways. For instance, AI can be used to analyze user behavior and preferences, allowing developers to create more personalized and intuitive interfaces. AI can also be used to provide real-time feedback and recommendations to users, making software more engaging and interactive.
  4. Increasing productivity: AI-powered tools and frameworks can help developers be more productive by automating repetitive tasks, suggesting solutions to problems, and speeding up the development process.
  5. Facilitating collaboration: AI-powered tools can facilitate collaboration among developers by identifying areas where different developers can work together, highlighting common issues, and recommending solutions.

Overall, AI is transforming software development by making it more efficient, effective, and collaborative. As AI continues to evolve, developers will likely see even more changes in the way they work and the software they produce.

Me: What are ways that software developers can prepare for the changes AI is bringing to software development?

The AI: As AI continues to disrupt the software development industry, software developers need to prepare for the changes that are coming. Here are some ways that software developers can prepare for the changes AI is bringing to software development:

  1. Stay informed: Developers need to keep up with the latest trends and developments in AI and machine learning. This includes reading industry publications, attending conferences, and participating in online forums.
  2. Learn new skills: Developers should invest in learning new skills that will be valuable in an AI-driven future. This includes understanding machine learning algorithms, natural language processing, and data analysis techniques.
  3. Collaborate with AI experts: Developers can collaborate with AI experts to learn more about AI technologies and how they can be applied to software development. This can involve working on joint projects, attending meetups, and networking with AI experts.
  4. Embrace automation: Developers need to be open to automation and embrace tools that can automate repetitive tasks. This includes using AI-powered code review and testing tools, as well as automated build and deployment systems.
  5. Focus on the user: Developers should focus on creating software that delivers value to users. AI can be used to create more personalized and intuitive interfaces that enhance the user experience. By focusing on the user, developers can stay ahead of the curve and ensure that their software remains relevant.
  6. Develop a growth mindset: Lastly, developers should develop a growth mindset that enables them to embrace change and learn new skills. This includes being open to feedback, seeking out new challenges, and continuously improving their skills.

In summary, by staying informed, learning new skills, collaborating with AI experts, embracing automation, focusing on the user, and developing a growth mindset, software developers can prepare themselves for the changes AI is bringing to software development.

Me: What effects might AI cause in the developer job market with the reduction and automation of work that many software developers do today?

The AI: The increasing adoption of AI in software development is likely to have a significant impact on the job market for software developers. While AI will create new job opportunities in areas such as data science and machine learning, it may also lead to the reduction and automation of work that many software developers do today. Here are some potential effects:

  1. Automation of repetitive tasks: As AI-powered tools become more prevalent, developers may find that many of the tasks they previously performed manually are automated. This could include tasks such as testing, debugging, and code review. While this will free up developers’ time to focus on more complex tasks, it may also lead to a reduction in the number of jobs available.
  2. Shift in skill requirements: As AI becomes more prevalent, the skills required to be a successful software developer may change. Developers may need to have a greater understanding of machine learning algorithms, data analysis, and natural language processing. This may lead to a shift in the education and training required for software development.
  3. Emergence of new job roles: While some jobs may be automated, AI is also likely to create new job opportunities in areas such as data science, machine learning engineering, and AI architecture. These jobs may require a different set of skills and expertise than traditional software development roles.
  4. Increased competition: As the number of jobs available in software development potentially decreases, competition for the remaining jobs may increase. This may put pressure on developers to constantly update their skills and stay relevant in the job market.

Overall, the impact of AI on the job market for software developers is likely to be complex and multifaceted. While some jobs may be automated or reduced, new job opportunities may emerge. Developers who stay up-to-date with the latest trends and developments in AI and machine learning will be better positioned to take advantage of these new opportunities.

Me: Is there any evidence that there will be a net loss of jobs when AI becomes a larger part of software development?

The AI: There is some debate among experts about the impact of AI on jobs in the software development industry. While AI will certainly lead to the automation of some tasks, it may also create new job opportunities in areas such as data science, machine learning engineering, and AI architecture.

There have been several studies on the impact of AI on jobs in the broader economy, and the results have been mixed. Some studies suggest that AI will lead to significant job losses, while others suggest that it will create new job opportunities and lead to a net gain in jobs.

In the software development industry specifically, there is evidence that the impact of AI on jobs will depend on the specific tasks and skills involved. For example, a study by Deloitte found that while AI may automate some routine programming tasks, it is unlikely to replace developers altogether. Instead, AI is likely to shift the focus of developers towards more complex tasks that require human creativity and problem-solving skills.

Another study by the World Economic Forum found that AI is likely to create new job roles in software development, such as data analysts, machine learning engineers, and AI architects. However, the study also found that there may be some displacement of jobs, particularly for those with lower skill levels.

Overall, while it is difficult to predict the exact impact of AI on jobs in software development, it is likely that there will be both job losses and job gains. Developers who are able to adapt and acquire new skills in areas such as machine learning and data analysis are likely to be better positioned to take advantage of the new job opportunities created by AI.

Summary

Funny thing, and maybe it’s all a simulation right, but AI has given you basically the same exact answers I’d give you if asked these same questions. However, I’m writing an article to add more detail that AI (at least chatGPT) has not been able to provide. If you’ve used it, you’ve probably noticed, but there are distinctive limitations to the AI’s learning so far that just isn’t equitable to a well researched and well read human writing up a piece on a topic they understand well. It appears, so far, we’re not entirely replaceable yet!