@Override public GraphQLObjectType getType(TypeResolutionEnvironment env) { Object javaObject = env.getObject(); if (javaObject instanceof Wizard) { return (GraphQLObjectType) env.getSchema().getType("WizardType"); } else if (javaObject instanceof Witch) { return (GraphQLObjectType) env.getSchema().getType("WitchType"); } else { return (GraphQLObjectType) env.getSchema().getType("NecromancerType"); } } };
@Override public void checkTypeName(TypeName typeName) { if ((getValidationContext().getSchema().getType(typeName.getName())) == null) { String message = String.format("Unknown type %s", typeName.getName()); addError(ValidationErrorType.UnknownType, typeName.getSourceLocation(), message); } } }
private void enterImpl(InlineFragment inlineFragment) { TypeName typeCondition = inlineFragment.getTypeCondition(); GraphQLOutputType type; if (typeCondition != null) { GraphQLType typeConditionType = schema.getType(typeCondition.getName()); if (typeConditionType instanceof GraphQLOutputType) { type = (GraphQLOutputType) typeConditionType; } else { type = null; } } else { type = getParentType(); } addOutputType(type); }
private void enterImpl(FragmentDefinition fragmentDefinition) { enterName(fragmentDefinition.getName()); GraphQLType type = schema.getType(fragmentDefinition.getTypeCondition().getName()); addOutputType(type instanceof GraphQLOutputType ? (GraphQLOutputType) type : null); }
@Override public void checkFragmentDefinition(FragmentDefinition fragmentDefinition) { GraphQLType type = getValidationContext().getSchema().getType(fragmentDefinition.getTypeCondition().getName()); if (type == null) return; if (!(type instanceof GraphQLCompositeType)) { String message = "Fragment type condition is invalid, must be on Object/Interface/Union"; addError(ValidationErrorType.InlineFragmentTypeConditionInvalid, fragmentDefinition.getSourceLocation(), message); } } }
public static GraphQLType getTypeFromAST(GraphQLSchema schema, Type type) { GraphQLType innerType; if (type instanceof ListType) { innerType = getTypeFromAST(schema, ((ListType) type).getType()); return innerType != null ? list(innerType) : null; } else if (type instanceof NonNullType) { innerType = getTypeFromAST(schema, ((NonNullType) type).getType()); return innerType != null ? nonNull(innerType) : null; } return schema.getType(((TypeName) type).getName()); } }
@Override public void checkInlineFragment(InlineFragment inlineFragment) { if (inlineFragment.getTypeCondition() == null) { return; } GraphQLType type = getValidationContext().getSchema().getType(inlineFragment.getTypeCondition().getName()); if (type == null) return; if (!(type instanceof GraphQLCompositeType)) { String message = "Inline fragment type condition is invalid, must be on Object/Interface/Union"; addError(ValidationErrorType.InlineFragmentTypeConditionInvalid, inlineFragment.getSourceLocation(), message); } }
@Override public void checkVariableDefinition(VariableDefinition variableDefinition) { TypeName unmodifiedAstType = getValidationUtil().getUnmodifiedType(variableDefinition.getType()); GraphQLType type = getValidationContext().getSchema().getType(unmodifiedAstType.getName()); if (type == null) return; if (!isInput(type)) { String message = "Wrong type for a variable"; addError(ValidationErrorType.NonInputTypeOnVariable, variableDefinition.getSourceLocation(), message); } } }
@Override public TraversalControl visitFragmentDefinition(FragmentDefinition node, TraverserContext<Node> context) { if (!conditionalNodes.shouldInclude(variables, node.getDirectives())) { return TraversalControl.ABORT; } if (context.getVar(NodeTraverser.LeaveOrEnter.class) == LEAVE) { return TraversalControl.CONTINUE; } QueryTraversalContext parentEnv = context.getVarFromParents(QueryTraversalContext.class); GraphQLCompositeType typeCondition = (GraphQLCompositeType) schema.getType(node.getTypeCondition().getName()); context .setVar(QueryTraversalContext.class, new QueryTraversalContext(typeCondition, typeCondition, parentEnv.getEnvironment(), node)); return TraversalControl.CONTINUE; }
@Override public TraversalControl visitFragmentSpread(FragmentSpread fragmentSpread, TraverserContext<Node> context) { if (!conditionalNodes.shouldInclude(variables, fragmentSpread.getDirectives())) { return TraversalControl.ABORT; } FragmentDefinition fragmentDefinition = fragmentsByName.get(fragmentSpread.getName()); if (!conditionalNodes.shouldInclude(variables, fragmentDefinition.getDirectives())) { return TraversalControl.ABORT; } QueryVisitorFragmentSpreadEnvironment fragmentSpreadEnvironment = new QueryVisitorFragmentSpreadEnvironmentImpl(fragmentSpread, fragmentDefinition, context); if (context.getVar(NodeTraverser.LeaveOrEnter.class) == LEAVE) { postOrderCallback.visitFragmentSpread(fragmentSpreadEnvironment); return TraversalControl.CONTINUE; } preOrderCallback.visitFragmentSpread(fragmentSpreadEnvironment); QueryTraversalContext parentEnv = context.getVarFromParents(QueryTraversalContext.class); GraphQLCompositeType typeCondition = (GraphQLCompositeType) schema.getType(fragmentDefinition.getTypeCondition().getName()); assertNotNull(typeCondition, "Invalid type condition '%s' in fragment '%s'", fragmentDefinition.getTypeCondition().getName(), fragmentDefinition.getName()); context .setVar(QueryTraversalContext.class, new QueryTraversalContext(typeCondition, typeCondition, parentEnv.getEnvironment(), fragmentDefinition)); return TraversalControl.CONTINUE; }
@Override public TraversalControl visitInlineFragment(InlineFragment inlineFragment, TraverserContext<Node> context) { if (!conditionalNodes.shouldInclude(variables, inlineFragment.getDirectives())) { return TraversalControl.ABORT; } QueryVisitorInlineFragmentEnvironment inlineFragmentEnvironment = new QueryVisitorInlineFragmentEnvironmentImpl(inlineFragment, context); if (context.getVar(NodeTraverser.LeaveOrEnter.class) == LEAVE) { postOrderCallback.visitInlineFragment(inlineFragmentEnvironment); return TraversalControl.CONTINUE; } preOrderCallback.visitInlineFragment(inlineFragmentEnvironment); // inline fragments are allowed not have type conditions, if so the parent type counts QueryTraversalContext parentEnv = context.getVarFromParents(QueryTraversalContext.class); GraphQLCompositeType fragmentCondition; if (inlineFragment.getTypeCondition() != null) { TypeName typeCondition = inlineFragment.getTypeCondition(); fragmentCondition = (GraphQLCompositeType) schema.getType(typeCondition.getName()); } else { fragmentCondition = parentEnv.getRawType(); } // for unions we only have other fragments inside context.setVar(QueryTraversalContext.class, new QueryTraversalContext(fragmentCondition, fragmentCondition, parentEnv.getEnvironment(), inlineFragment)); return TraversalControl.CONTINUE; }
@Override public void checkTypeName(TypeName typeName) { if ((getValidationContext().getSchema().getType(typeName.getName())) == null) { String message = String.format("Unknown type %s", typeName.getName()); addError(ValidationErrorType.UnknownType, typeName.getSourceLocation(), message); } } }
private void enterImpl(FragmentDefinition fragmentDefinition) { enterName(fragmentDefinition.getName()); GraphQLType type = schema.getType(fragmentDefinition.getTypeCondition().getName()); addOutputType(type instanceof GraphQLOutputType ? (GraphQLOutputType) type : null); }
@Override public void checkFragmentDefinition(FragmentDefinition fragmentDefinition) { GraphQLType type = getValidationContext().getSchema().getType(fragmentDefinition.getTypeCondition().getName()); if (type == null) return; if (!(type instanceof GraphQLCompositeType)) { String message = "Fragment type condition is invalid, must be on Object/Interface/Union"; addError(ValidationErrorType.InlineFragmentTypeConditionInvalid, fragmentDefinition.getSourceLocation(), message); } } }
public static GraphQLType getTypeFromAST(GraphQLSchema schema, Type type) { GraphQLType innerType; if (type instanceof ListType) { innerType = getTypeFromAST(schema, ((ListType) type).getType()); return innerType != null ? list(innerType) : null; } else if (type instanceof NonNullType) { innerType = getTypeFromAST(schema, ((NonNullType) type).getType()); return innerType != null ? nonNull(innerType) : null; } return schema.getType(((TypeName) type).getName()); } }
@Override public void checkInlineFragment(InlineFragment inlineFragment) { if (inlineFragment.getTypeCondition() == null) { return; } GraphQLType type = getValidationContext().getSchema().getType(inlineFragment.getTypeCondition().getName()); if (type == null) return; if (!(type instanceof GraphQLCompositeType)) { String message = "Inline fragment type condition is invalid, must be on Object/Interface/Union"; addError(ValidationErrorType.InlineFragmentTypeConditionInvalid, inlineFragment.getSourceLocation(), message); } }
@Override public GraphQLType build() { return Types.interfaceTypeBuilder() .name(NAME) .field(Fields.notNullStringField("id")) .typeResolver((env) -> { Object source = env.getObject(); if(source instanceof Element) { Element element = (Element) source; return (GraphQLObjectType) env.getSchema().getType(element.type()); } throw new IllegalArgumentException("Can't resolve type"); }) .build(); }
@Override public void checkVariableDefinition(VariableDefinition variableDefinition) { TypeName unmodifiedAstType = getValidationUtil().getUnmodifiedType(variableDefinition.getType()); GraphQLType type = getValidationContext().getSchema().getType(unmodifiedAstType.getName()); if (type == null) return; if (!isInput(type)) { String message = "Wrong type for a variable"; addError(ValidationErrorType.NonInputTypeOnVariable, variableDefinition.getSourceLocation(), message); } } }
@Override public TraversalControl visitFragmentDefinition(FragmentDefinition node, TraverserContext<Node> context) { if (!conditionalNodes.shouldInclude(variables, node.getDirectives())) { return TraversalControl.ABORT; } if (context.getVar(NodeTraverser.LeaveOrEnter.class) == LEAVE) { return TraversalControl.CONTINUE; } QueryTraversalContext parentEnv = context.getVarFromParents(QueryTraversalContext.class); GraphQLCompositeType typeCondition = (GraphQLCompositeType) schema.getType(node.getTypeCondition().getName()); context .setVar(QueryTraversalContext.class, new QueryTraversalContext(typeCondition, typeCondition, parentEnv.getEnvironment(), node)); return TraversalControl.CONTINUE; }