GraphQL API Example Use Cases
CreateProject
You can use the CreateProject
mutation to create a new Zeet App and deploy your code or container, which you would otherwise create through the Create New -> App in the UI.
This guide walks through the steps to programatically setup and call this mutaiton using Node JS.
1. Installing GraphQL
First install GraphQL
in your local development environment.
npm add graphql-request graphql
2. Generate your Zeet API Key
You can generate an API key for Zeet by going to https://zeet.co/account/api. You can also find this by going to Team Settings → API Keys. Once you are here, create a new API Key which you can use in your program as shown below.
3. Import package and setup endpoint
You are now ready to start sending API requests using GraphQL to Zeet! Let’s import the package in our file and set up our endpoint and authorization for Zeet.
import { GraphQLClient, gql } from 'graphql-request'
async function main () {
// define your HTTP endpoint
const endpoint = "https://anchor.zeet.co/graphql";
// create new GraphQLClient object
const graphQLClient = new GraphQLClient (endpoint, {
headers : {
authorization : 'Bearer ' + API_KEY,
},
});
}
4. Craft your GQL Mutation
Now, let's actually craft the mutation request to be sent. The CreateProject
mutation takes in CreateProjectInput
as input and returns a Repo
object.
The following snippet demonstrates creating a Node Express App. Keep in mind that for this to work, the GitHub account that you are deploying from must already be connected to Zeet. Additionally, any mentions of an id
- including but not limited to id
, userId
, awsAccountID
, gcpAccountID
, and clusterID
refers to the Zeet ID of the entity unless otherwise specified.
const req = gql`
mutation {
createProject(input: {
userID: $user_id,
name: "gql-test"
owner: "zeet-demo",
repo: "node-express-demo",
environmentName: "production",
deployTarget: {
deployTarget: "AWS_SAM",
region: "us-east-2",
awsAccountID: $aws_id,
},
build: {
buildType: "NODE",
nodejsVersion: "14",
},
envs: [
{
name: "MY_API_KEY",
value: "1234567890",
sealed: false,
},
],
ports: [
{
port: "3000",
protocol: "tcp",
public: true,
https: true,
},
],
}) {
id
}
}
`
5. Send the request
Finally, send the request and wait for a response. In this case, as specified in the return section of the mutation, we are only asking for the id
of the new Repo
created. If successful, you should see this and a new projet in your Zeet dashboard!
// send the query as a request
const data = await graphQLClient.request(req);
// print the data received
console.log (JSON.stringify(data, undefined, 4));