/** * This helps you transform the current GraphQLObjectType into another one by starting a builder with all * the current values and allows you to transform it how you want. * * @param builderConsumer the consumer code that will be given a builder to transform * * @return a new object based on calling build on that builder */ public GraphQLObjectType transform(Consumer<Builder> builderConsumer) { Builder builder = newObject(this); builderConsumer.accept(builder); return builder.build(); }
public Builder(GraphQLObjectType existing) { name = existing.getName(); description = existing.getDescription(); definition = existing.getDefinition(); fields.putAll(getByName(existing.getFieldDefinitions(), GraphQLFieldDefinition::getName)); interfaces.putAll(getByName(existing.getInterfaces(), GraphQLType::getName)); directives.putAll(getByName(existing.getDirectives(), GraphQLDirective::getName)); }
private void checkObjectImplementsInterface(GraphQLObjectType objectType, GraphQLInterfaceType interfaceType, SchemaValidationErrorCollector validationErrorCollector) { List<GraphQLFieldDefinition> fieldDefinitions = interfaceType.getFieldDefinitions(); for (GraphQLFieldDefinition interfaceFieldDef : fieldDefinitions) { GraphQLFieldDefinition objectFieldDef = objectType.getFieldDefinition(interfaceFieldDef.getName()); if (objectFieldDef == null) { validationErrorCollector.addError( error(format("object type '%s' does not implement interface '%s' because field '%s' is missing", objectType.getName(), interfaceType.getName(), interfaceFieldDef.getName()))); } else { checkFieldTypeCompatibility(objectType, interfaceType, validationErrorCollector, interfaceFieldDef, objectFieldDef); } } }
@Override public TraversalControl visitGraphQLObjectType(GraphQLObjectType node, TraverserContext<GraphQLType> context) { node.replaceInterfaces(node.getInterfaces().stream() .map(type -> (GraphQLOutputType) typeMap.get(type.getName())) .collect(Collectors.toList())); return super.visitGraphQLObjectType(node, context); }
private TypePrinter<GraphQLObjectType> objectPrinter() { return (out, type, visibility) -> { if (isIntrospectionType(type)) { return; } printComments(out, type, ""); if (type.getInterfaces().isEmpty()) { out.format("type %s%s {\n", type.getName(), directivesString(type.getDirectives())); } else { Stream<String> interfaceNames = type.getInterfaces() .stream() .map(GraphQLType::getName) .sorted(Comparator.naturalOrder()); out.format("type %s implements %s%s {\n", type.getName(), interfaceNames.collect(joining(" & ")), directivesString(type.getDirectives())); } visibility.getFieldDefinitions(type) .stream() .sorted(Comparator.comparing(GraphQLFieldDefinition::getName)) .forEach(fd -> { printComments(out, fd, " "); out.format(" %s%s: %s%s\n", fd.getName(), argsString(fd.getArguments()), typeString(fd.getType()), directivesString(fd.getDirectives())); }); out.format("}\n\n"); }; }
@Override public GraphQLInputObjectType getInputObject(GraphQLObjectType graphQLType, String newNamePrefix) { GraphQLObjectType object = graphQLType; return new GraphQLInputObjectType("" + newNamePrefix + object.getName(), object.getDescription(), object.getFieldDefinitions().stream(). map(field -> { GraphQLOutputType type = field.getType(); GraphQLInputType inputType; if (type instanceof GraphQLObjectType) { inputType = getInputObject((GraphQLObjectType) type, newNamePrefix); } else { inputType = (GraphQLInputType) type; } return new GraphQLInputObjectField(field.getName(), field.getDescription(), inputType, null); }). collect(Collectors.toList())); }
private static Object fakeObjectValue(GraphQLObjectType fieldType) { Map<String, Object> map = new LinkedHashMap<>(); fieldType.getFieldDefinitions().forEach(fldDef -> { GraphQLOutputType innerFieldType = fldDef.getType(); Object obj = null; if (innerFieldType instanceof GraphQLObjectType) { obj = fakeObjectValue((GraphQLObjectType) innerFieldType); } else if (innerFieldType instanceof GraphQLScalarType) { obj = fakeScalarValue(fldDef.getName(), (GraphQLScalarType) innerFieldType); } map.put(fldDef.getName(), obj); }); return map; }
@Test public void testIgnoredFields() { IGraphQLObjectMapper graphQLObjectMapper = newGraphQLObjectMapper(Optional.<ITypeNamingStrategy> absent()); GraphQLObjectType type = (GraphQLObjectType) graphQLObjectMapper.getOutputType(TestIgnoredFields.class); assertEquals(TestIgnoredFields.class.getSimpleName(), type.getName()); assertEquals(1, type.getFieldDefinitions().size()); assertNull(type.getFieldDefinition("keyValueStore")); assertNotNull(type.getFieldDefinition("stringField")); assertEquals("stringField", type.getFieldDefinition("stringField").getName()); assertEquals(Scalars.GraphQLString, type.getFieldDefinition("stringField").getType()); }
private GraphQLObjectType buildObjectType(BuildContext buildCtx, ObjectTypeDefinition typeDefinition) { buildCtx.enterNode(typeDefinition); GraphQLObjectType.Builder builder = GraphQLObjectType.newObject(); builder.definition(typeDefinition); builder.name(typeDefinition.getName()); objectType = objectType.transform(objBuilder -> objBuilder.field(fieldDefinition));
public Builder possibleType(GraphQLObjectType type) { assertNotNull(type, "possible type can't be null"); types.put(type.getName(), type); return this; }
public GraphQLObjectTypeWrapper(Class<?> objectClass, GraphQLObjectType objectType) { super(objectType.getName(), objectType.getDescription(), objectType.getFieldDefinitions(), objectType.getInterfaces()); this.objectClass = objectClass; }
public static boolean isRelayConnectionType(GraphQLType type) { return type instanceof GraphQLObjectType && !type.getName().equals(CONNECTION) && type.getName().endsWith(CONNECTION) && ((GraphQLObjectType) type).getFieldDefinition(EDGES) != null && ((GraphQLObjectType) type).getFieldDefinition(PAGE_INFO) != null; }
@Test public void defaults() { GraphQLObjectType object = GraphQLAnnotations.object(TestDefaults.class); assertEquals(object.getName(), "TestDefaults"); assertNull(object.getDescription()); }
@Override public boolean equals(Object obj) { return obj instanceof GraphQLObjectType && ((GraphQLObjectType) obj).getName().contentEquals(getName()) && ((GraphQLObjectType) obj).getFieldDefinitions().equals(getFieldDefinitions()); } }
@Test public void testDocumentedAttributes() { IGraphQLObjectMapper graphQLObjectMapper = newGraphQLObjectMapper( ImmutableList.<IGraphQLTypeMapper> builder().addAll(GraphQLSchemaBuilder.getDefaultTypeMappers()).build()); GraphQLObjectType objectType = (GraphQLObjectType) graphQLObjectMapper.getOutputType(new TypeToken<TestDocumentationClass>() { }.getType()); assertNotNull(objectType.getFieldDefinition("field")); assertEquals("The class description",objectType.getDescription()); assertEquals("The field description", objectType.getFieldDefinition("field").getDescription()); assertEquals("The method description", objectType.getFieldDefinition("value").getDescription()); }
@Test public void interfaceInheritance() { GraphQLObjectType object = GraphQLAnnotations.object(IfaceImpl.class); assertEquals(object.getFieldDefinitions().size(), 1); assertEquals(object.getFieldDefinition("field").getType(), GraphQLString); }
unwrappedQueryType = buildContext.typeCache.resolveType(unwrappedQueryType.getName()); if (unwrappedQueryType instanceof GraphQLObjectType && ((GraphQLObjectType) unwrappedQueryType).getInterfaces().contains(node)) { nodeQueriesByType.put(unwrappedQueryType.getName(), query.getName()); } else if (unwrappedQueryType instanceof GraphQLInterfaceType) { typeRegistry.getOutputTypes(unwrappedQueryType.getName()).stream() .map(MappedType::getAsObjectType) .filter(implementation -> implementation.getInterfaces().contains(node)) .forEach(nodeType -> nodeQueriesByType.putIfAbsent(nodeType.getName(), query.getName())); //never override more precise resolvers } else if (unwrappedQueryType instanceof GraphQLUnionType) { typeRegistry.getOutputTypes(unwrappedQueryType.getName()).stream() .map(MappedType::getAsObjectType) .filter(implementation -> implementation.getInterfaces().contains(node)) .filter(Directives::isMappedType) .forEach(nodeType -> nodeQueriesByType.putIfAbsent(nodeType.getName(), query.getName())); //never override more precise resolvers
boolean objectImplementsInterface(GraphQLInterfaceType interfaceType, GraphQLObjectType objectType) { return objectType.getInterfaces().contains(interfaceType); }
private static GraphQLObjectType getObjectType(final graphql.schema.GraphQLObjectType type){ final String name = type.getName(); if(REFUSED_OBJECT_TYPES.contains(name)){ return null; } final String id = generateId(); final GraphQLObjectType graphQLObjectType = new GraphQLObjectType(); graphQLObjectType.setName(name); graphQLObjectType.setId(id); graphQLObjectType.setDescription(type.getDescription()); for(GraphQLFieldDefinition definition : type.getFieldDefinitions()){ GraphQLAttribute attribute = getAttribute(definition); graphQLObjectType.getAttributes().add(attribute); } return graphQLObjectType; }
@Override public String[] getMutations() { return getInvocationInputFactory().getSchemaProvider().getSchema().getMutationType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new); }