Skip to content

Commit 3fa03ec

Browse files
committed
option to not output descriptions in schema
1 parent 287e5cc commit 3fa03ec

5 files changed

Lines changed: 38 additions & 19 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- A resolver can now be told what the engine will read off the objects it returns, so it can fetch only that instead of everything - the point of batching with `ResolveBulk` when the data comes from another database or service. Take an `IFieldSelection` parameter and the engine supplies it, the way it supplies `CancellationToken` and `QueryRequestContext`; new two-service `ResolveBulk`/`ResolveBulkAsync` overloads let a loader take it alongside its own service. It is not the caller's selection set: a selected field that is itself resolved from a service is replaced by the member its resolver reads, nested objects come through as paths (`Address.City`), `@skip`/`@include` are applied, fragments are expanded, aliases collapse, `__typename` is excluded, and one load is told the union of every place the field is selected. See [Fetching only the fields that will be used](https://entitygraphql.github.io/schema-creation/other-data-sources#fetching-only-the-fields-that-will-be-used).
66
- A field with a `ResolveBulk`/`ResolveBulkAsync` resolver that has to resolve per item instead of bulk loading now logs a warning to the `ILogger` the schema was built with, naming the field and why the bulk load could not run - the query shape usually controls it, so it is something a caller can act on. Turn it off with `ExecutionOptions.WarnOnBulkResolverFallback = false`. `ISchemaProvider.Logger` is new (exceptions keep going through `LogException`); it returns null by default so existing implementations are unaffected.
7+
- `ToGraphQLSchemaString()` takes an optional `includeDescriptions` argument. Pass `false` to leave the `"""..."""` descriptions out of the generated SDL.
78

89
## Fixes
910

‎src/EntityGraphQL/Schema/ISchemaProvider.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void AddMutationsFrom<TType>(SchemaBuilderOptions? options = null)
9292
ISchemaProvider RemoveType(string schemaType);
9393
void RemoveTypeAndAllFields<TSchemaType>();
9494
void RemoveTypeAndAllFields(string typeName);
95-
string ToGraphQLSchemaString();
95+
string ToGraphQLSchemaString(bool includeDescriptions = true);
9696
SchemaType<TType> Type<TType>();
9797
SchemaType<TType> Type<TType>(string typeName);
9898
ISchemaType Type(string typeName);

‎src/EntityGraphQL/Schema/SchemaGenerator.cs‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ internal static string EscapeString(string? input)
2222
return input.Replace("\\", "\\\\").Replace("\"", "\\\"");
2323
}
2424

25-
internal static string Make(ISchemaProvider schema)
25+
internal static string Make(ISchemaProvider schema, bool includeDescriptions = true)
2626
{
2727
var rootQueryType = schema.GetSchemaType(schema.QueryContextType, false, null);
2828
var mutationType = schema.Mutation().SchemaType;
2929
var subscriptionType = schema.Subscription().SchemaType;
3030

31-
var types = BuildSchemaTypes(schema);
31+
var types = BuildSchemaTypes(schema, includeDescriptions);
3232

3333
var schemaBuilder = new StringBuilder("schema {");
3434
schemaBuilder.AppendLine();
@@ -45,44 +45,44 @@ internal static string Make(ISchemaProvider schema)
4545

4646
foreach (var item in schema.GetScalarTypes().Distinct().OrderBy(t => t.Name))
4747
{
48-
if (!string.IsNullOrEmpty(item.Description))
48+
if (includeDescriptions && !string.IsNullOrEmpty(item.Description))
4949
schemaBuilder.AppendLine($"\"\"\"{EscapeString(item.Description)}\"\"\"");
5050
schemaBuilder.AppendLine($"scalar {item.Name}{GetDirectives(item.Directives)}");
5151
}
5252
schemaBuilder.AppendLine();
5353

5454
foreach (var directive in schema.GetDirectives().OrderBy(t => t.Name))
5555
{
56-
if (!string.IsNullOrEmpty(directive.Description))
56+
if (includeDescriptions && !string.IsNullOrEmpty(directive.Description))
5757
schemaBuilder.AppendLine($"\"\"\"{EscapeString(directive.Description)}\"\"\"");
5858

5959
schemaBuilder.AppendLine($"directive @{directive.Name}{GetDirectiveArgs(schema, directive)} on {string.Join(" | ", directive.Location.Select(i => i.GetDescription()))}");
6060
}
6161
schemaBuilder.AppendLine();
6262

63-
schemaBuilder.Append(BuildEnumTypes(schema));
63+
schemaBuilder.Append(BuildEnumTypes(schema, includeDescriptions));
6464

65-
schemaBuilder.AppendLine(OutputSchemaType(schema, schema.GetSchemaType(schema.QueryContextName, null)));
65+
schemaBuilder.AppendLine(OutputSchemaType(schema, schema.GetSchemaType(schema.QueryContextName, null), includeDescriptions));
6666

6767
schemaBuilder.Append(types);
6868

6969
if (outputMutation)
70-
schemaBuilder.AppendLine(OutputSchemaType(schema, schema.Mutation().SchemaType));
70+
schemaBuilder.AppendLine(OutputSchemaType(schema, schema.Mutation().SchemaType, includeDescriptions));
7171
if (outputSubscription)
72-
schemaBuilder.AppendLine(OutputSchemaType(schema, schema.Subscription().SchemaType));
72+
schemaBuilder.AppendLine(OutputSchemaType(schema, schema.Subscription().SchemaType, includeDescriptions));
7373

7474
return schemaBuilder.ToString();
7575
}
7676

77-
private static string BuildEnumTypes(ISchemaProvider schema)
77+
private static string BuildEnumTypes(ISchemaProvider schema, bool includeDescriptions)
7878
{
7979
var types = new StringBuilder();
8080
foreach (var typeItem in schema.GetNonContextTypes().OrderBy(t => t.Name))
8181
{
8282
if (typeItem.Name.StartsWith("__", StringComparison.InvariantCulture) || !typeItem.IsEnum)
8383
continue;
8484

85-
if (!string.IsNullOrEmpty(typeItem.Description))
85+
if (includeDescriptions && !string.IsNullOrEmpty(typeItem.Description))
8686
types.AppendLine($"\"\"\"{EscapeString(typeItem.Description)}\"\"\"");
8787

8888
types.AppendLine($"enum {typeItem.Name} {{");
@@ -91,7 +91,7 @@ private static string BuildEnumTypes(ISchemaProvider schema)
9191
if (field.Name.StartsWith("__", StringComparison.InvariantCulture))
9292
continue;
9393

94-
if (!string.IsNullOrEmpty(field.Description))
94+
if (includeDescriptions && !string.IsNullOrEmpty(field.Description))
9595
types.AppendLine($"\t\"\"\"{EscapeString(field.Description)}\"\"\"");
9696

9797
types.AppendLine($"\t{field.Name}{GetDirectives(field.DirectivesReadOnly)}");
@@ -103,7 +103,7 @@ private static string BuildEnumTypes(ISchemaProvider schema)
103103
return types.ToString();
104104
}
105105

106-
private static string BuildSchemaTypes(ISchemaProvider schema)
106+
private static string BuildSchemaTypes(ISchemaProvider schema, bool includeDescriptions)
107107
{
108108
var types = new StringBuilder();
109109
foreach (var typeItem in schema.GetNonContextTypes().OrderBy(t => t.Name))
@@ -120,7 +120,7 @@ private static string BuildSchemaTypes(ISchemaProvider schema)
120120
if (!typeItem.GetFields().Any(f => !f.Name.StartsWith("__", StringComparison.InvariantCulture)) && typeItem.GqlType != GqlTypes.Union && typeItem.BaseTypesReadOnly.Count == 0)
121121
continue;
122122

123-
types.AppendLine(OutputSchemaType(schema, typeItem));
123+
types.AppendLine(OutputSchemaType(schema, typeItem, includeDescriptions));
124124
}
125125

126126
return types.ToString();
@@ -238,11 +238,11 @@ private static string GetDirectiveArgs(ISchemaProvider schema, IDirectiveProcess
238238
return string.IsNullOrEmpty(allArgs) ? string.Empty : $"({allArgs})";
239239
}
240240

241-
private static string OutputSchemaType(ISchemaProvider schema, ISchemaType schemaType)
241+
private static string OutputSchemaType(ISchemaProvider schema, ISchemaType schemaType, bool includeDescriptions)
242242
{
243243
var sb = new StringBuilder();
244244

245-
if (!string.IsNullOrEmpty(schemaType.Description))
245+
if (includeDescriptions && !string.IsNullOrEmpty(schemaType.Description))
246246
sb.AppendLine($"\"\"\"{EscapeString(schemaType.Description)}\"\"\"");
247247

248248
if (schemaType.GqlType == GqlTypes.Union)
@@ -276,7 +276,7 @@ private static string OutputSchemaType(ISchemaProvider schema, ISchemaType schem
276276
{
277277
if (field.Name.StartsWith("__", StringComparison.InvariantCulture))
278278
continue;
279-
if (!string.IsNullOrEmpty(field.Description))
279+
if (includeDescriptions && !string.IsNullOrEmpty(field.Description))
280280
sb.AppendLine($"\t\"\"\"{EscapeString(field.Description)}\"\"\"");
281281
sb.AppendLine($"\t{schema.SchemaFieldNamer(field.Name)}{GetGqlArgs(schema, field)}: {field.ReturnType.GqlTypeForReturnOrArgument}{GetDirectives(field.DirectivesReadOnly)}");
282282
}

‎src/EntityGraphQL/Schema/SchemaProvider.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,10 +1138,11 @@ public List<ISchemaType> GetEnumTypes()
11381138
/// <summary>
11391139
/// Builds a GraphQL schema definition from the schema.
11401140
/// </summary>
1141+
/// <param name="includeDescriptions">Include descriptions (doc strings) in the output. Defaults to true</param>
11411142
/// <returns>String containing the schema definition</returns>
1142-
public string ToGraphQLSchemaString()
1143+
public string ToGraphQLSchemaString(bool includeDescriptions = true)
11431144
{
1144-
return SchemaGenerator.Make(this);
1145+
return SchemaGenerator.Make(this, includeDescriptions);
11451146
}
11461147

11471148
/// <summary>

‎src/tests/EntityGraphQL.Tests/SchemaTests/ToGraphQLSchemaStringTests.cs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ public void TestIgnoreWithSchema()
3131
Assert.Contains("addAlbum(name: String!, genre: Genre!): Album", schema);
3232
}
3333

34+
[Fact]
35+
public void TestExcludeDescriptions()
36+
{
37+
var schemaProvider = SchemaBuilder.FromObject<IgnoreTestSchema>();
38+
schemaProvider.Type<Album>().Description = "An album";
39+
schemaProvider.Type<Album>().ReplaceField("name", a => a.Name, "The album name");
40+
41+
Assert.Contains("An album", schemaProvider.ToGraphQLSchemaString());
42+
Assert.Contains("The album name", schemaProvider.ToGraphQLSchemaString());
43+
44+
var schema = schemaProvider.ToGraphQLSchemaString(includeDescriptions: false);
45+
Assert.DoesNotContain("An album", schema);
46+
Assert.DoesNotContain("The album name", schema);
47+
Assert.DoesNotContain("\"\"\"", schema);
48+
Assert.Contains("name: String!", schema);
49+
}
50+
3451
[Fact]
3552
public void TestIgnoreWithSchemaBuilder()
3653
{

0 commit comments

Comments
 (0)