private void checkTypeExtensionHasCorrespondingType(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, String name, List<? extends TypeDefinition> extTypeList, Class<? extends TypeDefinition> targetClass) { TypeDefinition extensionDefinition = extTypeList.get(0); Optional<? extends TypeDefinition> typeDefinition = typeRegistry.getType(TypeName.newTypeName().name(name).build(), targetClass); if (!typeDefinition.isPresent()) { errors.add(new TypeExtensionMissingBaseTypeError(extensionDefinition)); } }
@SuppressWarnings("unchecked") private void checkTypeExtensionDirectiveRedefinition(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, String name, List<? extends TypeDefinition> extensions, Class<? extends TypeDefinition> targetClass) { Optional<? extends TypeDefinition> typeDefinition = typeRegistry.getType(TypeName.newTypeName().name(name).build(), targetClass); if (typeDefinition.isPresent() && typeDefinition.get().getClass().equals(targetClass)) { List<Directive> directives = typeDefinition.get().getDirectives(); Map<String, Directive> directiveMap = FpKit.getByName(directives, Directive::getName, mergeFirst()); extensions.forEach(typeExt -> { List<Directive> extDirectives = typeExt.getDirectives(); extDirectives.forEach(directive -> { if (directiveMap.containsKey(directive.getName())) { errors.add(new TypeExtensionDirectiveRedefinitionError(typeDefinition.get(), directive)); } }); } ); } }
@SuppressWarnings("unchecked") UnionTypeDefinition createUnion(Map<String, Object> input) { assertTrue(input.get("kind").equals("UNION"), "wrong input"); UnionTypeDefinition.Builder unionTypeDefinition = UnionTypeDefinition.newUnionTypeDefinition(); unionTypeDefinition.name((String) input.get("name")); unionTypeDefinition.comments(toComment((String) input.get("description"))); List<Map<String, Object>> possibleTypes = (List<Map<String, Object>>) input.get("possibleTypes"); for (Map<String, Object> possibleType : possibleTypes) { TypeName typeName = TypeName.newTypeName().name((String) possibleType.get("name")).build(); unionTypeDefinition.memberType(typeName); } return unionTypeDefinition.build(); }
private Optional<OperationTypeDefinition> synthOperationTypeDefinition(Function<Type, Optional<ObjectTypeDefinition>> typeReteriver, String opName) { TypeName type = TypeName.newTypeName().name(capitalize(opName)).build(); Optional<ObjectTypeDefinition> typeDef = typeReteriver.apply(type); return typeDef.map(objectTypeDefinition -> OperationTypeDefinition.newOperationTypeDefinition().name(opName).typeName(type).build()); }
/** * We build the query / mutation / subscription path as a tree of referenced types * but then we build the rest of the types specified and put them in as additional types * * @param buildCtx the context we need to work out what we are doing * * @return the additional types not referenced from the top level operations */ private Set<GraphQLType> buildAdditionalTypes(BuildContext buildCtx) { Set<GraphQLType> additionalTypes = new LinkedHashSet<>(); TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry(); typeRegistry.types().values().forEach(typeDefinition -> { TypeName typeName = TypeName.newTypeName().name(typeDefinition.getName()).build(); if (typeDefinition instanceof InputObjectTypeDefinition) { if (buildCtx.hasInputType(typeDefinition) == null) { additionalTypes.add(buildInputType(buildCtx, typeName)); } } else { if (buildCtx.hasOutputType(typeDefinition) == null) { additionalTypes.add(buildOutputType(buildCtx, typeName)); } } }); return additionalTypes; }
protected TypeName createTypeName(GraphqlParser.TypeNameContext ctx) { TypeName.Builder builder = TypeName.newTypeName(); builder.name(ctx.name().getText()); addCommonData(builder, ctx); return builder.build(); }
@SuppressWarnings("unchecked") private Type createTypeIndirection(Map<String, Object> type) { String kind = (String) type.get("kind"); switch (kind) { case "INTERFACE": case "OBJECT": case "UNION": case "ENUM": case "INPUT_OBJECT": case "SCALAR": return TypeName.newTypeName().name((String) type.get("name")).build(); case "NON_NULL": return NonNullType.newNonNullType().type(createTypeIndirection((Map<String, Object>) type.get("ofType"))).build(); case "LIST": return ListType.newListType().type(createTypeIndirection((Map<String, Object>) type.get("ofType"))).build(); default: return assertShouldNeverHappen("Unknown kind %s", kind); } }
TypeName query = TypeName.newTypeName().name((String) queryType.get("name")).build(); boolean nonDefaultQueryName = !"Query".equals(query.getName()); boolean nonDefaultMutationName = false; if (mutationType != null) { TypeName mutation = TypeName.newTypeName().name((String) mutationType.get("name")).build(); nonDefaultMutationName = !"Mutation".equals(mutation.getName()); schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("mutation").typeName(mutation).build()); boolean nonDefaultSubscriptionName = false; if (subscriptionType != null) { TypeName subscription = TypeName.newTypeName().name(((String) subscriptionType.get("name"))).build(); nonDefaultSubscriptionName = !"Subscription".equals(subscription.getName()); schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("subscription").typeName(subscription).build());
TypeDefinition queryTypeDef = typeRegistry.getType("Query").get(); query = buildOutputType(buildCtx, TypeName.newTypeName().name(queryTypeDef.getName()).build()); schemaBuilder.query(query); mutation = buildOutputType(buildCtx, TypeName.newTypeName().name((mutationTypeDef.get().getName())).build()); schemaBuilder.mutation(mutation); subscription = buildOutputType(buildCtx, TypeName.newTypeName().name(subscriptionTypeDef.get().getName()).build()); schemaBuilder.subscription(subscription);
protected FragmentDefinition createFragmentDefinition(GraphqlParser.FragmentDefinitionContext ctx) { FragmentDefinition.Builder fragmentDefinition = FragmentDefinition.newFragmentDefinition(); addCommonData(fragmentDefinition, ctx); fragmentDefinition.name(ctx.fragmentName().getText()); fragmentDefinition.typeCondition(TypeName.newTypeName().name(ctx.typeCondition().typeName().getText()).build()); fragmentDefinition.directives(createDirectives(ctx.directives())); fragmentDefinition.selectionSet(createSelectionSet(ctx.selectionSet())); return fragmentDefinition.build(); }
private void checkTypeExtensionHasCorrespondingType(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, String name, List<? extends TypeDefinition> extTypeList, Class<? extends TypeDefinition> targetClass) { TypeDefinition extensionDefinition = extTypeList.get(0); Optional<? extends TypeDefinition> typeDefinition = typeRegistry.getType(TypeName.newTypeName().name(name).build(), targetClass); if (!typeDefinition.isPresent()) { errors.add(new TypeExtensionMissingBaseTypeError(extensionDefinition)); } }
@SuppressWarnings("unchecked") private void checkTypeExtensionDirectiveRedefinition(List<GraphQLError> errors, TypeDefinitionRegistry typeRegistry, String name, List<? extends TypeDefinition> extensions, Class<? extends TypeDefinition> targetClass) { Optional<? extends TypeDefinition> typeDefinition = typeRegistry.getType(TypeName.newTypeName().name(name).build(), targetClass); if (typeDefinition.isPresent() && typeDefinition.get().getClass().equals(targetClass)) { List<Directive> directives = typeDefinition.get().getDirectives(); Map<String, Directive> directiveMap = FpKit.getByName(directives, Directive::getName, mergeFirst()); extensions.forEach(typeExt -> { List<Directive> extDirectives = typeExt.getDirectives(); extDirectives.forEach(directive -> { if (directiveMap.containsKey(directive.getName())) { errors.add(new TypeExtensionDirectiveRedefinitionError(typeDefinition.get(), directive)); } }); } ); } }
@SuppressWarnings("unchecked") UnionTypeDefinition createUnion(Map<String, Object> input) { assertTrue(input.get("kind").equals("UNION"), "wrong input"); UnionTypeDefinition.Builder unionTypeDefinition = UnionTypeDefinition.newUnionTypeDefinition(); unionTypeDefinition.name((String) input.get("name")); unionTypeDefinition.comments(toComment((String) input.get("description"))); List<Map<String, Object>> possibleTypes = (List<Map<String, Object>>) input.get("possibleTypes"); for (Map<String, Object> possibleType : possibleTypes) { TypeName typeName = TypeName.newTypeName().name((String) possibleType.get("name")).build(); unionTypeDefinition.memberType(typeName); } return unionTypeDefinition.build(); }
private Optional<OperationTypeDefinition> synthOperationTypeDefinition(Function<Type, Optional<ObjectTypeDefinition>> typeReteriver, String opName) { TypeName type = TypeName.newTypeName().name(capitalize(opName)).build(); Optional<ObjectTypeDefinition> typeDef = typeReteriver.apply(type); return typeDef.map(objectTypeDefinition -> OperationTypeDefinition.newOperationTypeDefinition().name(opName).typeName(type).build()); }
/** * We build the query / mutation / subscription path as a tree of referenced types * but then we build the rest of the types specified and put them in as additional types * * @param buildCtx the context we need to work out what we are doing * * @return the additional types not referenced from the top level operations */ private Set<GraphQLType> buildAdditionalTypes(BuildContext buildCtx) { Set<GraphQLType> additionalTypes = new LinkedHashSet<>(); TypeDefinitionRegistry typeRegistry = buildCtx.getTypeRegistry(); typeRegistry.types().values().forEach(typeDefinition -> { TypeName typeName = TypeName.newTypeName().name(typeDefinition.getName()).build(); if (typeDefinition instanceof InputObjectTypeDefinition) { if (buildCtx.hasInputType(typeDefinition) == null) { additionalTypes.add(buildInputType(buildCtx, typeName)); } } else { if (buildCtx.hasOutputType(typeDefinition) == null) { additionalTypes.add(buildOutputType(buildCtx, typeName)); } } }); return additionalTypes; }
protected TypeName createTypeName(GraphqlParser.TypeNameContext ctx) { TypeName.Builder builder = TypeName.newTypeName(); builder.name(ctx.name().getText()); addCommonData(builder, ctx); return builder.build(); }
@SuppressWarnings("unchecked") private Type createTypeIndirection(Map<String, Object> type) { String kind = (String) type.get("kind"); switch (kind) { case "INTERFACE": case "OBJECT": case "UNION": case "ENUM": case "INPUT_OBJECT": case "SCALAR": return TypeName.newTypeName().name((String) type.get("name")).build(); case "NON_NULL": return NonNullType.newNonNullType().type(createTypeIndirection((Map<String, Object>) type.get("ofType"))).build(); case "LIST": return ListType.newListType().type(createTypeIndirection((Map<String, Object>) type.get("ofType"))).build(); default: return assertShouldNeverHappen("Unknown kind %s", kind); } }
TypeName query = TypeName.newTypeName().name((String) queryType.get("name")).build(); boolean nonDefaultQueryName = !"Query".equals(query.getName()); boolean nonDefaultMutationName = false; if (mutationType != null) { TypeName mutation = TypeName.newTypeName().name((String) mutationType.get("name")).build(); nonDefaultMutationName = !"Mutation".equals(mutation.getName()); schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("mutation").typeName(mutation).build()); boolean nonDefaultSubscriptionName = false; if (subscriptionType != null) { TypeName subscription = TypeName.newTypeName().name(((String) subscriptionType.get("name"))).build(); nonDefaultSubscriptionName = !"Subscription".equals(subscription.getName()); schemaDefinition.operationTypeDefinition(OperationTypeDefinition.newOperationTypeDefinition().name("subscription").typeName(subscription).build());
TypeDefinition queryTypeDef = typeRegistry.getType("Query").get(); query = buildOutputType(buildCtx, TypeName.newTypeName().name(queryTypeDef.getName()).build()); schemaBuilder.query(query); mutation = buildOutputType(buildCtx, TypeName.newTypeName().name((mutationTypeDef.get().getName())).build()); schemaBuilder.mutation(mutation); subscription = buildOutputType(buildCtx, TypeName.newTypeName().name(subscriptionTypeDef.get().getName()).build()); schemaBuilder.subscription(subscription);
protected FragmentDefinition createFragmentDefinition(GraphqlParser.FragmentDefinitionContext ctx) { FragmentDefinition.Builder fragmentDefinition = FragmentDefinition.newFragmentDefinition(); addCommonData(fragmentDefinition, ctx); fragmentDefinition.name(ctx.fragmentName().getText()); fragmentDefinition.typeCondition(TypeName.newTypeName().name(ctx.typeCondition().typeName().getText()).build()); fragmentDefinition.directives(createDirectives(ctx.directives())); fragmentDefinition.selectionSet(createSelectionSet(ctx.selectionSet())); return fragmentDefinition.build(); }