First Query Understand how a GraphQL selection maps to nested database results. start guide start/first-query start/first-query.md

First Query

Understand how a GraphQL selection maps to nested database results.

Shape the response

GraphJin treats the database schema as the source of truth. A table becomes a root field, columns become fields, and foreign-key relationships become nested selections.

GraphQL
query {
  products(id: $id) {
    id
    name
    owner {
      email
    }
  }
}
JSON
{ "id": 2 }

When you query by primary key, GraphJin returns a single object. When you query a collection, it returns a list.

Verified by Example_queryByID tests/query_test.go:558

Primary-key lookup works because GraphJin has discovered the table primary key from the database schema. For composite keys or manually mapped tables, keep table metadata explicit in config so the compiler can still distinguish single-row lookup from collection lookup.

Use aliases

GraphQL aliases let you expose API-friendly names without changing the database:

GraphQL
query {
  users {
    id
    fullName: full_name
  }
}
Verified by Example_queryWithAlternateFieldNames tests/query_test.go:532

Add variables

Variables replace scalar values, not arbitrary GraphQL fragments:

GraphQL
query Products($ownerID: ID!, $limit: Int = 10) {
  products(
    where: { owner_id: { eq: $ownerID } }
    limit: $limit
    order_by: { id: asc }
  ) {
    id
    name
  }
}
Verified by Example_queryWithVariablesDefaultValue tests/query_test.go:942

Keep order explicit

SQL result order is undefined without order_by. Tests and production clients should ask for deterministic ordering whenever order matters.

GraphQL
query {
  products(limit: 5, order_by: { id: asc }) {
    id
    name
  }
}

This rule matters across dialects. A result that appears stable on PostgreSQL without order_by is still relying on undefined SQL ordering.

Docs