private boolean isNoNullArgWithoutDefaultValue(InputValueDefinition definitionArgument) { return definitionArgument.getType() instanceof NonNullType && definitionArgument.getDefaultValue() == null; } }
/** * Parses an AST value literal into the correct {@link graphql.language.Value} which * MUST be of the correct shape eg '"string"' or 'true' or '1' or '{ "object", "form" }' * or '[ "array", "form" ]' otherwise an exception is thrown * * @param astLiteral the string to parse an AST literal * * @return a valid Value * * @throws graphql.AssertException if the input can be parsed */ public static Value valueFromAst(String astLiteral) { // we use the parser to give us the AST elements as if we defined an inputType String toParse = "input X { x : String = " + astLiteral + "}"; try { Document doc = new Parser().parseDocument(toParse); InputObjectTypeDefinition inputType = (InputObjectTypeDefinition) doc.getDefinitions().get(0); InputValueDefinition inputValueDefinition = inputType.getInputValueDefinitions().get(0); return inputValueDefinition.getDefaultValue(); } catch (Exception e) { return Assert.assertShouldNeverHappen("valueFromAst of '%s' failed because of '%s'", astLiteral, e.getMessage()); } } }
private NodePrinter<InputValueDefinition> inputValueDefinition() { String nameTypeSep = compactMode ? ":" : ": "; String defaultValueEquals = compactMode ? "=" : "= "; return (out, node) -> { Value defaultValue = node.getDefaultValue(); out.printf("%s", comments(node)); out.printf("%s", spaced( node.getName() + nameTypeSep + type(node.getType()), wrap(defaultValueEquals, defaultValue, ""), directives(node.getDirectives()) ) ); }; }
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(); }
private Builder(InputValueDefinition existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.name = existing.getName(); this.type = existing.getType(); this.defaultValue = existing.getDefaultValue(); this.description = existing.getDescription(); this.directives = existing.getDirectives(); }
private void checkArgInputObjectValueFieldMatchesAllowedDefinition(List<GraphQLError> errors, ObjectField objectField, InputValueDefinition allowedValueDef) { if (objectField != null) { checkArgValueMatchesAllowedType(errors, objectField.getValue(), allowedValueDef.getType()); return; } // check if field definition is required and has no default value if (allowedValueDef.getType() instanceof NonNullType && allowedValueDef.getDefaultValue() == null) { addValidationError(errors, MISSING_REQUIRED_FIELD_MESSAGE, allowedValueDef.getName()); } // other cases are // - field definition is marked as non-null but has a default value, so the default value can be used // - field definition is nullable hence null can be used }
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); }
private GraphQLInputObjectField buildInputField(BuildContext buildCtx, InputValueDefinition fieldDef) { buildCtx.enterNode(fieldDef); GraphQLInputObjectField.Builder fieldBuilder = GraphQLInputObjectField.newInputObjectField(); fieldBuilder.definition(fieldDef); fieldBuilder.name(fieldDef.getName()); fieldBuilder.description(schemaGeneratorHelper.buildDescription(fieldDef, fieldDef.getDescription())); // currently the spec doesnt allow deprecations on InputValueDefinitions but it should! //fieldBuilder.deprecate(buildDeprecationReason(fieldDef.getDirectives())); GraphQLInputType inputType = buildInputType(buildCtx, fieldDef.getType()); fieldBuilder.type(inputType); Value defaultValue = fieldDef.getDefaultValue(); if (defaultValue != null) { fieldBuilder.defaultValue(schemaGeneratorHelper.buildValue(defaultValue, inputType)); } fieldBuilder.withDirectives( buildDirectives(fieldDef.getDirectives(), emptyList(), INPUT_FIELD_DEFINITION, buildCtx.getDirectiveDefinitions()) ); GraphQLInputObjectField inputObjectField = fieldBuilder.build(); inputObjectField = directiveBehaviour.onInputObjectField(inputObjectField, buildCtx.mkBehaviourParams()); return buildCtx.exitNode(inputObjectField); }
Value oldValue = oldArg.getDefaultValue(); Value newValue = newArg.getDefaultValue(); if (oldValue != null && newValue != null) { if (!oldValue.getClass().equals(newValue.getClass())) {
private boolean isNoNullArgWithoutDefaultValue(InputValueDefinition definitionArgument) { return definitionArgument.getType() instanceof NonNullType && definitionArgument.getDefaultValue() == null; } }
/** * Parses an AST value literal into the correct {@link graphql.language.Value} which * MUST be of the correct shape eg '"string"' or 'true' or '1' or '{ "object", "form" }' * or '[ "array", "form" ]' otherwise an exception is thrown * * @param astLiteral the string to parse an AST literal * * @return a valid Value * * @throws graphql.AssertException if the input can be parsed */ public static Value valueFromAst(String astLiteral) { // we use the parser to give us the AST elements as if we defined an inputType String toParse = "input X { x : String = " + astLiteral + "}"; try { Document doc = new Parser().parseDocument(toParse); InputObjectTypeDefinition inputType = (InputObjectTypeDefinition) doc.getDefinitions().get(0); InputValueDefinition inputValueDefinition = inputType.getInputValueDefinitions().get(0); return inputValueDefinition.getDefaultValue(); } catch (Exception e) { return Assert.assertShouldNeverHappen("valueFromAst of '%s' failed because of '%s'", astLiteral, e.getMessage()); } } }
private List<Arg> toArgs(List<InputValueDefinition> defs) { List<Arg> result = new ArrayList<>(); for ( InputValueDefinition def : defs ) { Arg arg = new Arg(def.getName(), toJavaTypeName(def.getType())); arg.graphQLType = toGraphQLType(def.getType()); arg.defaultValue = toJavaValue(def.getDefaultValue()); result.add(arg); } return result; }
private List<Field> getFields(InputObjectTypeDefinition def) { List<Field> fields = new ArrayList<Field>(); for ( InputValueDefinition fieldDef : def.getInputValueDefinitions() ) { Field field = new Field(fieldDef.getName(), toJavaTypeName(fieldDef.getType())); field.graphQLType = toGraphQLType(fieldDef.getType()); field.defaultValue = toJavaValue(fieldDef.getDefaultValue()); fields.add(field); } return fields; }
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(); }
private NodePrinter<InputValueDefinition> inputValueDefinition() { String nameTypeSep = compactMode ? ":" : ": "; String defaultValueEquals = compactMode ? "=" : "= "; return (out, node) -> { Value defaultValue = node.getDefaultValue(); out.printf("%s", comments(node)); out.printf("%s", spaced( node.getName() + nameTypeSep + type(node.getType()), wrap(defaultValueEquals, defaultValue, ""), directives(node.getDirectives()) ) ); }; }
private Builder(InputValueDefinition existing) { this.sourceLocation = existing.getSourceLocation(); this.comments = existing.getComments(); this.name = existing.getName(); this.type = existing.getType(); this.defaultValue = existing.getDefaultValue(); this.description = existing.getDescription(); this.directives = existing.getDirectives(); }
private void checkArgInputObjectValueFieldMatchesAllowedDefinition(List<GraphQLError> errors, ObjectField objectField, InputValueDefinition allowedValueDef) { if (objectField != null) { checkArgValueMatchesAllowedType(errors, objectField.getValue(), allowedValueDef.getType()); return; } // check if field definition is required and has no default value if (allowedValueDef.getType() instanceof NonNullType && allowedValueDef.getDefaultValue() == null) { addValidationError(errors, MISSING_REQUIRED_FIELD_MESSAGE, allowedValueDef.getName()); } // other cases are // - field definition is marked as non-null but has a default value, so the default value can be used // - field definition is nullable hence null can be used }
private GraphQLInputObjectField buildInputField(BuildContext buildCtx, InputValueDefinition fieldDef) { buildCtx.enterNode(fieldDef); GraphQLInputObjectField.Builder fieldBuilder = GraphQLInputObjectField.newInputObjectField(); fieldBuilder.definition(fieldDef); fieldBuilder.name(fieldDef.getName()); fieldBuilder.description(schemaGeneratorHelper.buildDescription(fieldDef, fieldDef.getDescription())); // currently the spec doesnt allow deprecations on InputValueDefinitions but it should! //fieldBuilder.deprecate(buildDeprecationReason(fieldDef.getDirectives())); GraphQLInputType inputType = buildInputType(buildCtx, fieldDef.getType()); fieldBuilder.type(inputType); Value defaultValue = fieldDef.getDefaultValue(); if (defaultValue != null) { fieldBuilder.defaultValue(schemaGeneratorHelper.buildValue(defaultValue, inputType)); } fieldBuilder.withDirectives( buildDirectives(fieldDef.getDirectives(), emptyList(), INPUT_FIELD_DEFINITION, buildCtx.getDirectiveDefinitions()) ); GraphQLInputObjectField inputObjectField = fieldBuilder.build(); inputObjectField = directiveBehaviour.onInputObjectField(inputObjectField, buildCtx.mkBehaviourParams()); return buildCtx.exitNode(inputObjectField); }
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); }
Value oldValue = oldArg.getDefaultValue(); Value newValue = newArg.getDefaultValue(); if (oldValue != null && newValue != null) { if (!oldValue.getClass().equals(newValue.getClass())) {