/** * This helps you transform the current GraphQLSchema object 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 GraphQLSchema object based on calling build on that builder */ public GraphQLSchema transform(Consumer<Builder> builderConsumer) { Builder builder = newSchema(this); builderConsumer.accept(builder); return builder.build(); }
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(); } }
@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"); } } };
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(); }
/** * 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); }
@Test public void simpleConnection_buildSchema_TypeOfSimpleConnectionIsGraphQLList() throws Exception { GraphQLObjectType object = GraphQLAnnotations.object(MainConnection.class); GraphQLSchema schema = newSchema().query(object).build(); String objsTypeName = schema.getQueryType().getFieldDefinition("objs").getType().getName(); assertThat(objsTypeName, is("ObjChunk")); }
static GraphQLSchema copyReadOnly(GraphQLSchema schema) { return GraphQLSchema.newSchema() .query(schema.getQueryType()) .subscription(schema.getSubscriptionType()) .additionalTypes(schema.getAdditionalTypes()) .build(); }
@Override public TypeResolver getTypeResolver(InterfaceWiringEnvironment environment) { return env -> env.getSchema().getQueryType(); }
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(); } }
@Test public void customRelayMethodList() { try { GraphQLAnnotations.getInstance().setRelay(new CustomRelay()); GraphQLObjectType object = GraphQLAnnotations.object(TestConnections.class); GraphQLSchema schema = newSchema().query(object).build(); graphql.schema.GraphQLObjectType f = (GraphQLObjectType) schema.getType("ObjConnection"); assertTrue(f.getFieldDefinitions().size() == 4); assertTrue(f.getFieldDefinition("nodes").getType() instanceof GraphQLList); assertEquals(((GraphQLList) f.getFieldDefinition("nodes").getType()).getWrappedType().getName(), "Obj"); GraphQLObjectType pageInfo = (GraphQLObjectType) schema.getType("PageInfo"); assertTrue(pageInfo.getFieldDefinition("additionalInfo") != null); } finally { GraphQLAnnotations.getInstance().setRelay(new Relay()); } }
final List<GraphQLEnumType> enums = new ArrayList<>(); for(graphql.schema.GraphQLType graphQLType : graphQLSchema.getAllTypesAsList()){ GraphQLType type = GraphQLObjectTypeFactory.parse(graphQLType); if(graphQLSchema.getQueryType() != null){ List<GraphQLFieldDefinition> fieldQueries = graphQLSchema.getQueryType().getFieldDefinitions(); for(GraphQLFieldDefinition fieldQuery : fieldQueries){ GraphQLQuery query = GraphQLObjectTypeFactory.query(fieldQuery); if(graphQLSchema.getMutationType() != null){ List<GraphQLFieldDefinition> fieldMutations = graphQLSchema.getMutationType().getFieldDefinitions(); for(GraphQLFieldDefinition fieldMutation : fieldMutations){ GraphQLMutation mutation = GraphQLObjectTypeFactory.mutation(fieldMutation); if(graphQLSchema.getSubscriptionType() != null){ List<GraphQLFieldDefinition> fieldSubscriptions = graphQLSchema.getSubscriptionType().getFieldDefinitions(); for(GraphQLFieldDefinition fieldSubscription : fieldSubscriptions){ GraphQLSubscription subscription = GraphQLObjectTypeFactory.subscription(fieldSubscription);
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 GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLType parentType, Field field) { if (schema.getQueryType().equals(parentType)) { if (field.getName().equals(SchemaMetaFieldDef.getName())) { return SchemaMetaFieldDef; } if (field.getName().equals(TypeMetaFieldDef.getName())) { return TypeMetaFieldDef; } } if (field.getName().equals(TypeNameMetaFieldDef.getName()) && (parentType instanceof GraphQLObjectType || parentType instanceof GraphQLInterfaceType || parentType instanceof GraphQLUnionType)) { return TypeNameMetaFieldDef; } if (parentType instanceof GraphQLFieldsContainer) { return schema.getFieldVisibility().getFieldDefinition((GraphQLFieldsContainer) parentType, field.getName()); } return null; } }
if (schema.getQueryType() == parentType) { if (fieldName.equals(SchemaMetaFieldDef.getName())) { return SchemaMetaFieldDef; GraphQLFieldDefinition fieldDefinition = schema.getCodeRegistry().getFieldVisibility().getFieldDefinition(fieldsContainer, fieldName); Assert.assertTrue(fieldDefinition != null, "Unknown field '%s'", fieldName); return fieldDefinition;
@Override public String[] getMutations() { return getInvocationInputFactory().getSchemaProvider().getSchema().getMutationType().getFieldDefinitions().stream().map(GraphQLFieldDefinition::getName).toArray(String[]::new); }
@Override public Object convertOutput(Publisher<T> original, AnnotatedType type, ResolutionEnvironment resolutionEnvironment) { //Subscriptions are expected to return a Publisher directly, so no conversion needed if (resolutionEnvironment.dataFetchingEnvironment.getParentType() == resolutionEnvironment.dataFetchingEnvironment.getGraphQLSchema().getSubscriptionType()) { return original; } //Otherwise, convert the Publisher into a CompletableFuture return convertOutputForNonSubscription(original, type, resolutionEnvironment); }
@Test public void testInputAndOutputWithSameNameWithChildrenWithSameName() { // arrange + act GraphQLSchema schema = newSchema().query(GraphQLAnnotations.object(QuerySameNameWithChildren.class)).build(); // assert assertEquals(schema.getQueryType().getFieldDefinition("getAout").getType().getName(), "A"); assertEquals(schema.getQueryType().getFieldDefinition("getAout").getType().getClass(), GraphQLObjectType.class); assertEquals(schema.getQueryType().getFieldDefinition("getBout").getType().getName(), "B"); assertEquals(schema.getQueryType().getFieldDefinition("getBout").getType().getClass(), GraphQLObjectType.class); assertEquals(schema.getQueryType().getFieldDefinition("getA").getArgument("input").getType().getName(), "InputA"); assertEquals(schema.getQueryType().getFieldDefinition("getA").getArgument("input").getType().getClass(), GraphQLInputObjectType.class); assertEquals(schema.getQueryType().getFieldDefinition("getB").getArgument("input").getType().getClass(), GraphQLInputObjectType.class); } }
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); }
@Override public TypeResolver getTypeResolver(UnionWiringEnvironment environment) { return env -> env.getSchema().getQueryType(); }