This repository demonstrates the usage of GQLPT (GraphQL Plain Text) to query a GraphQL API using natural language. It uses the GitHub GraphQL API as an example.
-
Clone this repository:
git clone https://github.com/danstarns/talk-to-graphql cd talk-to-graphql
-
Install dependencies:
npm install
-
Create a
.env
file in the root directory with the following content:GQLPT_URL="https://api.github.com/graphql" GQLPT_HEADERS='{"Authorization": "Bearer YOUR_GITHUB_PERSONAL_ACCESS_TOKEN"}' OPENAI_API_KEY="YOUR_OPENAI_API_KEY" GQLPT_ADAPTER="openai"
Replace
YOUR_GITHUB_PERSONAL_ACCESS_TOKEN
with your actual GitHub Personal Access Token andYOUR_OPENAI_API_KEY
with your OpenAI API key.
-
Generate types:
npm run generate
This command will generate TypeScript types based on the GraphQL schema.
-
Run the application:
npm run start
This will execute the main script, which demonstrates querying the GitHub GraphQL API using plain text.
The main script (index.ts
) does the following:
- Sets up a GQLPT client using the GitHub GraphQL API URL and your OpenAI API key.
- Defines two functions:
getLatestGraphQLIssue()
: Retrieves the latest issue from the graphql/graphql-js repository.getGraphQLStarCount()
: Fetches the star count of the graphql/graphql-js repository.
- In the
main()
function, it calls both of these functions and logs the results.
Feel free to modify the plain text queries in the getLatestGraphQLIssue()
and getGraphQLStarCount()
functions to experiment with different queries to the GitHub GraphQL API.
If you encounter any issues:
- Ensure all environment variables in the
.env
file are set correctly. - Check that your GitHub Personal Access Token has the necessary permissions.
- Verify that your OpenAI API key is valid and has sufficient credits.
For any other problems, please open an issue in this repository.