public List<GraphQLArgument> getBackwardPaginationConnectionFieldArguments() { List<GraphQLArgument> args = new ArrayList<>(); args.add(newArgument() .name("before") .description("fetching only nodes before this node (exclusive)") .type(GraphQLString) .build()); args.add(newArgument() .name("last") .description("fetching only the last certain number of nodes") .type(GraphQLInt) .build()); return args; }
public List<GraphQLArgument> getForwardPaginationConnectionFieldArguments() { List<GraphQLArgument> args = new ArrayList<>(); args.add(newArgument() .name("after") .description("fetching only nodes after this node (exclusive)") .type(GraphQLString) .build()); args.add(newArgument() .name("first") .description("fetching only the first certain number of nodes") .type(GraphQLInt) .build()); return args; }
private GraphQLArgument buildDirectiveArgument(Argument arg, Optional<GraphQLDirective> directiveDefinition) { Optional<GraphQLArgument> directiveDefArgument = directiveDefinition.map(dd -> dd.getArgument(arg.getName())); GraphQLArgument.Builder builder = GraphQLArgument.newArgument(); builder.name(arg.getName()); GraphQLInputType inputType; Object defaultValue = null; if (directiveDefArgument.isPresent()) { inputType = directiveDefArgument.get().getType(); defaultValue = directiveDefArgument.get().getDefaultValue(); } else { inputType = buildDirectiveInputType(arg.getValue()); } builder.type(inputType); builder.defaultValue(defaultValue); Object value = buildValue(arg.getValue(), inputType); // // we put the default value in if the specified is null builder.value(value == null ? defaultValue : value); return builder.build(); }
public static GraphQLArgument floatArg (String name) { return newArgument() .name(name) .type(GraphQLFloat) .build(); }
@Test public void queryName_chainedDirectives_wiringIsActivatedInCorrectOrder() throws Exception { GraphQLDirective upperCase = newDirective().name("upperCase").argument(builder -> builder.name("isActive").type(GraphQLBoolean).defaultValue(true)) .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build(); GraphQLDirective suffixDirective = GraphQLDirective.newDirective().name("suffix").argument(builder -> builder.name("suffix").type(GraphQLString)) .validLocations(Introspection.DirectiveLocation.FIELD_DEFINITION).build(); GraphQLObjectType object = GraphQLAnnotations.object(Query3.class, upperCase, suffixDirective); GraphQLSchema schema = newSchema().query(object).build(); ExecutionResult result = GraphQL.newGraphQL(schema).build().execute("query { name }"); assertTrue(result.getErrors().isEmpty()); assertEquals(((Map<String, String>) result.getData()).get("name").toString(), "YARINcoolSuffix"); }
@Override public GraphQLArgument transformArgument(GraphQLArgument argument, DirectiveArgument directiveArgument, OperationMapper operationMapper, BuildContext buildContext) { if (directiveArgument.getAnnotation() != null && directiveArgument.getDefaultValue() == null) { return argument.transform(builder -> builder.type(GraphQLNonNull.nonNull(argument.getType()))); } return transformArgument(argument, directiveArgument.getTypedElement(), directiveArgument.toString(), operationMapper, buildContext); }
/** * Return a new name argument with the provided description. * * @param description * @return */ public GraphQLArgument createNameArg(String description) { return newArgument().name("name").type(GraphQLString).description(description).build(); }
private GraphQLArgument toGraphQLArgument(DirectiveArgument directiveArgument, BuildContext buildContext) { GraphQLArgument argument = newArgument() .name(directiveArgument.getName()) .description(directiveArgument.getDescription()) .type(toGraphQLInputType(directiveArgument.getJavaType(), buildContext)) .value(directiveArgument.getValue()) .defaultValue(directiveArgument.getDefaultValue()) .withDirectives(toGraphQLDirectives(directiveArgument.getTypedElement(), buildContext.directiveBuilder::buildArgumentDefinitionDirectives, buildContext)) .build(); return buildContext.transformers.transform(argument, directiveArgument, this, buildContext); }
.argument(GraphQLArgument.newArgument() .name("date") .type(Scalars.GraphQLString) .build()) .dataFetcher(environment -> { .argument(GraphQLArgument.newArgument() .name("startTime") .type(Scalars.GraphQLString) // No long exists in GraphQL .defaultValue("0") // Default value is current time .build()) .argument(GraphQLArgument.newArgument() .name("timeRange") .type(Scalars.GraphQLInt) .defaultValue(24 * 60 * 60) .build()) .argument(GraphQLArgument.newArgument() .name("numberOfDepartures") .type(Scalars.GraphQLInt) .defaultValue(5) .build()) .argument(GraphQLArgument.newArgument() .name("omitNonPickups") .type(Scalars.GraphQLBoolean) .defaultValue(false) .build()) .argument(GraphQLArgument.newArgument() .name("startTime")
.argument(GraphQLArgument.newArgument() .name("regex") .type(GraphQLString)) .dataFetcher(splitFetcher)) .argument(GraphQLArgument.newArgument() .name("regex") .type(GraphQLString)) .dataFetcher(splitFetcher)) .argument(GraphQLArgument.newArgument() .name("text") .type(GraphQLString)) .dataFetcher(appendFetcher)) .argument(GraphQLArgument.newArgument() .name("text") .type(GraphQLString)) .dataFetcher(emptyOptionalFetcher)) .argument(GraphQLArgument.newArgument() .name("text") .type(GraphQLString)) .dataFetcher(optionalFetcher)) .argument(GraphQLArgument.newArgument() .name("text") .type(GraphQLString)) .dataFetcher(completableFutureFetcher))
.newArgument() .name("format") .type(Scalars.GraphQLString) .defaultValue("dd-MM-YYYY")
public List<GraphQLArgument> getConnectionFieldArguments() { List<GraphQLArgument> args = new ArrayList<>(); args.add(newArgument() .name("before") .description("fetching only nodes before this node (exclusive)") .type(GraphQLString) .build()); args.add(newArgument() .name("after") .description("fetching only nodes after this node (exclusive)") .type(GraphQLString) .build()); args.add(newArgument() .name("first") .description("fetching only the first certain number of nodes") .type(GraphQLInt) .build()); args.add(newArgument() .name("last") .description("fetching only the last certain number of nodes") .type(GraphQLInt) .build()); return args; }
private GraphQLArgument buildArgument(BuildContext buildCtx, InputValueDefinition valueDefinition) { buildCtx.enterNode(valueDefinition); GraphQLArgument.Builder builder = GraphQLArgument.newArgument(); builder.definition(valueDefinition); builder.name(valueDefinition.getName()); builder.description(schemaGeneratorHelper.buildDescription(valueDefinition, valueDefinition.getDescription())); GraphQLInputType inputType = buildInputType(buildCtx, valueDefinition.getType()); builder.type(inputType); Value defaultValue = valueDefinition.getDefaultValue(); if (defaultValue != null) { builder.defaultValue(schemaGeneratorHelper.buildValue(defaultValue, inputType)); } builder.withDirectives( buildDirectives(valueDefinition.getDirectives(), emptyList(), ARGUMENT_DEFINITION, buildCtx.getDirectiveDefinitions()) ); GraphQLArgument argument = builder.build(); argument = directiveBehaviour.onArgument(argument, buildCtx.mkBehaviourParams()); return buildCtx.exitNode(argument); }
public GraphQLFieldDefinition mutation(String name, String fieldName, List<GraphQLInputObjectField> inputFields, List<GraphQLFieldDefinition> outputFields, DataFetcher dataFetcher) { GraphQLInputObjectType inputObjectType = newInputObject() .name(name + "Input") .fields(inputFields) .build(); GraphQLObjectType outputType = newObject() .name(name + "Payload") .fields(outputFields) .build(); return newFieldDefinition() .name(fieldName) .type(outputType) .argument(newArgument() .name("input") .type(nonNull(inputObjectType))) .dataFetcher(dataFetcher) .build(); }
private List<GraphQLArgument> transferMissingArguments(List<GraphQLArgument> arguments, GraphQLDirective directiveDefinition) { Map<String, GraphQLArgument> declaredArgs = FpKit.getByName(arguments, GraphQLArgument::getName, FpKit.mergeFirst()); List<GraphQLArgument> argumentsOut = new ArrayList<>(arguments); for (GraphQLArgument directiveDefArg : directiveDefinition.getArguments()) { if (!declaredArgs.containsKey(directiveDefArg.getName())) { GraphQLArgument missingArg = GraphQLArgument.newArgument() .name(directiveDefArg.getName()) .description(directiveDefArg.getDescription()) .definition(directiveDefArg.getDefinition()) .type(directiveDefArg.getType()) .defaultValue(directiveDefArg.getDefaultValue()) .value(directiveDefArg.getDefaultValue()) .build(); argumentsOut.add(missingArg); } } return argumentsOut; }
void listsAndNonNullLists() { GraphQLList.list(GraphQLString); // a list of Strings GraphQLNonNull.nonNull(GraphQLString); // a non null String // with static imports its even shorter newArgument() .name("example") .type(nonNull(list(GraphQLString))); }
private GraphQLArgument buildDirectiveArgumentFromDefinition(InputValueDefinition arg, Function<Type, GraphQLInputType> inputTypeFactory) { GraphQLArgument.Builder builder = GraphQLArgument.newArgument() .name(arg.getName()) .definition(arg); GraphQLInputType inputType = inputTypeFactory.apply(arg.getType()); builder.type(inputType); builder.value(buildValue(arg.getDefaultValue(), inputType)); builder.defaultValue(buildValue(arg.getDefaultValue(), inputType)); return builder.build(); }
public GraphQLFieldDefinition nodeField(GraphQLInterfaceType nodeInterface, DataFetcher nodeDataFetcher) { return newFieldDefinition() .name("node") .description("Fetches an object given its ID") .type(nodeInterface) .dataFetcher(nodeDataFetcher) .argument(newArgument() .name("id") .description("The ID of an object") .type(nonNull(GraphQLID))) .build(); }