# Getting started with the pyck GraphQL API

Audience: Programmer

Getting Started with the GraphQL API

## Introduction

This guide shows how to make your first query and understand the response format.

## Prerequisites

Before you begin, you'll need:

- A pyck account with API access
- Authentication credentials (JWT token or PAT)
- A GraphQL client or HTTP client

## Making your first query

This query returns the current user:

```graphql
query {
  me {
    UserID
    Username
    Email
  }
}
```

This query requests the user ID, username, and email of the authenticated user.

### Using curl

```bash
curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  --data '{"query": "query { me { UserID Username Email } }"}' \
  https://eu.pyck.cloud/graphql
```

### Using JavaScript

```javascript
async function fetchCurrentUser() {
  const response = await fetch('https://eu.pyck.cloud/graphql', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    },
    body: JSON.stringify({
      query: `
        query {
          me {
            UserID
            Username
            Email
          }
        }
      `
    })
  });

  const result = await response.json();
  return result.data.me;
}
```

## Understanding the response

GraphQL responses always follow the same structure:

```json
{
  "data": {
    "me": {
      "UserID": "0190a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a5b",
      "Username": "john.doe",
      "Email": "john.doe@example.com"
    }
  }
}
```

If there are errors, they'll be returned in an `errors` array:

```json
{
  "errors": [
    {
      "message": "You are not authenticated",
      "locations": [{ "line": 2, "column": 3 }],
      "path": ["me"]
    }
  ],
  "data": null
}
```

## Basic query examples

### Listing inventory items

```graphql
query {
  inventoryItems(first: 10) {
    edges {
      node {
        id
        sku
      }
    }
  }
}
```

### Creating an inventory item

```graphql
mutation {
  createInventoryItem(input: {
    sku: "TP-001"
    data: { weight: 1.5, color: "blue" }
  }) {
    inventoryItem {
      id
      sku
      createdAt
    }
  }
}
```

## Using variables

GraphQL allows you to use variables for dynamic values:

```graphql
query GetItem($id: ID!) {
  nodeInventory(id: $id) {
    ... on InventoryItem {
      id
      sku
    }
  }
}
```

With variables:

```json
{
  "id": "item-123"
}
```

## Next steps

After your first queries, you can:

1. Explore the [Schema Overview](https://docs.pyck.cloud/latest/api.md) to understand available operations
2. Learn about [Authentication](https://docs.pyck.cloud/latest/developers/authentication.md#sending-authenticated-requests) for secure API access
3. Read [Error Handling](https://docs.pyck.cloud/latest/developers/error-handling.md) to handle API errors
4. Read the documentation for specific queries and mutations

## Tools and resources

- GraphQL endpoint: `https://eu.pyck.cloud/graphql`
- [Apollo Client](https://www.apollographql.com/docs/react/): Popular GraphQL client for JavaScript
- [Insomnia](https://insomnia.rest/): API client with GraphQL support
- [Postman](https://learning.postman.com/docs/sending-requests/supported-api-frameworks/graphql/): API testing tool with GraphQL support
