avatarTech Wisdom

Summarize

Is AI writing medium articles? I asked OpenAI

I gave chatGPT a shot to help me write one and results are intriguing!

The first time heard of the term “Natural Language Processing” was back in 2008 while still pursuing my graduation. A friend of mine then introduced me to Wolfram Alpha which would go on to power Siri and Amazon’s alexa. Looking at this powerful computational engine, my first impression was “This will change the search”. It certainly did in some ways! Fast forward to 2022 and despite being absent from twitter, I couldn't escape the fuss around Open AI, specifically its latest offering ChatGPT.

So, I decided to check it out myself. “Will it soon be the new Ghost Writer for humans?”, “Will it replace mundane tasks done by humans or even more creative pursuits like writing code and even blog posts?”, “Can it be trained to be a mental health expert or a Counselor?”

If the above sound far fetched to any of the readers, I would highly encourage to give it a try. It has the potential to dazzle you even in this world of Siri and Alexa. Working with other tools in open AI, you can even generate art, so how hard would it be to write an article? Like Copilot, you can even use open AI to help you write code!

I have already encountered an application that integrates with ChatGPT to allow its users to create content for social networking websites. There is no question that it can easily create slide contents, tweets, Facebook or LinkedIn posts for you but I wanted to know how much it can help me write an article. I was planning on writing an article on GraphQL soon, So I thought I will ask ChatGPT for help and here’s our conversation (for those not familiar with API design and REST, I recommend reading the highlighted section of each answer and jump to summary — if you don’t want to read any further, still try ChatGPT yourself!):

Q: Can you prepare a writeup for graphql?

Response: Sure, I’d be happy to help! GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. It provides a more efficient, powerful and flexible alternative to REST and ad-hoc web service architectures.

Here is a brief overview of how GraphQL works:

  • A client makes a request to a GraphQL server for data using a specific query.
  • The server then responds with the requested data in a JSON format.
  • The client can specify exactly what data it needs in the query, so the server only needs to send the relevant data, reducing the amount of data sent over the network.

Some key benefits of using GraphQL include:

  • It allows clients to request only the data they need, making the client applications more efficient and performant.
  • It enables developers to evolve their APIs over time without breaking existing client applications.
  • It provides a consistent and predictable way to request and retrieve data from the server.

Overall, GraphQL is a powerful tool for building APIs that can provide great benefits in terms of performance, flexibility, and developer experience.

Q: How can I explain this to a layman?

Response: Here’s a simple way to explain GraphQL to a layman:

Imagine you’re at a restaurant and you want to order food. With a traditional restaurant, you have a fixed menu with limited options to choose from. If you want something that’s not on the menu, you can’t order it. This is like a traditional API, where the server has a fixed set of data that it can provide to the client, and the client has to work with whatever data is available.

Now imagine that the restaurant has a chef who can make anything you want, as long as you can describe it. This is like a GraphQL server. The client can make a specific request for the data it needs, and the server will provide it in the exact format requested. The client doesn’t have to work with a fixed set of data, and the server doesn’t have to send more data than necessary. This makes the whole process more efficient and flexible.

In short, GraphQL allows a client to request exactly the data it needs from a server, in the exact format it needs, making the client more efficient and the server more flexible.

Q: How is this different from a REST API?

Response: REST (Representational State Transfer) is a common architectural style for building web APIs. It is based on the idea of representing the state of an application as resources that can be manipulated using a fixed set of operations (e.g. GET, POST, PUT, DELETE).

GraphQL, on the other hand, is a query language for APIs that provides a more flexible and efficient alternative to REST. Some key differences between the two include:

  • In a REST API, the server provides a fixed set of data that the client can access, with each endpoint representing a specific resource or collection of resources. In a GraphQL API, the client can specify exactly what data it needs in a single query, and the server will return the data in the exact format requested.
  • In a REST API, the client has to make multiple requests to the server to retrieve all the data it needs, potentially leading to over-fetching or under-fetching of data. In a GraphQL API, the client can get all the data it needs in a single request, reducing the amount of data sent over the network and making the client more efficient.
  • In a REST API, the server and the client have a fixed contract that defines the data and operations available. If the server changes, it can break existing client applications. In a GraphQL API, the server can evolve over time without breaking existing client applications, as long as it maintains the underlying data schema.

Overall, GraphQL provides a more flexible and efficient way to request and retrieve data from an API, compared to REST.

Q: Where would I use one against the other?

Response: The choice between using REST or GraphQL for an API often depends on the specific requirements and constraints of the project. Here are some general guidelines for when to use each:

  • If you have a simple API with a limited number of resources and well-defined operations, and you don’t expect it to evolve significantly over time, REST may be a good choice. It is a well-established and widely-used architectural style, and there are many tools and libraries available to help you build and consume REST APIs.
  • If you have a complex API with a large number of resources and/or multiple ways to retrieve and manipulate those resources, and you need more control over the data that the client receives, GraphQL may be a better choice. It allows the client to specify exactly what data it needs in a single query, and the server can provide the data in the exact format requested. This can make the client more efficient and the server more flexible.
  • If you have an existing API built using REST, and it is working well for your needs, there may not be a strong reason to switch to GraphQL. However, if you find that your API is not meeting the needs of your clients, or if you are having trouble evolving the API over time without breaking existing clients, you may want to consider using GraphQL instead.

In general, it’s important to consider the specific requirements and constraints of your project when choosing between REST and GraphQL. There is no one-size-fits-all solution, and the right choice will depend on your specific needs and goals.

Q: Ok, Can you provide an example of a REST API being converted to Graphql?

Response (YES, IT WROTE ALL THIS CODE) : Sure, here is a simple example of converting a REST API to GraphQL.

Suppose you have a REST API that provides access to a list of users, and each user has an ID, name, and email address. The API has the following endpoints:

  • GET /users: Returns a list of all users.
  • GET /users/:id: Returns the user with the specified ID.
  • POST /users: Adds a new user.
  • PUT /users/:id: Updates the user with the specified ID.
  • DELETE /users/:id: Deletes the user with the specified ID.

Here is how you might convert this API to use GraphQL:

  1. Define the data schema for your users. This defines the structure of the data that your API will provide, and it serves as a contract between the server and the client. For example:
Copy codetype User {
  id: ID!
  name: String!
  email: String!
}

2. Define the root query type for your API. This defines the entry point for all GraphQL queries, and it specifies the fields that clients can query. For example:

Copy codetype Query {
  users: [User]
  user(id: ID!): User
}

3. Define the root mutation type for your API. This defines the entry point for all GraphQL mutations, and it specifies the fields that clients can mutate. For example:

type Mutation {
  addUser(name: String!, email: String!): User
  updateUser(id: ID!, name: String, email: String): User
  deleteUser(id: ID!): User
}

4. Implement the resolvers for your schema. This is the code that actually runs when a client makes a query or mutation, and it defines how the server will fulfill the request. For example:

const resolvers = {
  Query: {
    users: () => users,
    user: (_, args) => users.find(user => user.id === args.id),
  },
  Mutation: {
    addUser: (_, args) => {
      const user = { id: nextUserId++, name: args.name, email: args.email };
      users.push(user);
      return user;
    },
    updateUser: (_, args) => {
      const user = users.find(user => user.id === args.id);
      if (args.name) user.name = args.name;
      if (args.email) user.email = args.email;
      return user;
    },
    deleteUser: (_, args) => {
      const userIndex = users.findIndex(user => user.id === args.id);
      const deletedUser = users[userIndex];
      users.splice(userIndex, 1);
      return deletedUser;
    },
  },
};

5. Create a GraphQL server and pass it your schema and resolvers. This will allow clients to make queries and mutations against your API using GraphQL. For example:

const server = new GraphQLServer({ typeDefs, resolvers });
server.listen(PORT, () => {
  console.log(

Q: I want to use Java to build an API. How do I start?

Response: To use GraphQL in a Java project, you can download one of the many available Java libraries. Some popular options include graphql-java, graphql-spring-boot, and graphql-java-tools.

To download and install a library, you can follow these steps:

  1. Choose a library from the list above, or search for other Java libraries that support GraphQL.
  2. Check the library’s website for instructions on how to download and install it. Most libraries will provide a link to download the library as a JAR file, and they may also provide instructions on how to add it to your project’s dependencies.
  3. Download the JAR file and add it to your project’s classpath. This will allow your Java code to access the library’s classes and functions.
  4. Import the library’s classes and functions in your Java code, and use them to build your GraphQL API.

For example, if you are using the graphql-java library, you could add it to your project by following these steps:

  1. Download the graphql-java JAR file from the library's website: https://www.graphql-java.com/
  2. Add the JAR file to your project’s classpath.
  3. In your Java code, import the graphql-java classes and functions you want to use:
Copy codeimport graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import graphql.schema.StaticDataFetcher;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;

4. Use the imported classes and functions to build your GraphQL API. For example:

// parse the schema
TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(schema);
// create a runtime wiring
RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
  .type("Query", builder -> builder.dataFetcher("hello", new StaticDataFetcher("world")))
  .build();
// create the schema
GraphQLSchema schema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
// create the GraphQL instance
GraphQL graphQL = GraphQL.newGraphQL(schema).build();
// execute a query
ExecutionResult result = graphQL.execute("{ hello }");
// print the result
System.out.println(result.getData().toString());

This is just a simple example, and your specific code will depend on your needs and the features of the library you are using. You can find more detailed instructions and examples on the library’s website or in its documentation.

I then asked it to summarize our conversation and it did that too. If you have read till here, I guess you got my point. I even asked it to provide me a structure for my article and it just did that in seconds! ALL this took me less than 5 mins! I actually took longer to come up with questions.

All this shows how creativity and content creation is no longer a human monopoly. Is it perfect? No. Can it learn? YES! and that is a big yes. What’s most fascinating is its ability to understand and write in conversational manner, a more human way, if you will. By no means its the only such AI application out there and most of them aren’t even crawling web! So, the next time you read a fancy post on social media, you might want to ask yourself “Is it really author’s thoughts you are reading or a machine’s?” It may not be writing them but can sure help.

PS: Do check it for yourself at https://chat.openai.com/. It handles behavioral questions equally well.

OpenAI
ChatGPT
AI
GraphQL
Rest Api
Recommended from ReadMedium