X Tutup
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/graphql/execution/directives/QueryDirectives.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package graphql.execution.directives;

import graphql.DeprecatedAt;
import graphql.GraphQLContext;
import graphql.PublicApi;
import graphql.execution.CoercedVariables;
import graphql.execution.MergedField;
import graphql.language.Field;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLSchema;

import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
Expand Down Expand Up @@ -89,4 +94,28 @@ public interface QueryDirectives {
@Deprecated
@DeprecatedAt("2022-02-24")
Map<Field, List<GraphQLDirective>> getImmediateDirectivesByField();

/**
* @return a builder of {@link QueryDirectives}
*/
static Builder newQueryDirectives() {
return new QueryDirectivesBuilder();
}

interface Builder {

Builder schema(GraphQLSchema schema);

Builder mergedField(MergedField mergedField);

Builder field(Field field);

Builder coercedVariables(CoercedVariables coercedVariables);

Builder graphQLContext(GraphQLContext graphQLContext);

Builder locale(Locale locale);

QueryDirectives build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package graphql.execution.directives;

import graphql.GraphQLContext;
import graphql.Internal;
import graphql.execution.CoercedVariables;
import graphql.execution.MergedField;
import graphql.language.Field;
import graphql.schema.GraphQLSchema;

import java.util.Locale;

@Internal
public class QueryDirectivesBuilder implements QueryDirectives.Builder {

private MergedField mergedField;
private GraphQLSchema schema;
private CoercedVariables coercedVariables = CoercedVariables.emptyVariables();
private GraphQLContext graphQLContext = GraphQLContext.getDefault();
private Locale locale = Locale.getDefault();

@Override
public QueryDirectives.Builder schema(GraphQLSchema schema) {
this.schema = schema;
return this;
}

@Override
public QueryDirectives.Builder mergedField(MergedField mergedField) {
this.mergedField = mergedField;
return this;
}

@Override
public QueryDirectives.Builder field(Field field) {
this.mergedField = MergedField.newMergedField(field).build();
return this;
}

@Override
public QueryDirectives.Builder coercedVariables(CoercedVariables coercedVariables) {
this.coercedVariables = coercedVariables;
return this;
}

@Override
public QueryDirectives.Builder graphQLContext(GraphQLContext graphQLContext) {
this.graphQLContext = graphQLContext;
return this;
}

@Override
public QueryDirectives.Builder locale(Locale locale) {
this.locale = locale;
return this;
}


@Override
public QueryDirectives build() {
return new QueryDirectivesImpl(mergedField, schema, coercedVariables.toMap(), graphQLContext, locale);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphql.execution.directives

import graphql.GraphQLContext
import graphql.TestUtil
import graphql.execution.CoercedVariables
import graphql.execution.MergedField
import spock.lang.Specification

Expand Down Expand Up @@ -66,4 +67,26 @@ class QueryDirectivesImplTest extends Specification {
appliedResult[1].getArgument("forMillis").getValue() == 10
}

def "builder works as expected"() {

def f1 = TestUtil.parseField("f1 @cached @upper")
def f2 = TestUtil.parseField("f2 @cached(forMillis : \$var) @timeout")

def mergedField = MergedField.newMergedField([f1, f2]).build()

def queryDirectives = QueryDirectives.newQueryDirectives()
.mergedField(mergedField)
.schema(schema)
.coercedVariables(CoercedVariables.of([var: 10]))
.graphQLContext(GraphQLContext.getDefault())
.locale(Locale.getDefault())
.build()

when:
def appliedDirectivesByName = queryDirectives.getImmediateAppliedDirectivesByName()

then:
appliedDirectivesByName.keySet().sort() == ["cached", "timeout", "upper"]

}
}
X Tutup