Leverage AI to generate GraphQL queries from plain text.
Read more about GQLPT on our blog rconnect.tech/blog/gqlpt
Image showing the online playground for gqlpt.dev. GQLPT is a npm package that allows you to generate GraphQL queries from plain text using AI.
- Core library for generating GraphQL queries from plain text. GitHub
- TypeScript type generation for GQLPT. GitHub
- Adapter for Anthropic AI. GitHub
- Adapter for OpenAI GPT-3. GitHub
https://www.npmjs.com/package/gqlpt
npm install gqlpt @gqlpt/adapter-openai
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,
}),
});
async function main() {
await client.connect();
const query = "Find users by id 1";
const response = await client.generateQueryAndVariables(query);
console.log(response);
/*
{
query: 'query ($id: ID!) {\n user(id: $id) {\n id\n name\n }\n}',
variables: { id: '1' }
}
*/
}
main();
You can specify a URL and headers in the options to perform introspection on the .connect
command. This allows you to fetch the GraphQL schema directly from your endpoint.
Note: When specifying a URL for initial introspection, you must call the
connect
method.
const client = new GQLPTClient({
url: "http://localhost:4000/graphql", // Your GraphQL endpoint
headers: {
Authorization: `Bearer ${process.env.ACCESS_TOKEN}`,
},
adapter: new AdapterOpenAI({
apiKey: process.env.OPENAI_API_KEY,
}),
});
await client.connect(); // Performs introspection using the URL
The GQLPTClient
class includes a generateAndSend
method that simplifies generating a GraphQL query from plain text and sending it directly to a specified endpoint. This method leverages the generateQueryAndVariables
function and then posts the generated query to the endpoint.
import { AdapterOpenAI } from "@gqlpt/adapter-openai";
import { GQLPTClient } from "gqlpt";
const client = new GQLPTClient({
url: "http://localhost:4000/graphql",
adapter: new AdapterOpenAI({
apiKey: process.env.OPENAI_API_KEY,
}),
});
async function main() {
await client.connect();
const response = await client.generateAndSend("Find users by id 1");
console.log(response); // Logs the server's response to the query
/*
Example response structure:
{
"data": {
"user": {
"id": "1",
"name": "John Doe"
}
},
"errors": []
}
*/
}
main();
You can override the URL or headers when calling generateAndSend
:
const response = await client.generateAndSend("Find users by id 1", {
urlOverride: "http://another-server.com/graphql",
headersOverride: {
Authorization: "Bearer custom-token",
},
});
GQLPT offers seamless type generation without requiring changes to your existing code. The CLI tool automatically analyzes your codebase, generates appropriate types based on your usage patterns, and updates the type definitions in your node_modules
.
npm install -g @gqlpt/cli
Run the following command in your project root:
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.
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);
// response will be typed based on the generated types
}
In this example:
-
We create a
GQLPTClient
instance, specifying the GitHub GraphQL API endpoint and including an authorization header with a GitHub token. -
We don't need to explicitly import or specify any type information. The types are automatically applied based on the generated definitions.
-
After connecting to the client (which performs introspection on the GitHub API), we use a natural language query to search for repositories with "gqlpt" in the name.
-
The
generateAndSend
method generates the appropriate GraphQL query, sends it to the GitHub API, and returns the result with full type information. -
TypeScript provides full type safety and autocompletion when working with the response, based on the automatically generated types.
This seamless integration of type generation allows you to leverage the power of TypeScript's type system without any additional overhead in your development process. Just run the CLI tool whenever you update your GraphQL schema or change your GQLPT usage patterns, and enjoy automatic type safety in your code.
- I'm seeing the error "429 - Rate limit reached for requests" on making requests.
This error is related to sending too many requests to OpenAI, and them trying to rate-limit you. Try limiting the requests to gqlpt, or upgrading to a paid OpenAI API plan. In case of persistent errors, create an issue with us, or reach out to OpenAI support for OpenAI related errors.
MIT - Rocket Connect - https://github.com/rocket-connect