Skip to content

Commit 8eaf8c2

Browse files
committed
Make a weak singleton GetSchema function
1 parent 88ed5c6 commit 8eaf8c2

File tree

14 files changed

+129
-54
lines changed

14 files changed

+129
-54
lines changed

samples/separate/QueryObject.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ Query::Query()
3434
{ R"gql(unreadCounts)gql"sv, [this](service::ResolverParams&& params) { return resolveUnreadCounts(std::move(params)); } },
3535
{ R"gql(unreadCountsById)gql"sv, [this](service::ResolverParams&& params) { return resolveUnreadCountsById(std::move(params)); } }
3636
})
37-
, _schema(std::make_shared<schema::Schema>())
37+
, _schema(GetSchema())
3838
{
39-
introspection::AddTypesToSchema(_schema);
40-
today::AddTypesToSchema(_schema);
4139
}
4240

4341
service::FieldResult<std::shared_ptr<service::Object>> Query::getNode(service::FieldParams&&, response::IdType&&) const

samples/separate/TodaySchema.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<obj
9595
{ "query", query },
9696
{ "mutation", mutation },
9797
{ "subscription", subscription }
98-
}, nullptr)
98+
}, GetSchema())
9999
, _query(std::move(query))
100100
, _mutation(std::move(mutation))
101101
, _subscription(std::move(subscription))
@@ -226,5 +226,21 @@ void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema)
226226
schema->AddSubscriptionType(typeSubscription);
227227
}
228228

229+
std::shared_ptr<schema::Schema> GetSchema()
230+
{
231+
static std::weak_ptr<schema::Schema> s_wpSchema;
232+
auto schema = s_wpSchema.lock();
233+
234+
if (!schema)
235+
{
236+
schema = std::make_shared<schema::Schema>();
237+
introspection::AddTypesToSchema(schema);
238+
AddTypesToSchema(schema);
239+
s_wpSchema = schema;
240+
}
241+
242+
return schema;
243+
}
244+
229245
} /* namespace today */
230246
} /* namespace graphql */

samples/separate/TodaySchema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void AddFolderDetails(std::shared_ptr<schema::ObjectType> typeFolder, const std:
8686
void AddNestedTypeDetails(std::shared_ptr<schema::ObjectType> typeNestedType, const std::shared_ptr<schema::Schema>& schema);
8787
void AddExpensiveDetails(std::shared_ptr<schema::ObjectType> typeExpensive, const std::shared_ptr<schema::Schema>& schema);
8888

89-
void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema);
89+
std::shared_ptr<schema::Schema> GetSchema();
9090

9191
} /* namespace today */
9292
} /* namespace graphql */

samples/separate_nointrospection/QueryObject.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ Query::Query()
3232
{ R"gql(unreadCounts)gql"sv, [this](service::ResolverParams&& params) { return resolveUnreadCounts(std::move(params)); } },
3333
{ R"gql(unreadCountsById)gql"sv, [this](service::ResolverParams&& params) { return resolveUnreadCountsById(std::move(params)); } }
3434
})
35-
, _schema(std::make_shared<schema::Schema>())
3635
{
37-
introspection::AddTypesToSchema(_schema);
38-
today::AddTypesToSchema(_schema);
3936
}
4037

4138
service::FieldResult<std::shared_ptr<service::Object>> Query::getNode(service::FieldParams&&, response::IdType&&) const

samples/separate_nointrospection/QueryObject.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ class Query
4141
std::future<response::Value> resolveExpensive(service::ResolverParams&& params);
4242

4343
std::future<response::Value> resolve_typename(service::ResolverParams&& params);
44-
45-
std::shared_ptr<schema::Schema> _schema;
4644
};
4745

4846
} /* namespace graphql::today::object */

samples/separate_nointrospection/TodaySchema.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<obj
9595
{ "query", query },
9696
{ "mutation", mutation },
9797
{ "subscription", subscription }
98-
}, nullptr)
98+
}, GetSchema())
9999
, _query(std::move(query))
100100
, _mutation(std::move(mutation))
101101
, _subscription(std::move(subscription))
@@ -226,5 +226,21 @@ void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema)
226226
schema->AddSubscriptionType(typeSubscription);
227227
}
228228

229+
std::shared_ptr<schema::Schema> GetSchema()
230+
{
231+
static std::weak_ptr<schema::Schema> s_wpSchema;
232+
auto schema = s_wpSchema.lock();
233+
234+
if (!schema)
235+
{
236+
schema = std::make_shared<schema::Schema>();
237+
introspection::AddTypesToSchema(schema);
238+
AddTypesToSchema(schema);
239+
s_wpSchema = schema;
240+
}
241+
242+
return schema;
243+
}
244+
229245
} /* namespace today */
230246
} /* namespace graphql */

samples/separate_nointrospection/TodaySchema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void AddFolderDetails(std::shared_ptr<schema::ObjectType> typeFolder, const std:
8686
void AddNestedTypeDetails(std::shared_ptr<schema::ObjectType> typeNestedType, const std::shared_ptr<schema::Schema>& schema);
8787
void AddExpensiveDetails(std::shared_ptr<schema::ObjectType> typeExpensive, const std::shared_ptr<schema::Schema>& schema);
8888

89-
void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema);
89+
std::shared_ptr<schema::Schema> GetSchema();
9090

9191
} /* namespace today */
9292
} /* namespace graphql */

samples/unified/TodaySchema.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,8 @@ Query::Query()
109109
{ R"gql(unreadCounts)gql"sv, [this](service::ResolverParams&& params) { return resolveUnreadCounts(std::move(params)); } },
110110
{ R"gql(unreadCountsById)gql"sv, [this](service::ResolverParams&& params) { return resolveUnreadCountsById(std::move(params)); } }
111111
})
112-
, _schema(std::make_shared<schema::Schema>())
112+
, _schema(GetSchema())
113113
{
114-
introspection::AddTypesToSchema(_schema);
115-
today::AddTypesToSchema(_schema);
116114
}
117115

118116
service::FieldResult<std::shared_ptr<service::Object>> Query::getNode(service::FieldParams&&, response::IdType&&) const
@@ -1033,7 +1031,7 @@ Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<obj
10331031
{ "query", query },
10341032
{ "mutation", mutation },
10351033
{ "subscription", subscription }
1036-
}, nullptr)
1034+
}, GetSchema())
10371035
, _query(std::move(query))
10381036
, _mutation(std::move(mutation))
10391037
, _subscription(std::move(subscription))
@@ -1261,5 +1259,21 @@ void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema)
12611259
schema->AddSubscriptionType(typeSubscription);
12621260
}
12631261

1262+
std::shared_ptr<schema::Schema> GetSchema()
1263+
{
1264+
static std::weak_ptr<schema::Schema> s_wpSchema;
1265+
auto schema = s_wpSchema.lock();
1266+
1267+
if (!schema)
1268+
{
1269+
schema = std::make_shared<schema::Schema>();
1270+
introspection::AddTypesToSchema(schema);
1271+
AddTypesToSchema(schema);
1272+
s_wpSchema = schema;
1273+
}
1274+
1275+
return schema;
1276+
}
1277+
12641278
} /* namespace today */
12651279
} /* namespace graphql */

samples/unified/TodaySchema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class Operations
374374
std::shared_ptr<object::Subscription> _subscription;
375375
};
376376

377-
void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema);
377+
std::shared_ptr<schema::Schema> GetSchema();
378378

379379
} /* namespace today */
380380
} /* namespace graphql */

samples/unified_nointrospection/TodaySchema.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ Query::Query()
107107
{ R"gql(unreadCounts)gql"sv, [this](service::ResolverParams&& params) { return resolveUnreadCounts(std::move(params)); } },
108108
{ R"gql(unreadCountsById)gql"sv, [this](service::ResolverParams&& params) { return resolveUnreadCountsById(std::move(params)); } }
109109
})
110-
, _schema(std::make_shared<schema::Schema>())
111110
{
112-
introspection::AddTypesToSchema(_schema);
113-
today::AddTypesToSchema(_schema);
114111
}
115112

116113
service::FieldResult<std::shared_ptr<service::Object>> Query::getNode(service::FieldParams&&, response::IdType&&) const
@@ -1017,7 +1014,7 @@ Operations::Operations(std::shared_ptr<object::Query> query, std::shared_ptr<obj
10171014
{ "query", query },
10181015
{ "mutation", mutation },
10191016
{ "subscription", subscription }
1020-
}, nullptr)
1017+
}, GetSchema())
10211018
, _query(std::move(query))
10221019
, _mutation(std::move(mutation))
10231020
, _subscription(std::move(subscription))
@@ -1245,5 +1242,21 @@ void AddTypesToSchema(const std::shared_ptr<schema::Schema>& schema)
12451242
schema->AddSubscriptionType(typeSubscription);
12461243
}
12471244

1245+
std::shared_ptr<schema::Schema> GetSchema()
1246+
{
1247+
static std::weak_ptr<schema::Schema> s_wpSchema;
1248+
auto schema = s_wpSchema.lock();
1249+
1250+
if (!schema)
1251+
{
1252+
schema = std::make_shared<schema::Schema>();
1253+
introspection::AddTypesToSchema(schema);
1254+
AddTypesToSchema(schema);
1255+
s_wpSchema = schema;
1256+
}
1257+
1258+
return schema;
1259+
}
1260+
12481261
} /* namespace today */
12491262
} /* namespace graphql */

0 commit comments

Comments
 (0)