Java Code Examples for graphql.schema.GraphQLFieldDefinition#Builder

The following examples show how to use graphql.schema.GraphQLFieldDefinition#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Bootstrap.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
private GraphQLFieldDefinition createGraphQLFieldDefinitionFromField(String ownerName, Field field) {
    GraphQLFieldDefinition.Builder fieldBuilder = GraphQLFieldDefinition.newFieldDefinition()
            .name(field.getName())
            .description(field.getDescription());

    // Type
    fieldBuilder = fieldBuilder.type(createGraphQLOutputType(field));

    GraphQLFieldDefinition graphQLFieldDefinition = fieldBuilder.build();

    // DataFetcher
    PropertyDataFetcher datafetcher = new PropertyDataFetcher(field);
    codeRegistryBuilder.dataFetcher(FieldCoordinates.coordinates(ownerName,
            graphQLFieldDefinition.getName()), datafetcher);

    return graphQLFieldDefinition;
}
 
Example 2
Source File: OperationMapper.java    From graphql-spqr with Apache License 2.0 6 votes vote down vote up
private GraphQLFieldDefinition toGraphQLField(Operation operation, BuildContext buildContext) {
    GraphQLOutputType type = toGraphQLType(operation.getJavaType(), new TypeMappingEnvironment(operation.getTypedElement(), this, buildContext));
    GraphQLFieldDefinition.Builder fieldBuilder = newFieldDefinition()
            .name(operation.getName())
            .description(operation.getDescription())
            .deprecate(operation.getDeprecationReason())
            .type(type)
            .withDirective(Directives.mappedOperation(operation))
            .withDirectives(toGraphQLDirectives(operation.getTypedElement(), buildContext.directiveBuilder::buildFieldDefinitionDirectives, buildContext));

    List<GraphQLArgument> arguments = operation.getArguments().stream()
            .filter(OperationArgument::isMappable)
            .map(argument -> toGraphQLArgument(argument, buildContext))
            .collect(Collectors.toList());
    fieldBuilder.arguments(arguments);
    if (GraphQLUtils.isRelayConnectionType(type) && buildContext.relayMappingConfig.strictConnectionSpec) {
        validateConnectionSpecCompliance(operation.getName(), arguments, buildContext.relay);
    }

    return buildContext.transformers.transform(fieldBuilder.build(), operation, this, buildContext);
}
 
Example 3
Source File: InputFieldFactory.java    From engine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void createField(final Document contentTypeDefinition, final Node contentTypeField,
                        final String contentTypeFieldId, final String parentGraphQLTypeName,
                        final GraphQLObjectType.Builder parentGraphQLType, final String graphQLFieldName,
                        final GraphQLFieldDefinition.Builder graphQLField) {
    if (Boolean.parseBoolean(XmlUtils.selectSingleNodeValue(contentTypeField, tokenizeXPath))) {
        // Add the tokenized field as string with text filters
        parentGraphQLType.field(GraphQLFieldDefinition.newFieldDefinition()
            .name(StringUtils.substringBeforeLast(graphQLFieldName, FIELD_SEPARATOR) + FIELD_SUFFIX_TOKENIZED)
            .description("Tokenized version of " + contentTypeFieldId)
            .type(GraphQLString)
            .argument(TEXT_FILTER));
    }

    // Add the original according to the postfix
    setTypeFromFieldName(contentTypeFieldId, graphQLField);
}
 
Example 4
Source File: RTEFieldFactory.java    From engine with GNU General Public License v3.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void createField(final Document contentTypeDefinition, final Node contentTypeField,
                        final String contentTypeFieldId, final String parentGraphQLTypeName,
                        final GraphQLObjectType.Builder parentGraphQLType, final String graphQLFieldName,
                        final GraphQLFieldDefinition.Builder graphQLField) {
    // Add the copy field as string without filters
    parentGraphQLType.field(GraphQLFieldDefinition.newFieldDefinition()
        .name(getGraphQLName(graphQLFieldName) + FIELD_SUFFIX_RAW)
        .description(XmlUtils.selectSingleNodeValue(contentTypeField, titleXPath))
        .type(GraphQLString));

    // Add the original as string with text filters
    graphQLField.type(GraphQLString);
    graphQLField.argument(TEXT_FILTER);
}
 
Example 5
Source File: Bootstrap.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private GraphQLFieldDefinition createGraphQLFieldDefinitionFromOperation(String operationTypeName, Operation operation) {
    // Fields
    GraphQLFieldDefinition.Builder fieldBuilder = GraphQLFieldDefinition.newFieldDefinition()
            .name(operation.getName())
            .description(operation.getDescription());

    // Return field
    fieldBuilder = fieldBuilder.type(createGraphQLOutputType(operation));

    // Arguments
    if (operation.hasArguments()) {
        fieldBuilder = fieldBuilder.arguments(createGraphQLArguments(operation.getArguments()));
    }

    GraphQLFieldDefinition graphQLFieldDefinition = fieldBuilder.build();

    // DataFetcher
    Collection<DataFetcherDecorator> decorators = new ArrayList<>();
    if (config != null && config.isMetricsEnabled()) {
        decorators.add(new MetricDecorator());
    }
    if (config != null && config.isTracingEnabled()) {
        decorators.add(new OpenTracingDecorator());
    }
    if (config != null && config.isValidationEnabled() && operation.hasArguments()) {
        decorators.add(new ValidationDecorator());
    }

    DataFetcher<?> datafetcher;
    if (operation.isAsync()) {
        datafetcher = new AsyncDataFetcher(operation, decorators);
    } else {
        datafetcher = new ReflectionDataFetcher(operation, decorators);
    }

    codeRegistryBuilder.dataFetcher(FieldCoordinates.coordinates(operationTypeName,
            graphQLFieldDefinition.getName()), datafetcher);

    return graphQLFieldDefinition;
}
 
Example 6
Source File: ProtoToGql.java    From rejoiner with Apache License 2.0 5 votes vote down vote up
private static GraphQLFieldDefinition convertField(
    FieldDescriptor fieldDescriptor, SchemaOptions schemaOptions) {
  DataFetcher<?> dataFetcher = new ProtoDataFetcher(fieldDescriptor);
  GraphQLFieldDefinition.Builder builder =
      newFieldDefinition()
          .type(convertType(fieldDescriptor, schemaOptions))
          .dataFetcher(dataFetcher)
          .name(fieldDescriptor.getJsonName());
  builder.description(schemaOptions.commentsMap().get(fieldDescriptor.getFullName()));
  if (fieldDescriptor.getOptions().hasDeprecated()
      && fieldDescriptor.getOptions().getDeprecated()) {
    builder.deprecate("deprecated in proto");
  }
  return builder.build();
}
 
Example 7
Source File: FieldsGenerator.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
@Override
public List<GraphQLFieldDefinition> getOutputFields(Object object) {
    List<GraphQLFieldDefinition> fieldDefs = new ArrayList<GraphQLFieldDefinition>();
    List<Object> fieldObjects = getFieldRepresentativeObjects(object);
    if (fieldObjects == null) {
        return fieldDefs;
    }

    Set<String> fieldNames = new HashSet<String>();
    for (Object field : fieldObjects) {
        GraphQLFieldDefinition.Builder fieldBuilder =
                getOutputFieldDefinition(field);
        if (fieldBuilder == null) {
            continue;
        }
        
        GraphQLFieldDefinition fieldDef = fieldBuilder.build();
        //check for shadowed fields, where field "item" in superclass
        //is shadowed by field "item" in subclass
        if (fieldNames.contains(fieldDef.getName())) {
            continue;
        }
        fieldNames.add(fieldDef.getName());
        fieldDefs.add(fieldDef);
    }
    return fieldDefs;
}
 
Example 8
Source File: FieldsGenerator.java    From graphql-java-type-generator with MIT License 5 votes vote down vote up
/**
 * May return null should this field be disallowed
 * @param object
 * @return
 */
protected GraphQLFieldDefinition.Builder getOutputFieldDefinition(
        final Object object) {
    String fieldName = getFieldName(object);
    GraphQLOutputType fieldType = (GraphQLOutputType)
            getTypeOfField(object, TypeKind.OBJECT);
    if (fieldName == null || fieldType == null) {
        return null;
    }
    Object fieldFetcher = getFieldFetcher(object);
    String fieldDescription  = getFieldDescription(object);
    String fieldDeprecation  = getFieldDeprecation(object);
    List<GraphQLArgument> fieldArguments  = getFieldArguments(object);
    logger.debug("GraphQL field will be of type [{}] and name [{}] and fetcher [{}] with description [{}]",
            fieldType, fieldName, fieldFetcher, fieldDescription);
    
    GraphQLFieldDefinition.Builder fieldBuilder = newFieldDefinition()
            .name(fieldName)
            .type(fieldType)
            .description(fieldDescription)
            .deprecate(fieldDeprecation);
    if (fieldArguments != null) {
        fieldBuilder.argument(fieldArguments);
    }
    if (fieldFetcher instanceof DataFetcher) {
        fieldBuilder.dataFetcher((DataFetcher)fieldFetcher);
    }
    else if (fieldFetcher != null) {
        fieldBuilder.staticValue(fieldFetcher);
    }
    return fieldBuilder;
}
 
Example 9
Source File: TimeFieldFactory.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void createField(final Document contentTypeDefinition, final Node contentTypeField,
                        final String contentTypeFieldId, final String parentGraphQLTypeName,
                        final GraphQLObjectType.Builder parentGraphQLType, final String graphQLFieldName,
                        final GraphQLFieldDefinition.Builder graphQLField) {
    // Add the timezone field as text
    parentGraphQLType.field(GraphQLFieldDefinition.newFieldDefinition()
        .name(getGraphQLName(graphQLFieldName) + FIELD_SUFFIX_TZ)
        .description("Time Zone for field " + contentTypeFieldId)
        .type(GraphQLString)
        .argument(TEXT_FILTER));

    // Add the original according to the suffix
    setTypeFromFieldName(contentTypeFieldId, graphQLField);
}
 
Example 10
Source File: CheckboxFieldFactory.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void createField(final Document contentTypeDefinition, final Node contentTypeField,
                        final String contentTypeFieldId, final String parentGraphQLTypeName,
                        final GraphQLObjectType.Builder parentGraphQLType, final String graphQLFieldName,
                        final GraphQLFieldDefinition.Builder graphQLField) {
    graphQLField.type(GraphQLBoolean);
    graphQLField.argument(BOOLEAN_FILTER);
}
 
Example 11
Source File: RepeatGroupFieldFactory.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void createField(final Document contentTypeDefinition, final Node contentTypeField,
                        final String contentTypeFieldId, final String parentGraphQLTypeName,
                        final GraphQLObjectType.Builder parentGraphQLType, final String graphQLFieldName,
                        final GraphQLFieldDefinition.Builder graphQLField) {
    // For Repeating Groups we need to create a wrapper type and do everything all over
    GraphQLObjectType.Builder repeatType = GraphQLObjectType.newObject()
        .name(parentGraphQLTypeName + FIELD_SEPARATOR + graphQLFieldName + FIELD_SUFFIX_ITEM)
        .description("Item for repeat group of " + contentTypeFieldId);

    List<Node> fields =
        XmlUtils.selectNodes(contentTypeField, fieldsXPath, Collections.emptyMap());

    // Call recursively for all fields in the repeating group
    if (CollectionUtils.isNotEmpty(fields)) {
        fields.forEach(f -> typeFactory.createField(contentTypeDefinition, f, parentGraphQLTypeName, repeatType));
    }

    GraphQLObjectType wrapperType = GraphQLObjectType.newObject()
        .name(parentGraphQLTypeName + FIELD_SEPARATOR + graphQLFieldName + FIELD_SUFFIX_ITEMS)
        .description("Wrapper for list of items of " + contentTypeFieldId)
        .field(GraphQLFieldDefinition.newFieldDefinition()
            .name(FIELD_NAME_ITEM)
            .description("List of items of " + contentTypeFieldId)
            .type(list(repeatType.build())))
        .build();

    graphQLField.type(wrapperType);
}
 
Example 12
Source File: SchemaCustomizer.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
public FieldBuilder(final String typeName, final GraphQLFieldDefinition.Builder field,
                    final DataFetcher<?> fetcher) {
    this.typeName = typeName;
    this.field = field;
    this.fetcher = fetcher;
}
 
Example 13
Source File: CheckboxGroupFieldFactory.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@SuppressWarnings("unchecked")
@Override
public void createField(final Document contentTypeDefinition, final Node contentTypeField,
                        final String contentTypeFieldId, final String parentGraphQLTypeName,
                        final GraphQLObjectType.Builder parentGraphQLType, final String graphQLFieldName,
                        final GraphQLFieldDefinition.Builder graphQLField) {
    String datasourceName = XmlUtils.selectSingleNodeValue(contentTypeField, datasourceNameXPath);
    String datasourceSettings = XmlUtils.selectSingleNodeValue(
            contentTypeDefinition, String.format(datasourceSettingsXPathFormat, datasourceName));
    String datasourceType = null;
    String datasourceSuffix = null;

    try {
        if(StringUtils.isNotEmpty(datasourceSettings)) {
            List<Map<String, Object>> typeSetting = objectMapper.readValue(datasourceSettings, List.class);
            Optional<Map<String, Object>> selectedType =
                typeSetting.stream()
                           .filter(s -> (Boolean)s.get(FIELD_NAME_SELECTED)).findFirst();
            if (selectedType.isPresent()) {
                datasourceType = selectedType.get().get(FIELD_NAME_VALUE).toString();
                datasourceSuffix = StringUtils.substringAfter(datasourceType, FIELD_SEPARATOR);
            }
        }
    } catch (IOException e) {
        logger.warn("Error checking data source type for '{}'", contentTypeFieldId);
    }

    String valueKey = FIELD_NAME_VALUE;
    if (StringUtils.isNotEmpty(datasourceSuffix)) {
        valueKey += FIELD_SEPARATOR + datasourceSuffix + FIELD_SUFFIX_MULTIVALUE;
    }

    GraphQLFieldDefinition.Builder valueField = GraphQLFieldDefinition.newFieldDefinition()
        .name(valueKey)
        .description("The value of the item");

    if (StringUtils.isNotEmpty(datasourceType)) {
        setTypeFromFieldName(datasourceType, valueField);
    } else {
        valueField.type(GraphQLString);
        valueField.argument(TEXT_FILTER);
    }

    GraphQLObjectType itemType = GraphQLObjectType.newObject()
        .name(parentGraphQLTypeName + FIELD_SEPARATOR + graphQLFieldName + FIELD_SUFFIX_ITEM)
        .description("Item for field " + contentTypeFieldId)
        .field(GraphQLFieldDefinition.newFieldDefinition()
            .name(FIELD_NAME_KEY)
            .description("The key of the item")
            .type(GraphQLString)
            .argument(TEXT_FILTER))
        .field(valueField)
        .build();

    GraphQLObjectType itemWrapper = GraphQLObjectType.newObject()
        .name(parentGraphQLTypeName + FIELD_SEPARATOR + graphQLFieldName + FIELD_SUFFIX_ITEMS)
        .description("Wrapper for field " + contentTypeFieldId)
        .field(GraphQLFieldDefinition.newFieldDefinition()
            .name(FIELD_NAME_ITEM)
            .description("List of items for field " + contentTypeFieldId)
            .type(list(itemType)))
        .build();

    graphQLField.type(itemWrapper);
}
 
Example 14
Source File: NodeSelectorFieldFactory.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void createField(final Document contentTypeDefinition, final Node contentTypeField,
                        final String contentTypeFieldId, final String parentGraphQLTypeName,
                        final GraphQLObjectType.Builder parentGraphQLType, final String graphQLFieldName,
                        final GraphQLFieldDefinition.Builder graphQLField) {
    boolean disableFlattening = BooleanUtils.toBoolean(
            XmlUtils.selectSingleNodeValue(contentTypeField, disableFlatteningXPath));

    if (disableFlattening) {
        // Flattening is disabled, so use the generic item include type
        logger.debug("Flattening is disabled for node selector '{}'. Won't generate additional schema " +
            "types and fields for its items", graphQLFieldName);

        graphQLField.type(ITEM_INCLUDE_WRAPPER_TYPE);
        return;
    }

    String datasourceName = XmlUtils.selectSingleNodeValue(contentTypeField, datasourceNameXPath);
    String itemType = XmlUtils.selectSingleNodeValue(
        contentTypeDefinition, String.format(datasourceItemTypeXPathFormat, datasourceName));
    String itemGraphQLType = StringUtils.isNotEmpty(itemType)? getGraphQLName(itemType) : null;

    if (StringUtils.isEmpty(itemGraphQLType)) {
        // If there is no item content-type set in the datasource, use the generic item include type
        logger.debug("No specific item type found for node selector '{}'. Won't generate additional schema " +
            "types and fields for its items", graphQLFieldName);

        graphQLField.type(CONTENT_INCLUDE_WRAPPER_TYPE);
    } else {
        // If there is an item content-type, then create a specific GraphQL type for it
        logger.debug("Item type found for node selector '{}': '{}'. Generating additional schema types and " +
            "fields for the items...", itemGraphQLType, graphQLFieldName);

        GraphQLObjectType flattenedType = GraphQLObjectType.newObject()
            .name(parentGraphQLTypeName + FIELD_SEPARATOR + graphQLFieldName + "_flattened_item")
            .description("Contains the data from another item in the site")
            .field(GraphQLFieldDefinition.newFieldDefinition()
                .name(FIELD_NAME_VALUE)
                .description("The name of the item")
                .type(nonNull(GraphQLString)))
            .field(GraphQLFieldDefinition.newFieldDefinition()
                .name(FIELD_NAME_KEY)
                .description("The path of the item")
                .type(nonNull(GraphQLString)))
            .field(GraphQLFieldDefinition.newFieldDefinition()
                .name(FIELD_NAME_COMPONENT)
                .description("The content of the item")
                .type(GraphQLTypeReference.typeRef(itemGraphQLType)))
            .build();

        GraphQLObjectType wrapperType = GraphQLObjectType.newObject()
            .name(parentGraphQLTypeName + FIELD_SEPARATOR + graphQLFieldName + FIELD_SUFFIX_ITEMS)
            .description("Wrapper for flattened items")
            .field(GraphQLFieldDefinition.newFieldDefinition()
                .name(FIELD_NAME_ITEM)
                .description("List of flattened items")
                .type(list(nonNull(flattenedType))))
            .build();

        graphQLField.type(wrapperType);
    }
}
 
Example 15
Source File: GraphQLFieldFactory.java    From engine with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Adds all the required objects for a content-type field to a {@link GraphQLObjectType}
 *
 * @param contentTypeDefinition the XML document with the content type definition
 * @param contentTypeField      the XML node with the content-type field
 * @param contentTypeFieldId    the content-type field ID
 * @param parentGraphQLTypeName the field's parent GraphQL type name
 * @param parentGraphQLType     the field's parent {@link GraphQLObjectType}
 * @param graphQLFieldName      the field's GraphQL-friendly name
 * @param graphQLField          the field's {@link GraphQLFieldDefinition}
 */
void createField(Document contentTypeDefinition, Node contentTypeField, String contentTypeFieldId,
                 String parentGraphQLTypeName, GraphQLObjectType.Builder parentGraphQLType,
                 String graphQLFieldName, GraphQLFieldDefinition.Builder graphQLField);