Map<String, GraphQLType> allTypes(final GraphQLSchema schema, final Set<GraphQLType> additionalTypes) { List<GraphQLType> roots = new ArrayList<>(); roots.add(schema.getQueryType()); if (schema.isSupportingMutations()) { roots.add(schema.getMutationType()); } if (schema.isSupportingSubscriptions()) { roots.add(schema.getSubscriptionType()); } if (additionalTypes != null) { roots.addAll(additionalTypes); } if (schema.getDirectives() != null) { roots.addAll(schema.getDirectives()); } roots.add(Introspection.__Schema); GraphQLTypeCollectingVisitor visitor = new GraphQLTypeCollectingVisitor(); TRAVERSER.depthFirst(visitor, roots); return visitor.getResult(); }
private TypePrinter<GraphQLSchema> schemaPrinter() { return (out, type, visibility) -> { GraphQLObjectType queryType = type.getQueryType(); GraphQLObjectType mutationType = type.getMutationType(); GraphQLObjectType subscriptionType = type.getSubscriptionType();
private GraphQLObjectType getRootTypeFromOperation(OperationDefinition operationDefinition) { switch (operationDefinition.getOperation()) { case MUTATION: return assertNotNull(schema.getMutationType()); case QUERY: return assertNotNull(schema.getQueryType()); case SUBSCRIPTION: return assertNotNull(schema.getSubscriptionType()); default: return assertShouldNeverHappen(); } }
private void enterImpl(OperationDefinition operationDefinition) { if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) { addOutputType(schema.getMutationType()); } else if (operationDefinition.getOperation() == OperationDefinition.Operation.QUERY) { addOutputType(schema.getQueryType()); } else if (operationDefinition.getOperation() == OperationDefinition.Operation.SUBSCRIPTION) { addOutputType(schema.getSubscriptionType()); } else { Assert.assertShouldNeverHappen(); } }
public Set<SchemaValidationError> validateSchema(GraphQLSchema schema) { SchemaValidationErrorCollector validationErrorCollector = new SchemaValidationErrorCollector(); checkTypes(schema, validationErrorCollector); traverse(schema.getQueryType(), rules, validationErrorCollector); if (schema.isSupportingMutations()) { traverse(schema.getMutationType(), rules, validationErrorCollector); } if (schema.isSupportingSubscriptions()) { traverse(schema.getSubscriptionType(), rules, validationErrorCollector); } return validationErrorCollector.getErrors(); }
public static GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition) { OperationDefinition.Operation operation = operationDefinition.getOperation(); if (operation == MUTATION) { GraphQLObjectType mutationType = graphQLSchema.getMutationType(); if (mutationType == null) { throw new MissingRootTypeException("Schema is not configured for mutations.", operationDefinition.getSourceLocation()); } return mutationType; } else if (operation == QUERY) { GraphQLObjectType queryType = graphQLSchema.getQueryType(); if (queryType == null) { throw new MissingRootTypeException("Schema does not define the required query root type.", operationDefinition.getSourceLocation()); } return queryType; } else if (operation == SUBSCRIPTION) { GraphQLObjectType subscriptionType = graphQLSchema.getSubscriptionType(); if (subscriptionType == null) { throw new MissingRootTypeException("Schema is not configured for subscriptions.", operationDefinition.getSourceLocation()); } return subscriptionType; } else { return assertShouldNeverHappen("Unhandled case. An extra operation enum has been added without code support"); } }
private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition) { OperationDefinition.Operation operation = operationDefinition.getOperation(); if (operation == MUTATION) { GraphQLObjectType mutationType = graphQLSchema.getMutationType(); if (mutationType == null) { throw new MissingRootTypeException("Schema is not configured for mutations.", operationDefinition.getSourceLocation()); } return mutationType; } else if (operation == QUERY) { GraphQLObjectType queryType = graphQLSchema.getQueryType(); if (queryType == null) { throw new MissingRootTypeException("Schema does not define the required query root type.", operationDefinition.getSourceLocation()); } return queryType; } else if (operation == SUBSCRIPTION) { GraphQLObjectType subscriptionType = graphQLSchema.getSubscriptionType(); if (subscriptionType == null) { throw new MissingRootTypeException("Schema is not configured for subscriptions.", operationDefinition.getSourceLocation()); } return subscriptionType; } else { return assertShouldNeverHappen("Unhandled case. An extra operation enum has been added without code support"); } } }
/** * This allows you to build a schema from an existing schema. It copies everything from the existing * schema and then allows you to replace them. * * @param existingSchema the existing schema * * @return a new schema builder */ public static Builder newSchema(GraphQLSchema existingSchema) { return new Builder() .query(existingSchema.getQueryType()) .mutation(existingSchema.getMutationType()) .subscription(existingSchema.getSubscriptionType()) .codeRegistry(existingSchema.getCodeRegistry()) .clearAdditionalTypes() .clearDirectives() .additionalDirectives(existingSchema.directives) .additionalTypes(existingSchema.additionalTypes); }
@Override public String[] getMutations() { return getInvocationInputFactory().getSchemaProvider().getSchema().getMutationType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new); }
private GraphQLObjectType getRootType(GraphQLSchema schema, OperationDefinition operationDefinition) { if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) { return Objects.requireNonNull(schema.getMutationType()); } else if (operationDefinition.getOperation() == OperationDefinition.Operation.QUERY) { return Objects.requireNonNull(schema.getQueryType()); } else if (operationDefinition.getOperation() == OperationDefinition.Operation.SUBSCRIPTION) { return Objects.requireNonNull(schema.getSubscriptionType()); } else { throw new IllegalStateException("Unknown operation type encountered. Incompatible graphql-java version?"); } } }
@Override public String[] getMutations() { return configuration.getInvocationInputFactory().getSchemaProvider().getSchema().getMutationType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new); }
protected Map<String, Object> buildInput(final String mutationName, final Object... inputs) { Map<String, Object> variables = new HashMap<>(); GraphQLObjectType objectType = graphQLSchema.getMutationType(); GraphQLFieldDefinition graphQLFieldDefinition = objectType.getFieldDefinition(mutationName); List<GraphQLArgument> graphQLArguments = graphQLFieldDefinition.getArguments(); Map<String, Object> inputArgs = new HashMap<>(); if (inputs != null && inputs.length > 0) { Map<Class<?>, GraphQLInputObjectField> argsDefs = graphQLSchemaHolder.getMutationInputTypeResolverMap().get(mutationName); for (Object object : inputs) { Class<?> cls = object.getClass(); GraphQLInputObjectField graphQLInputObjectField = argsDefs.get(cls); if (graphQLInputObjectField != null) { inputArgs.put(graphQLInputObjectField.getName(), convertToMap(object)); } } } if (isClientMutationIdInjected()) inputArgs.put(getClientMutationIdName(), generateClientMutationId()); variables.put(getMutationInputArgumentName(), inputArgs); return variables; }
protected SchemaDefinition(GraphQLSchema schema, SchemaMetadata metadata) { this.schema = schema; this.schemaMetadata = metadata == null ? SchemaMetadata.create() : metadata; this.schemaName = schemaMetadata.getSchemaName() == null || schemaMetadata.getSchemaName().isEmpty() ? schema.getQueryType().getName() : schemaMetadata.getSchemaName(); this.serviceAddress = schemaMetadata.getServiceAddress() == null || schemaMetadata.getServiceAddress().isEmpty() ? Queryable.ADDRESS_PREFIX + "." + schemaName() : schemaMetadata.getServiceAddress(); schemaMetadata.put(SchemaMetadata.METADATA_QUERIES, schema.getQueryType().getFieldDefinitions().stream() .map(GraphQLFieldDefinition::getName).collect(Collectors.toList())); schemaMetadata.put(SchemaMetadata.METADATA_MUTATIONS, !schema.isSupportingMutations() ? Collections.emptyList() : schema.getMutationType().getFieldDefinitions().stream() .map(GraphQLFieldDefinition::getName).collect(Collectors.toList())); }
private GraphQLObjectType getOperationRootType(GraphQLSchema graphQLSchema, OperationDefinition operationDefinition) { if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) { return graphQLSchema.getMutationType(); } else if (operationDefinition.getOperation() == OperationDefinition.Operation.QUERY) { return graphQLSchema.getQueryType(); } else { throw new GraphQLException(); } }
public String buildQuery(final String mutationName) { Assert.hasText(mutationName, "Mutation value must not be null"); GraphQLObjectType objectType = graphQLSchema.getMutationType(); GraphQLFieldDefinition graphQLFieldDefinition = objectType.getFieldDefinition(mutationName); Assert.notNull(graphQLFieldDefinition, "Mutation does not exist"); GraphQLObjectType graphQLOutputType = (GraphQLObjectType) graphQLFieldDefinition.getType(); return String.format( MUTATION_TEMPLATE, mutationName, getMutationArgumentName(graphQLFieldDefinition), getMutationArgumentType(graphQLFieldDefinition), mutationName, getMutationInputArgumentName(), getMutationInputArgumentName(), expandNestedObjectTree("", graphQLOutputType)); }
private GraphQLObjectType getRootTypeFromOperation(OperationDefinition operationDefinition) { switch (operationDefinition.getOperation()) { case MUTATION: return assertNotNull(schema.getMutationType()); case QUERY: return assertNotNull(schema.getQueryType()); case SUBSCRIPTION: return assertNotNull(schema.getSubscriptionType()); default: return assertShouldNeverHappen(); } }
private void enterImpl(OperationDefinition operationDefinition) { if (operationDefinition.getOperation() == OperationDefinition.Operation.MUTATION) { addOutputType(schema.getMutationType()); } else if (operationDefinition.getOperation() == OperationDefinition.Operation.QUERY) { addOutputType(schema.getQueryType()); } else if (operationDefinition.getOperation() == OperationDefinition.Operation.SUBSCRIPTION) { addOutputType(schema.getSubscriptionType()); } else { Assert.assertShouldNeverHappen(); } }
public Set<SchemaValidationError> validateSchema(GraphQLSchema schema) { SchemaValidationErrorCollector validationErrorCollector = new SchemaValidationErrorCollector(); checkTypes(schema, validationErrorCollector); traverse(schema.getQueryType(), rules, validationErrorCollector); if (schema.isSupportingMutations()) { traverse(schema.getMutationType(), rules, validationErrorCollector); } if (schema.isSupportingSubscriptions()) { traverse(schema.getSubscriptionType(), rules, validationErrorCollector); } return validationErrorCollector.getErrors(); }
/** * This allows you to build a schema from an existing schema. It copies everything from the existing * schema and then allows you to replace them. * * @param existingSchema the existing schema * * @return a new schema builder */ public static Builder newSchema(GraphQLSchema existingSchema) { return new Builder() .query(existingSchema.getQueryType()) .mutation(existingSchema.getMutationType()) .subscription(existingSchema.getSubscriptionType()) .codeRegistry(existingSchema.getCodeRegistry()) .clearAdditionalTypes() .clearDirectives() .additionalDirectives(existingSchema.directives) .additionalTypes(existingSchema.additionalTypes); }
@SuppressWarnings("unchecked") @Test public void testDocumentedControllerAndMethods() { GraphQLSchema schema = GraphQLSchemaBuilder.newBuilder() .registerTypeFactory(new JacksonTypeFactory(new ObjectMapper())) .registerGraphQLControllerObjects(ImmutableList.<Object>of(new DocumentedMethodsTest())) .build(); GraphQLObjectType mutationType = (GraphQLObjectType) schema.getMutationType().getFieldDefinition("Mutations").getType(); GraphQLObjectType queryType = (GraphQLObjectType) schema.getQueryType().getFieldDefinition("Queries").getType();; assertEquals("Mutation Description", mutationType.getDescription()); assertEquals("Query Description", queryType.getDescription()); GraphQLFieldDefinition someQuery = queryType.getFieldDefinition("getSomeStrings"); GraphQLFieldDefinition someMutation = mutationType.getFieldDefinition("setSomeStrings"); assertEquals("getSomeStrings description", someQuery.getDescription()); assertEquals("setSomeStrings description", someMutation.getDescription()); } }