Skip to content

Commit 0b7756c

Browse files
committed
allow to print NFs directly
1 parent 1289fd0 commit 0b7756c

File tree

2 files changed

+107
-9
lines changed

2 files changed

+107
-9
lines changed

src/main/java/graphql/normalized/nf/NormalizedOperationToAstCompiler.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,51 @@ public Map<String, Object> getVariables() {
7070
}
7171
}
7272

73-
public static CompilerResult compileToDocument(@NotNull GraphQLSchema schema,
73+
public static CompilerResult compileToDocument(GraphQLSchema graphQLSchema,
74+
GraphQLObjectType rootType,
75+
List<NormalizedField> rootFields,
76+
@Nullable String operationName,
77+
OperationDefinition.Operation operationKind) {
78+
79+
return compileToDocumentImpl(graphQLSchema, rootType, rootFields, operationName, operationKind);
80+
}
81+
82+
public static CompilerResult compileToDocument(GraphQLSchema graphQLSchema,
83+
GraphQLObjectType rootType,
84+
NormalizedField singleRootField,
85+
@Nullable String operationName,
86+
OperationDefinition.Operation operationKind) {
87+
return compileToDocumentImpl(graphQLSchema, rootType, ImmutableList.of(singleRootField), operationName, operationKind);
88+
89+
90+
}
91+
92+
93+
public static CompilerResult compileToDocument(GraphQLSchema schema,
7494
NormalizedOperation normalizedOperation) {
7595
GraphQLObjectType operationType = getOperationType(schema, normalizedOperation.getOperation());
7696

77-
List<Selection<?>> selections = subSelectionsForNormalizedField(schema, operationType.getName(), normalizedOperation.getRootFields());
97+
return compileToDocumentImpl(
98+
schema,
99+
operationType,
100+
normalizedOperation.getRootFields(),
101+
normalizedOperation.getOperationName(),
102+
normalizedOperation.getOperation()
103+
);
104+
}
105+
106+
private static CompilerResult compileToDocumentImpl(GraphQLSchema schema,
107+
GraphQLObjectType rootType,
108+
List<NormalizedField> rootFields,
109+
@Nullable String operationName,
110+
OperationDefinition.Operation operationKind) {
111+
112+
List<Selection<?>> selections = subSelectionsForNormalizedFields(schema, rootType.getName(), rootFields);
78113
SelectionSet selectionSet = new SelectionSet(selections);
79114

80115
OperationDefinition.Builder definitionBuilder = OperationDefinition.newOperationDefinition()
81-
.name(normalizedOperation.getOperationName())
82-
.operation(normalizedOperation.getOperation())
116+
.name(operationName)
117+
.operation(operationKind)
83118
.selectionSet(selectionSet);
84119

85120
// definitionBuilder.variableDefinitions(variableAccumulator.getVariableDefinitions());
@@ -92,9 +127,10 @@ public static CompilerResult compileToDocument(@NotNull GraphQLSchema schema,
92127
);
93128
}
94129

95-
private static List<Selection<?>> subSelectionsForNormalizedField(GraphQLSchema schema,
96-
@NotNull String parentOutputType,
97-
List<NormalizedField> normalizedFields
130+
131+
private static List<Selection<?>> subSelectionsForNormalizedFields(GraphQLSchema schema,
132+
@NotNull String parentOutputType,
133+
List<NormalizedField> normalizedFields
98134
) {
99135
ImmutableList.Builder<Selection<?>> selections = ImmutableList.builder();
100136

@@ -155,7 +191,7 @@ private static Field selectionForNormalizedField(GraphQLSchema schema,
155191
GraphQLFieldDefinition fieldDef = getFieldDefinition(schema, objectTypeName, normalizedField);
156192
GraphQLUnmodifiedType fieldOutputType = unwrapAll(fieldDef.getType());
157193

158-
subSelections = subSelectionsForNormalizedField(
194+
subSelections = subSelectionsForNormalizedFields(
159195
schema,
160196
fieldOutputType.getName(),
161197
normalizedField.getChildren()
@@ -195,7 +231,6 @@ private static GraphQLFieldDefinition getFieldDefinition(GraphQLSchema schema,
195231
}
196232

197233

198-
@Nullable
199234
private static GraphQLObjectType getOperationType(@NotNull GraphQLSchema schema,
200235
@NotNull OperationDefinition.Operation operationKind) {
201236
switch (operationKind) {

src/test/groovy/graphql/normalized/nf/NormalizedOperationToAstCompilerTest.groovy

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import graphql.GraphQL
44
import graphql.TestUtil
55
import graphql.language.AstPrinter
66
import graphql.language.AstSorter
7+
import graphql.language.OperationDefinition
78
import graphql.parser.Parser
89
import graphql.schema.GraphQLSchema
910
import spock.lang.Specification
@@ -185,6 +186,68 @@ class NormalizedOperationToAstCompilerTest extends Specification {
185186
}
186187

187188

189+
def "print one root field"() {
190+
def sdl = """
191+
type Query {
192+
foo: Foo
193+
}
194+
type Foo {
195+
bar: String
196+
}
197+
"""
198+
def query = '''
199+
{ foo { bar } }
200+
'''
201+
GraphQLSchema schema = TestUtil.schema(sdl)
202+
assertValidQuery(schema, query)
203+
def normalizedDocument = NormalizedDocumentFactory.createNormalizedDocument(schema, Parser.parse(query))
204+
def normalizedOperation = normalizedDocument.getSingleNormalizedOperation()
205+
def rootField = normalizedOperation.getRootFields().get(0)
206+
when:
207+
def result = NormalizedOperationToAstCompiler.compileToDocument(schema, schema.getObjectType("Query"), rootField, "myOperation", OperationDefinition.Operation.QUERY)
208+
def printed = AstPrinter.printAst(new AstSorter().sort(result.document))
209+
then:
210+
printed == '''query myOperation {
211+
foo {
212+
bar
213+
}
214+
}
215+
'''
216+
}
217+
218+
def "print list of root fields"() {
219+
def sdl = """
220+
type Query {
221+
foo: Foo
222+
}
223+
type Foo {
224+
bar: String
225+
}
226+
"""
227+
def query = '''
228+
{ foo { bar } foo2: foo { bar } }
229+
'''
230+
GraphQLSchema schema = TestUtil.schema(sdl)
231+
assertValidQuery(schema, query)
232+
def normalizedDocument = NormalizedDocumentFactory.createNormalizedDocument(schema, Parser.parse(query))
233+
def normalizedOperation = normalizedDocument.getSingleNormalizedOperation()
234+
def rootFields = normalizedOperation.getRootFields()
235+
when:
236+
def result = NormalizedOperationToAstCompiler.compileToDocument(schema, schema.getObjectType("Query"), rootFields, "myOperation", OperationDefinition.Operation.QUERY)
237+
def printed = AstPrinter.printAst(new AstSorter().sort(result.document))
238+
then:
239+
printed == '''query myOperation {
240+
foo {
241+
bar
242+
}
243+
foo2: foo {
244+
bar
245+
}
246+
}
247+
'''
248+
}
249+
250+
188251
private void assertValidQuery(GraphQLSchema graphQLSchema, String query, Map variables = [:]) {
189252
GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build()
190253
assert graphQL.execute(newExecutionInput().query(query).variables(variables)).errors.isEmpty()

0 commit comments

Comments
 (0)