Skip to content

Commit

Permalink
Add workspace field to GraphQL Dataset (#4318)
Browse files Browse the repository at this point in the history
* add minimal gql workspace definitions

* graphql workspace test

* lint imports

* lint import
  • Loading branch information
benjaminpkane authored Apr 29, 2024
1 parent f073ba4 commit bdfc97f
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 202 deletions.
398 changes: 215 additions & 183 deletions app/packages/relay/src/queries/__generated__/datasetQuery.graphql.ts

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions app/packages/relay/src/queries/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { graphql } from "relay-runtime";

export default graphql`
query datasetQuery(
$savedViewSlug: String
$extendedView: BSONArray!
$name: String!
$savedViewSlug: String
$view: BSONArray!
$extendedView: BSONArray!
$workspaceSlug: String
) {
config {
colorBy
Expand Down Expand Up @@ -71,6 +72,13 @@ export default graphql`
}
}
}
workspace(slug: $workspaceSlug) {
id
child
slug
}
...datasetFragment
}
...savedViewsFragment
Expand Down
16 changes: 15 additions & 1 deletion app/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ type Dataset {
frameIndexes: [Index!]
sampleIndexes: [Index!]
stages(slug: String = null, view: BSONArray = null): BSONArray
workspace(slug: String): Workspace
}

type DatasetAppConfig {
Expand Down Expand Up @@ -739,8 +740,8 @@ input SidebarGroupInput {

enum SidebarMode {
all
disabled
best
disabled
fast
}

Expand Down Expand Up @@ -833,6 +834,19 @@ type WildcardProjection {
inclusion: Boolean!
}

type Workspace {
id: ID!
color: String
child: BSON!
datasetId: ID!
createdAt: datetime
description: String
name: String
lastModifiedAt: datetime
lastLoadedAt: datetime
slug: String
}

scalar date

scalar datetime
15 changes: 15 additions & 0 deletions fiftyone/server/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from fiftyone.server.scalars import BSON, BSONArray, JSON
from fiftyone.server.stage_definitions import stage_definitions
from fiftyone.server.utils import from_dict
from fiftyone.server.workspace import Workspace


ID = gql.scalar(
Expand Down Expand Up @@ -291,6 +292,20 @@ async def estimated_frame_count(
self.frame_collection_name
].estimated_document_count()

@gql.field
async def workspace(
self, slug: t.Optional[str], info: Info
) -> t.Optional[Workspace]:
if slug:
doc = await info.context.db["workspaces"].find_one({"slug": slug})

if doc:
doc["id"] = doc.pop("_id")
doc["dataset_id"] = doc.pop("_dataset_id")
return from_dict(Workspace, doc)

return None

@staticmethod
def modifier(doc: dict) -> dict:
doc["id"] = doc.pop("_id")
Expand Down
29 changes: 29 additions & 0 deletions fiftyone/server/workspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
FiftyOne Server workspace.
| Copyright 2017-2024, Voxel51, Inc.
| `voxel51.com <https://voxel51.com/>`_
|
"""

from datetime import datetime
import typing as t

import strawberry as gql

from fiftyone.server.scalars import BSON


@gql.type
class Workspace:
id: gql.ID

color: t.Optional[str]
child: BSON
dataset_id: gql.ID
created_at: t.Optional[datetime]
description: t.Optional[str]
name: t.Optional[str]
last_modified_at: t.Optional[datetime]
last_loaded_at: t.Optional[datetime]
slug: t.Optional[str]
8 changes: 4 additions & 4 deletions tests/unittests/lightning_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import fiftyone as fo

from fiftyone.server.constants import SCALAR_OVERRIDES
from fiftyone.server.context import get_context
from fiftyone.server.lightning import (
lightning_resolver,
LightningInput,
LightningPathInput,
)

from decorators import drop_async_dataset
from utils.graphql import execute


@gql.type
Expand Down Expand Up @@ -711,17 +711,17 @@ def _add_samples(dataset: fo.Dataset, lower: t.Dict, upper: t.Dict):
async def _execute(
query: str, dataset: fo.Dataset, field: fo.Field, keys: t.Set[str]
):
return await schema.execute(
return await execute(
schema,
query,
variable_values={
{
"input": asdict(
LightningInput(
dataset=dataset.name,
paths=_get_paths(dataset, field, keys),
)
)
},
context_value=get_context(use_global_db_client=False),
)


Expand Down
17 changes: 5 additions & 12 deletions tests/unittests/server_aggregations_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
|
"""

import typing as t
import unittest

import strawberry as gql
Expand All @@ -15,11 +14,11 @@
import fiftyone as fo

from fiftyone.server.constants import SCALAR_OVERRIDES
from fiftyone.server.context import get_context
from fiftyone.server.aggregate import AggregateQuery
from fiftyone.server.aggregations import aggregate_resolver

from decorators import drop_async_dataset
from utils.graphql import execute


@gql.type
Expand Down Expand Up @@ -55,7 +54,8 @@ async def test_group_mode_sidebar_counts(self, dataset: fo.Dataset):
}
"""

result = await _execute(
result = await execute(
schema,
query,
{
"form": {
Expand Down Expand Up @@ -127,7 +127,8 @@ async def test_group_mode_histogram_counts(self, dataset: fo.Dataset):
}
"""

result = await _execute(
result = await execute(
schema,
query,
{
"dataset": dataset.name,
Expand Down Expand Up @@ -173,11 +174,3 @@ def _add_samples(dataset: fo.Dataset):
),
]
)


async def _execute(query: str, variables: t.Dict):
return await schema.execute(
query,
variable_values=variables,
context_value=get_context(use_global_db_client=False),
)
72 changes: 72 additions & 0 deletions tests/unittests/server_workspace_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
FiftyOne Server workspace tests.
| Copyright 2017-2024, Voxel51, Inc.
| `voxel51.com <https://voxel51.com/>`_
|
"""

import unittest

import strawberry as gql
from strawberry.schema.config import StrawberryConfig

import fiftyone as fo

from fiftyone.server.constants import SCALAR_OVERRIDES
from fiftyone.server.query import Dataset

from decorators import drop_async_dataset
from utils.graphql import execute


@gql.type
class DatasetQuery(Dataset):
dataset: Dataset = gql.field(resolver=Dataset.resolver)


schema = gql.Schema(
query=DatasetQuery,
scalar_overrides=SCALAR_OVERRIDES,
config=StrawberryConfig(auto_camel_case=False),
)


class TestGroupModeSidebarCounts(unittest.IsolatedAsyncioTestCase):
@drop_async_dataset
async def test_workspace_field(self, dataset: fo.Dataset):
histograms_panel = fo.Panel(type="Histograms")
name = "histograms"
workspace = fo.Space(children=[histograms_panel])
dataset.save_workspace(name, workspace)

query = """
query Query($name: String!, $slug: String!) {
dataset(name: $name) {
workspace(slug: $slug) {
child
name
slug
}
}
}
"""

result = await execute(
schema,
query,
{"name": dataset.name, "slug": name},
)

self.assertEqual(
result.data,
{
"dataset": {
"workspace": {
"child": workspace.to_dict(),
"name": "histograms",
"slug": name,
}
}
},
)
23 changes: 23 additions & 0 deletions tests/unittests/utils/graphql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import typing as t

import strawberry as gql

from fiftyone.server.context import get_context


async def execute(schema: gql.Schema, query: str, variables: t.Dict):
"""Execute a test GraphQL query.
Args:
schema: a :class:`strawberry.Schema`
query: a GraphQL query string
variables: a variables dictionary
Returns:
an :class:`strawberry.types.execution.ExecutionResult`
"""
return await schema.execute(
query,
variable_values=variables,
context_value=get_context(use_global_db_client=False),
)

0 comments on commit bdfc97f

Please sign in to comment.