graphql.language.ObjectTypeDefinition Java Examples
The following examples show how to use
graphql.language.ObjectTypeDefinition.
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: GraphQLSchemaDefinition.java From carbon-apimgt with Apache License 2.0 | 6 votes |
/** * Extract GraphQL Types and Fields from given schema * * @param schema GraphQL Schema * @return list of all types and fields */ public List<GraphqlSchemaType> extractGraphQLTypeList(String schema) { List<GraphqlSchemaType> typeList = new ArrayList<>(); SchemaParser schemaParser = new SchemaParser(); TypeDefinitionRegistry typeRegistry = schemaParser.parse(schema); Map<java.lang.String, TypeDefinition> list = typeRegistry.types(); for (Map.Entry<String, TypeDefinition> entry : list.entrySet()) { if (entry.getValue() instanceof ObjectTypeDefinition) { GraphqlSchemaType graphqlSchemaType = new GraphqlSchemaType(); List<String> fieldList = new ArrayList<>(); graphqlSchemaType.setType(entry.getValue().getName()); for (FieldDefinition fieldDef : ((ObjectTypeDefinition) entry.getValue()).getFieldDefinitions()) { fieldList.add(fieldDef.getName()); } graphqlSchemaType.setFieldList(fieldList); typeList.add(graphqlSchemaType); } } return typeList; }
Example #2
Source File: GraphQLIntrospectionResultToSchema.java From js-graphql-intellij-plugin with MIT License | 6 votes |
@SuppressWarnings("unchecked") ObjectTypeDefinition createObject(Map<String, Object> input) { assertTrue(input.get("kind").equals("OBJECT"), () -> "wrong input"); ObjectTypeDefinition.Builder builder = ObjectTypeDefinition.newObjectTypeDefinition() .name((String) input.get("name")) .description(getDescription(input)); if (input.containsKey("interfaces")) { builder.implementz( ((List<Map<String, Object>>) input.get("interfaces")).stream() .map(this::createTypeIndirection) .collect(Collectors.toList()) ); } List<Map<String, Object>> fields = (List<Map<String, Object>>) input.get("fields"); builder.fieldDefinitions(createFields(fields)); return builder.build(); }
Example #3
Source File: GqlParentType.java From manifold with Apache License 2.0 | 6 votes |
private List<? extends NamedNode> getDefinitions( Definition def ) { if( def instanceof OperationDefinition ) { return ((OperationDefinition)def).getVariableDefinitions(); } if( def instanceof InputObjectTypeDefinition ) { return ((InputObjectTypeDefinition)def).getInputValueDefinitions(); } if( def instanceof ObjectTypeDefinition ) { return ((ObjectTypeDefinition)def).getFieldDefinitions(); } throw new IllegalStateException(); }
Example #4
Source File: GqlParentType.java From manifold with Apache License 2.0 | 6 votes |
private void addObjectExtensions( ObjectTypeDefinition type, SrcLinkedClass srcClass ) { List<FieldDefinition> baseFieldDefinitions = type.getFieldDefinitions(); List<ObjectTypeExtensionDefinition> objectExtensions = _registry.objectTypeExtensions().get( type.getName() ); if( objectExtensions != null ) { for( ObjectTypeExtensionDefinition ext: objectExtensions ) { List<FieldDefinition> extFieldDefinitions = ext.getFieldDefinitions(); for( FieldDefinition member: extFieldDefinitions ) { addMember( srcClass, member, name -> baseFieldDefinitions.stream().anyMatch( f -> f.getName().equals( name ) ) || extFieldDefinitions.stream().anyMatch( f -> f.getName().equals( name ) ) ); } } } }
Example #5
Source File: STModel.java From graphql-apigen with Apache License 2.0 | 6 votes |
public synchronized List<Field> getFields() { if ( null == fields ) { Definition def = typeEntry.getDefinition(); if ( isObjectType() ) { fields = getFields((ObjectTypeDefinition)def); } else if ( isInterfaceType() ) { fields = getFields((InterfaceTypeDefinition)def); } else if ( isInputObjectType() ) { fields = getFields((InputObjectTypeDefinition)def); } else if ( isUnionType() ) { fields = getFields((UnionTypeDefinition)def); } else if ( isEnumType() ) { fields = getFields((EnumTypeDefinition)def); } else if ( isSchemaType() ) { fields = getFields((SchemaDefinition)def); } else { fields = Collections.emptyList(); } } return fields; }
Example #6
Source File: STModel.java From graphql-apigen with Apache License 2.0 | 6 votes |
public synchronized List<String> getImports() { if ( null == imports ) { Definition def = typeEntry.getDefinition(); Set<String> names = new TreeSet<String>(); if ( isObjectType() ) { addImports(names, (ObjectTypeDefinition)def); } else if ( isInterfaceType() ) { addImports(names, (InterfaceTypeDefinition)def); } else if ( isInputObjectType() ) { addImports(names, (InputObjectTypeDefinition)def); } else if ( isUnionType() ) { addImports(names, (UnionTypeDefinition)def); } else if ( isEnumType() ) { addImports(names, (EnumTypeDefinition)def); } else if ( isSchemaType() ) { addImports(names, (SchemaDefinition)def); } imports = new ArrayList<>(names); } return imports; }
Example #7
Source File: STModel.java From graphql-apigen with Apache License 2.0 | 6 votes |
public List<Interface> getInterfaces() { interfaces = new ArrayList<>(); if (!isObjectType()) { return interfaces; } ObjectTypeDefinition objectTypeDefinition = (ObjectTypeDefinition) typeEntry.getDefinition(); List<Type> interfaceTypes = objectTypeDefinition.getImplements(); for (Type anInterfaceType : interfaceTypes) { Interface anInterface = new Interface(); anInterface.type = toJavaTypeName(anInterfaceType); interfaces.add(anInterface); } return interfaces; }
Example #8
Source File: TypeEntry.java From graphql-apigen with Apache License 2.0 | 6 votes |
private static List<Directive> getDirectives(Definition def) { if ( def instanceof ObjectTypeDefinition ) { return ((ObjectTypeDefinition)def).getDirectives(); } if ( def instanceof InterfaceTypeDefinition ) { return ((InterfaceTypeDefinition)def).getDirectives(); } if ( def instanceof EnumTypeDefinition ) { return ((EnumTypeDefinition)def).getDirectives(); } if ( def instanceof ScalarTypeDefinition ) { return ((ScalarTypeDefinition)def).getDirectives(); } if ( def instanceof UnionTypeDefinition ) { return ((UnionTypeDefinition)def).getDirectives(); } if ( def instanceof InputObjectTypeDefinition ) { return ((InputObjectTypeDefinition)def).getDirectives(); } if ( def instanceof SchemaDefinition ) { return ((SchemaDefinition)def).getDirectives(); } return Collections.emptyList(); }
Example #9
Source File: TypeEntry.java From graphql-apigen with Apache License 2.0 | 5 votes |
public boolean hasIdField() { if ( definition instanceof ObjectTypeDefinition) { return ((ObjectTypeDefinition)definition).getFieldDefinitions() .stream() .anyMatch((field) -> "id".equals(field.getName())); } return false; }
Example #10
Source File: GraphQLSchemaDefinition.java From carbon-apimgt with Apache License 2.0 | 5 votes |
/** * * @param entry Entry * @param operationArray operationArray */ private void addOperations(Map.Entry<String, TypeDefinition> entry, List<URITemplate> operationArray) { for (FieldDefinition fieldDef : ((ObjectTypeDefinition) entry.getValue()).getFieldDefinitions()) { URITemplate operation = new URITemplate(); operation.setHTTPVerb(entry.getKey()); operation.setUriTemplate(fieldDef.getName()); operationArray.add(operation); } }
Example #11
Source File: STModel.java From graphql-apigen with Apache License 2.0 | 5 votes |
private List<Field> getFields(ObjectTypeDefinition def) { List<Field> fields = new ArrayList<Field>(); for ( FieldDefinition fieldDef : def.getFieldDefinitions() ) { Field field = new Field(fieldDef.getName(), toJavaTypeName(fieldDef.getType())); field.graphQLType = toGraphQLType(fieldDef.getType()); field.dataResolver = toDataResolver(fieldDef.getType()); field.args = toArgs(fieldDef.getInputValueDefinitions()); fields.add(field); } return fields; }
Example #12
Source File: Federation.java From federation-jvm with MIT License | 5 votes |
private static void ensureQueryTypeExists(TypeDefinitionRegistry typeRegistry) { final String queryName = typeRegistry.schemaDefinition() .flatMap(sdef -> sdef.getOperationTypeDefinitions() .stream() .filter(op -> "query".equals(op.getName())) .findFirst() .map(def -> def.getTypeName().getName())) .orElse("Query"); if (!typeRegistry.getType(queryName).isPresent()) { typeRegistry.add(ObjectTypeDefinition.newObjectTypeDefinition().name(queryName).build()); } }
Example #13
Source File: GqlParentType.java From manifold with Apache License 2.0 | 5 votes |
@SuppressWarnings("unused") private String getStartSymbol( Node node ) { String start = ""; if( node instanceof TypeDefinition ) { if( node instanceof ObjectTypeDefinition ) { start = "type"; } else if( node instanceof EnumTypeDefinition ) { start = "enum"; } else if( node instanceof InputObjectTypeDefinition ) { start = "input"; } else if( node instanceof InterfaceTypeDefinition ) { start = "interface"; } else if( node instanceof ScalarTypeDefinition ) { start = "scalar"; } else if( node instanceof UnionTypeDefinition ) { start = "union"; } } else if( node instanceof OperationDefinition ) { start = ((OperationDefinition)node).getOperation().name().toLowerCase(); } return start; }
Example #14
Source File: GqlParentType.java From manifold with Apache License 2.0 | 5 votes |
private String findLub( UnionTypeDefinition typeDef ) { Set<Set<InterfaceTypeDefinition>> ifaces = new HashSet<>(); for( Type t: typeDef.getMemberTypes() ) { TypeDefinition td = findTypeDefinition( t ); if( td instanceof ObjectTypeDefinition ) { ifaces.add( ((ObjectTypeDefinition)td).getImplements().stream() .map( type -> (InterfaceTypeDefinition)findTypeDefinition( type ) ) .collect( Collectors.toSet() ) ); } else if( td instanceof InterfaceTypeDefinition ) { ifaces.add( Collections.singleton( (InterfaceTypeDefinition)td ) ); } } //noinspection OptionalGetWithoutIsPresent Set<InterfaceTypeDefinition> intersection = ifaces.stream().reduce( ( p1, p2 ) -> { p1.retainAll( p2 ); return p1; } ).get(); if( intersection.isEmpty() ) { // no common interface return null; } else { // can only use one return intersection.iterator().next().getName(); } }
Example #15
Source File: GqlParentType.java From manifold with Apache License 2.0 | 5 votes |
private void addIntersectionMethods( SrcLinkedClass srcClass, UnionTypeDefinition union ) { Set<Pair<String, String>> fieldDefs = new HashSet<>(); Map<String, FieldDefinition> nameToFieldDef = new HashMap<>(); for( Type memberType: union.getMemberTypes() ) { TypeDefinition typeDef = findTypeDefinition( memberType ); if( typeDef instanceof ObjectTypeDefinition ) { if( fieldDefs.isEmpty() ) { fieldDefs.addAll( ((ObjectTypeDefinition)typeDef).getFieldDefinitions() .stream().map( e -> { nameToFieldDef.put( e.getName(), e ); return new Pair<>( e.getName(), e.getType().toString() ); } ).collect( Collectors.toSet() ) ); } else { fieldDefs.retainAll( ((ObjectTypeDefinition)typeDef).getFieldDefinitions() .stream().map( e -> new Pair<>( e.getName(), e.getType().toString() ) ).collect( Collectors.toSet() ) ); } } } fieldDefs.forEach( fieldDef -> addMember( srcClass, null, nameToFieldDef.get( fieldDef.getFirst() ).getType(), fieldDef.getFirst(), name -> fieldDefs.stream().anyMatch( f -> f.getFirst().equals( name ) ) ) ); }
Example #16
Source File: GqlParentType.java From manifold with Apache License 2.0 | 5 votes |
private void addInnerObjectType( ObjectTypeDefinition type, SrcLinkedClass enclosingType ) { String identifier = makeIdentifier( type.getName(), false ); String fqn = getFqn() + '.' + identifier; SrcLinkedClass srcClass = new SrcLinkedClass( fqn, enclosingType, Interface ) .addInterface( IJsonBindingsBacked.class.getSimpleName() ) .addAnnotation( new SrcAnnotationExpression( Structural.class.getSimpleName() ) .addArgument( "factoryClass", Class.class, identifier + ".ProxyFactory.class" ) ) .modifiers( Modifier.PUBLIC ); addUnionInterfaces( type, srcClass ); addActualNameAnnotation( srcClass, type.getName(), false ); addSourcePositionAnnotation( srcClass, type, srcClass ); List<Type> interfaces = type.getImplements(); addInterfaces( srcClass, interfaces ); addProxyFactory( srcClass ); addBuilder( srcClass, type ); addCreateMethod( srcClass, type ); addBuilderMethod( srcClass, type ); addLoadMethod( srcClass ); addCopier( srcClass, type ); addCopierMethod( srcClass ); addCopyMethod( srcClass ); List<FieldDefinition> fieldDefinitions = type.getFieldDefinitions(); for( FieldDefinition member: fieldDefinitions ) { addMember( srcClass, member, name -> fieldDefinitions.stream().anyMatch( f -> f.getName().equals( name ) ) ); } addObjectExtensions( type, srcClass ); enclosingType.addInnerClass( srcClass ); }
Example #17
Source File: GqlParentType.java From manifold with Apache License 2.0 | 5 votes |
private void addInnerTypes( SrcLinkedClass srcClass ) { mapUnionMemberToUnions(); for( TypeDefinition type: _registry.types().values() ) { if( type instanceof ObjectTypeDefinition ) { addInnerObjectType( (ObjectTypeDefinition)type, srcClass ); } else if( type instanceof InterfaceTypeDefinition ) { addInnerInterfaceType( (InterfaceTypeDefinition)type, srcClass ); } else if( type instanceof EnumTypeDefinition ) { addInnerEnumType( (EnumTypeDefinition)type, srcClass ); } else if( type instanceof InputObjectTypeDefinition ) { addInnerInputType( (InputObjectTypeDefinition)type, srcClass ); } else if( type instanceof UnionTypeDefinition ) { addInnerUnionType( (UnionTypeDefinition)type, srcClass ); } } }
Example #18
Source File: Generator.java From smallrye-graphql with Apache License 2.0 | 5 votes |
@Override protected String generateBody() { ObjectTypeDefinition typeDefinition = (ObjectTypeDefinition) schema.getType(typeName) .orElseThrow(() -> new GraphQlGeneratorException("type '" + typeName + "' not found in schema")); return typeDefinition.getFieldDefinitions().stream() .filter(field -> fieldNames.contains(field.getName())) .map(this::toJava) .collect(joining(";\n ", " ", ";\n")); }
Example #19
Source File: Generator.java From smallrye-graphql with Apache License 2.0 | 5 votes |
private JavaType returnType() { ObjectTypeDefinition query = (ObjectTypeDefinition) schema.getType("Query") .orElseThrow(() -> new GraphQlGeneratorException("'Query' type not found in schema")); FieldDefinition fieldDefinition = query.getFieldDefinitions().stream() .filter(f -> f.getName().equals(method.getName())) .findAny() .orElseThrow(() -> new GraphQlGeneratorException("field (method) '" + method.getName() + "' not found in " + query.getFieldDefinitions().stream().map(FieldDefinition::getName).collect(listString()))); List<String> selections = operation.getSelectionSet().getSelectionsOfType(Field.class).stream() .flatMap(this::selectedFields) .map(Field::getName) .collect(toList()); return new JavaType(fieldDefinition.getType(), selections); }
Example #20
Source File: STModel.java From graphql-apigen with Apache License 2.0 | 4 votes |
public boolean isObjectType() { return typeEntry.getDefinition() instanceof ObjectTypeDefinition; }
Example #21
Source File: STModel.java From graphql-apigen with Apache License 2.0 | 4 votes |
private void addImports(Collection<String> imports, ObjectTypeDefinition def) { for ( FieldDefinition fieldDef : def.getFieldDefinitions() ) { addImports(imports, fieldDef.getType()); } }