GQLPT: Revolutionizing GraphQL Query Generation with AI
GQLPT is an innovative npm package that leverages the power of AI to generate GraphQL queries from plain text. This cutting-edge tool is designed to streamline the development process and make working with GraphQL more accessible and efficient for developers of all levels.
What is GQLPT?
GQLPT is a powerful npm package that allows developers to generate GraphQL queries using natural language. Instead of manually constructing complex queries, you can simply describe what data you need in plain English, and GQLPT will do the heavy lifting for you.
Key Features
-
AI-Powered Query Generation: GQLPT uses advanced AI models to interpret your plain text descriptions and generate accurate GraphQL queries.
-
Flexible Adapters: The package supports multiple AI services through its adapter system. Currently, it offers adapters for Anthropic and OpenAI.
-
Schema Introspection: GQLPT can connect directly to your GraphQL endpoint and introspect the schema, ensuring that generated queries are always up-to-date.
-
Generate and Send: With the generateAndSend method, GQLPT can not only generate queries but also send them directly to your GraphQL endpoint.
-
Bring Your Own AI: GQLPT operates on a "bring your own AI" model. You need to supply your own API key for either OpenAI or Anthropic to use their respective adapters.
-
Automatic Type Generation: GQLPT offers seamless type generation without requiring changes to your existing code, enhancing type safety in your projects.
GQLPT in Action
Here's a glimpse of the GQLPT playground in action:
We encourage you to try out the GQLPT playground at gqlpt.dev. It's a great way to experience the power of AI-generated GraphQL queries firsthand!
Getting Started with GQLPT
Let's walk through the process of setting up and using GQLPT in your project:
Installation
First, install GQLPT and the OpenAI adapter:
npm install gqlpt @gqlpt/adapter-openai
Basic Usage
Here's a simple example of how to use GQLPT:
import { AdapterOpenAI } from "@gqlpt/adapter-openai";
import { GQLPTClient } from "gqlpt";
const typeDefs = /* GraphQL */ `
type User {
id: ID!
name: String!
}
type Query {
user(id: ID!): User
}
`;
const client = new GQLPTClient({
typeDefs,
adapter: new AdapterOpenAI({
apiKey: process.env.OPENAI_API_KEY, // Make sure to provide your OpenAI API key
}),
});
async function main() {
await client.connect();
const query = "Find users by id 1";
const response = await client.generateQueryAndVariables(query);
console.log(response);
// This would log something like:
// {
// query: `
// query ($id: ID!) {
// user(id: $id) {
// id
// name
// }
// }
// `,
// variables: { id: "1" }
// }
}
main();
Note: Remember to supply your own OpenAI API key in the above example.
Using Introspection
GQLPT can fetch your schema directly from your GraphQL endpoint:
const client = new GQLPTClient({
url: "http://your-graphql-endpoint.com/graphql",
adapter: new AdapterOpenAI({ apiKey: process.env.OPENAI_API_KEY }),
});
await client.connect(); // Performs introspection
Generate and Send
For a complete end-to-end solution, use the generateAndSend method:
const response = await client.generateAndSend("Find users by id 1");
console.log(response);
// This would log something like:
// {
// data: {
// user: {
// id: "1",
// name: "John Doe"
// }
// }
// }
Automatic Type Generation
GQLPT offers seamless type generation without requiring changes to your existing code. This feature enhances type safety in your projects and improves the developer experience.
Installing the CLI Tool
To use the type generation feature, first install the GQLPT CLI tool:
npm install -g @gqlpt/cli
Generating Types
Run the following command in your project root to generate types:
npx @gqlpt/cli generate ./src
This command will:
- Scan your
./src
directory for GQLPT usage - Generate TypeScript types based on your plain text queries
- Update the types in
node_modules/gqlpt/build/types.d.ts
You don't need to manually import or reference these types in your code. GQLPT will automatically use them to provide type safety.
Example: Querying GitHub API with Automatic Type Safety
Here's an example of how to use GQLPT with the GitHub GraphQL API, leveraging automatically generated types:
import { AdapterOpenAI } from "@gqlpt/adapter-openai";
import { GQLPTClient } from "gqlpt";
const client = new GQLPTClient({
url: "https://api.github.com/graphql",
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
},
adapter: new AdapterOpenAI({
apiKey: process.env.OPENAI_API_KEY,
}),
});
async function searchGitHubRepos() {
await client.connect();
const query = "Find repositories with the name gqlpt";
const response = await client.generateAndSend(query);
console.log(response);
// This would log something like:
// {
// data: {
// search: {
// edges: [
// {
// node: {
// name: "gqlpt",
// description: "AI-powered GraphQL query generation",
// url: "https://github.com/example/gqlpt"
// }
// },
// // ... more results
// ]
// }
// }
// }
}
In this example, TypeScript provides full type safety and autocompletion when working with the response, based on the automatically generated types.
Real-World Benefits
- Faster Development: Spend less time writing complex queries and more time building features.
- Improved Accessibility: Make GraphQL more accessible to team members who might not be GraphQL experts.
- Reduced Errors: AI-generated queries can help reduce syntax errors and improve query accuracy.
- Flexibility: Easily switch between different AI providers using the adapter system.
- Enhanced Type Safety: Automatic type generation improves code quality and developer experience.
Community and Support
We value community involvement and contributions! Here's how you can get involved:
- GitHub Repository: Star our repository to show your support and stay updated with the latest developments.
- Create Issues: Have ideas for improvements or found a bug? Create an issue and let us know!
- Contribute: We welcome contributions! Check out our contributing guidelines to get started.
- GQLPT Playground: Try out GQLPT and provide feedback.
- npm Package: Check out our npm page for the latest versions and documentation.
Conclusion
GQLPT represents a significant step forward in making GraphQL more accessible and efficient. By bridging the gap between natural language and GraphQL queries, it opens up new possibilities for rapid development and improved team collaboration. The inclusion of automatic type generation further enhances its capabilities, providing a more robust and type-safe development experience.
Whether you're a GraphQL veteran looking to speed up your workflow or a newcomer seeking an easier entry point, GQLPT offers a powerful solution. Give it a try and experience the future of GraphQL query generation today!
Remember, GQLPT is a community-driven project. We encourage you to star our repository, submit issues if you have ideas or find bugs, and contribute to make GQLPT even better. Your involvement helps shape the future of GraphQL development!