Skip to content

Commit

Permalink
https://github.com/bonfire-networks/bonfire-app/issues/915
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Apr 23, 2024
1 parent bcc0c97 commit 6778116
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 119 deletions.
112 changes: 59 additions & 53 deletions lib/web/graphql_schema.ex → lib/web/graphql/graphql_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and

import_types(Absinthe.Type.Custom)

import_sdl(path: "lib/web/graphql/util.gql")

import_types(Bonfire.API.GraphQL.JSON)
import_types(Bonfire.API.GraphQL.Cursor)

Expand All @@ -69,8 +71,8 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and
import_types(Bonfire.Tag.GraphQL.TagSchema)
import_types(Bonfire.Classify.GraphQL.ClassifySchema)

# import_types(Bonfire.Quantify.Units.GraphQL)
# import_types(Bonfire.Geolocate.GraphQL)
import_types(Bonfire.Quantify.Units.GraphQL)
import_types(Bonfire.Geolocate.GraphQL)

# import_types(Bonfire.ValueFlows.API.Schema)
# import_types(Bonfire.ValueFlows.API.Schema.Subscriptions)
Expand All @@ -95,9 +97,8 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and

# import_fields(:locales_queries)

# import_fields(:measurement_query)

# import_fields(:geolocation_query)
import_fields(:measurement_query)
import_fields(:geolocation_query)

# ValueFlows
# import_fields(:value_flows_query)
Expand All @@ -118,8 +119,8 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and
import_fields(:tag_mutations)
import_fields(:classify_mutations)

# import_fields(:geolocation_mutation)
# import_fields(:measurement_mutation)
import_fields(:geolocation_mutation)
import_fields(:measurement_mutation)

# ValueFlows

Expand All @@ -138,9 +139,9 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and
def hydrate(%Absinthe.Blueprint{}, _) do
SchemaUtils.hydrations_merge([
Bonfire.Geolocate.GraphQL.Hydration,
Bonfire.Quantify.Hydration,
ValueFlows.Hydration,
ValueFlows.Observe.Hydration
Bonfire.Quantify.GraphQL.Hydration
# ValueFlows.Hydration,
# ValueFlows.Observe.Hydration
])
end

Expand All @@ -162,8 +163,8 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and
# :collection,
:user,
# :organisation,
:category
# :spatial_thing
:category,
:spatial_thing
])

resolve_type(&schema_to_api_type/2)
Expand All @@ -187,68 +188,73 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and
# :organisation,
:category,
:tag,
:poll
# :spatial_thing,
# :intent,
# :process,
# :economic_event
:poll,
:spatial_thing
])

resolve_type(&schema_to_api_type/2)
end

def schema_to_api_type(object, recursing) do
case object do
%Bonfire.Data.Identity.User{} ->
def schema_to_api_type(object, _ \\ nil) do
maybe_schema_to_api_type(object) ||
schema_to_api_type_fallback(object)
end

def maybe_schema_to_api_type(%struct{}), do: maybe_schema_to_api_type(struct)

def maybe_schema_to_api_type(struct) do
case struct do
Bonfire.Data.Identity.User ->
:user

%Bonfire.Data.Social.Post{} ->
Bonfire.Data.Social.Post ->
:post

%Bonfire.Data.Social.Activity{} ->
Bonfire.Data.Social.Activity ->
:activity

%Bonfire.Data.Social.Follow{} ->
Bonfire.Data.Social.Follow ->
:follow

# %Bonfire.Data.SharedUser{} ->
# :organisation
Bonfire.Data.SharedUser ->
:organisation

# %Bonfire.Geolocate.Geolocation{} ->
# :spatial_thing
Bonfire.Geolocate.Geolocation ->
:spatial_thing

%Bonfire.Classify.Category{} ->
Bonfire.Classify.Category ->
:category

%Bonfire.Poll.Question{} ->
Bonfire.Poll.Question ->
:poll

# %Bonfire.Tag{} -> :tag
# %ValueFlows.Planning.Intent{} -> :intent
# %ValueFlows.Process{} -> :process
# %ValueFlows.EconomicEvent{} -> :economic_event

object ->
case Bonfire.Common.Types.typeof(object) do
type when is_atom(type) and not is_nil(type) ->
debug(type, "any_context: object type recognised :-)")

if recursing != true do
schema_to_api_type(struct(type), true)
else
error(type, "any_context: no API type is defined for schema")
debug(object, "any_context object")
nil
end

_ ->
warn(
object,
"any_context: resolved to an unknown type"
)
Bonfire.Tag ->
:tag

_ ->
nil
end
end

def schema_to_api_type_fallback(object, type_match_fun \\ &maybe_schema_to_api_type/1) do
case Bonfire.Common.Types.typeof(object) do
type when is_atom(type) and not is_nil(type) ->
debug(type, "any_context: object type recognised :-)")

type_match_fun.(type) ||
(
warn(type, "any_context: no API type is defined for schema")
debug(object, "any_context object")
nil
end
)

_ ->
warn(
object,
"any_context: resolved to an unknown type, you need to add it to the graphql schema module"
)

nil
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,9 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and
Define dataloaders
see https://hexdocs.pm/absinthe/1.4.6/ecto.html#dataloader
"""
def context(ctx) do
# IO.inspect(ctx: ctx)
loader =
Dataloader.add_source(
Dataloader.new(),
Needle.Pointer,
Bonfire.Common.Needles.dataloader(ctx)
)

# |> Dataloader.add_source(Bonfire.Data.Social.Posts, Bonfire.Common.Needles.dataloader(ctx) )
def context(ctx), do: Bonfire.API.GraphQL.Schema.context(ctx)

Map.put(ctx, :loader, loader)
end

def plugins do
[
Absinthe.Middleware.Async,
Absinthe.Middleware.Batch,
Absinthe.Middleware.Dataloader
] ++
Absinthe.Plugin.defaults()
end
def plugins, do: Bonfire.API.GraphQL.Schema.plugins()

def middleware(middleware, _field, _object) do
# [{Bonfire.API.GraphQL.Middleware.Debug, :start}] ++
Expand Down Expand Up @@ -132,7 +113,7 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and
def hydrate(%Absinthe.Blueprint{}, _) do
SchemaUtils.hydrations_merge([
Bonfire.Geolocate.GraphQL.Hydration,
Bonfire.Quantify.Hydration,
Bonfire.Quantify.GraphQL.Hydration,
ValueFlows.Hydration,
ValueFlows.Observe.Hydration
])
Expand Down Expand Up @@ -190,55 +171,29 @@ if Application.compile_env(:bonfire_api_graphql, :modularity) != :disabled and
resolve_type(&schema_to_api_type/2)
end

def schema_to_api_type(object, recursing) do
case object do
%Bonfire.Data.Identity.User{} ->
:user

%Bonfire.Data.Social.Post{} ->
:post

%Bonfire.Data.Social.Activity{} ->
:activity

%Bonfire.Data.Social.Follow{} ->
:follow

# %Bonfire.Data.SharedUser{} ->
# :organisation

%Bonfire.Geolocate.Geolocation{} ->
:spatial_thing

%Bonfire.Classify.Category{} ->
:category
def schema_to_api_type(object, _ \\ nil) do
maybe_schema_to_api_type(object) ||
Bonfire.API.GraphQL.Schema.schema_to_api_type_fallback(
object,
&maybe_schema_to_api_type/1
)
end

# %Bonfire.Tag{} -> :tag
# %ValueFlows.Planning.Intent{} -> :intent
# %ValueFlows.Process{} -> :process
# %ValueFlows.EconomicEvent{} -> :economic_event
def maybe_schema_to_api_type(%struct{}), do: maybe_schema_to_api_type(struct)

object ->
case Bonfire.Common.Types.typeof(object) do
type when is_atom(type) and not is_nil(type) ->
debug(type, "any_context: object type recognised :-)")
def maybe_schema_to_api_type(struct) do
case struct do
ValueFlows.Planning.Intent ->
:intent

if recursing != true do
schema_to_api_type(struct(type), true)
else
error(type, "any_context: no API type is defined for schema")
debug(object, "any_context object")
nil
end
ValueFlows.Process ->
:process

_ ->
warn(
object,
"any_context: resolved to an unknown type"
)
ValueFlows.EconomicEvent ->
:economic_event

nil
end
_ ->
Bonfire.API.GraphQL.Schema.maybe_schema_to_api_type(struct)
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions lib/web/graphql/util.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
##
#
# Utility structs
#
# These are common data structures used throughout the API
#
##

"""
The `URI` type simply declares a reference to an external web URL, Holochain entry or other resource.
"""
scalar URI

# """
# The `DateTime` scalar type represents a DateTime value as specified by
# [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601).
# """
# scalar DateTime

0 comments on commit 6778116

Please sign in to comment.