graphql.schema.GraphQLType Java Examples
The following examples show how to use
graphql.schema.GraphQLType.
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: TypeGeneratorParameterizedTest.java From graphql-java-type-generator with MIT License | 6 votes |
public void basicsOutput() { GraphQLType type = generator.getOutputType(clazz); Assert.assertThat(type, instanceOf(GraphQLObjectType.class)); GraphQLObjectType objectType = (GraphQLObjectType) type; Assert.assertThat(objectType.getName(), containsString(expectedName)); Assert.assertThat(objectType.getDescription(), containsString("Autogenerated f")); Assert.assertThat(objectType.getFieldDefinitions(), notNullValue()); for (GraphQLFieldDefinition field : objectType.getFieldDefinitions()) { Assert.assertThat(field, notNullValue()); Assert.assertThat(field.getDescription(), containsString("Autogenerated f")); } Assert.assertThat(objectType.getFieldDefinitions().size(), is(expectedNumFields)); }
Example #2
Source File: JavaScriptEvaluator.java From graphql-spqr with Apache License 2.0 | 6 votes |
@Override public int getComplexity(ResolvedField node, int childScore) { Resolver resolver = node.getResolver(); if (resolver == null || Utils.isEmpty(resolver.getComplexityExpression())) { GraphQLType fieldType = node.getFieldType(); if (fieldType instanceof GraphQLScalarType || fieldType instanceof GraphQLEnumType) { return 1; } if (GraphQLUtils.isRelayConnectionType(fieldType)) { Integer pageSize = getPageSize(node.getArguments()); if (pageSize != null) { return pageSize * childScore; } } return 1 + childScore; } Bindings bindings = engine.createBindings(); bindings.putAll(node.getArguments()); bindings.put("childScore", childScore); try { return ((Number) engine.eval(resolver.getComplexityExpression(), bindings)).intValue(); } catch (Exception e) { throw new IllegalArgumentException(String.format("Complexity expression \"%s\" on field %s could not be evaluated", resolver.getComplexityExpression(), node.getName()), e); } }
Example #3
Source File: UnionTest.java From graphql-spqr with Apache License 2.0 | 6 votes |
@Test public void testInlineUnion() { InlineUnionService unionService = new InlineUnionService(); GraphQLSchema schema = new TestSchemaGenerator() .withTypeAdapters(new MapToListTypeAdapter()) .withOperationsFromSingleton(unionService) .generate(); GraphQLOutputType fieldType = schema.getQueryType().getFieldDefinition("union").getType(); assertNonNull(fieldType, GraphQLList.class); GraphQLType list = ((graphql.schema.GraphQLNonNull) fieldType).getWrappedType(); assertListOf(list, GraphQLList.class); GraphQLType map = ((GraphQLList) list).getWrappedType(); assertMapOf(map, GraphQLUnionType.class, GraphQLUnionType.class); GraphQLObjectType entry = (GraphQLObjectType) GraphQLUtils.unwrap(map); GraphQLUnionType key = (GraphQLUnionType) entry.getFieldDefinition("key").getType(); GraphQLNamedOutputType value = (GraphQLNamedOutputType) entry.getFieldDefinition("value").getType(); assertEquals("Simple_One_Two", key.getName()); assertEquals("nice", key.getDescription()); assertEquals(value.getName(), "Education_Street"); assertUnionOf(key, schema.getType("SimpleOne"), schema.getType("SimpleTwo")); assertUnionOf(value, schema.getType("Education"), schema.getType("Street")); }
Example #4
Source File: Directives.java From graphql-spqr with Apache License 2.0 | 6 votes |
public static FragmentDirectiveCollector collect(DataFetchingEnvironment env, ExecutionStepInfo step) { FragmentDirectiveCollector fragmentDirectiveCollector = new FragmentDirectiveCollector(env); // This is safe because top-level fields don't get to here and all deeper fields at least have a parent (source object) and a grand-parent (query root) ExecutionStepInfo rootStep = step.getParent().getParent(); if (rootStep == null) { //Should never be possible, see above return fragmentDirectiveCollector; } GraphQLType rootParentType = GraphQLUtils.unwrapNonNull(rootStep.getType()); while(!(rootParentType instanceof GraphQLObjectType)) { rootStep = rootStep.getParent(); rootParentType = GraphQLUtils.unwrapNonNull(rootStep.getType()); } QueryTraverser traversal = QueryTraverser.newQueryTraverser() .fragmentsByName(env.getFragmentsByName()) .schema(env.getGraphQLSchema()) .variables(env.getVariables()) .root(env.getExecutionStepInfo().getParent().getField().getSingleField()) .rootParentType((GraphQLObjectType) rootParentType) .build(); traversal.visitPostOrder(fragmentDirectiveCollector); return fragmentDirectiveCollector; }
Example #5
Source File: ProtoRegistry.java From rejoiner with Apache License 2.0 | 6 votes |
/** Applies the supplied modifications to the GraphQLTypes. */ private static BiMap<String, GraphQLType> modifyTypes( BiMap<String, GraphQLType> mapping, ImmutableListMultimap<String, TypeModification> modifications) { BiMap<String, GraphQLType> result = HashBiMap.create(mapping.size()); for (String key : mapping.keySet()) { if (mapping.get(key) instanceof GraphQLObjectType) { GraphQLObjectType val = (GraphQLObjectType) mapping.get(key); if (modifications.containsKey(key)) { for (TypeModification modification : modifications.get(key)) { val = modification.apply(val); } } result.put(key, val); } else { result.put(key, mapping.get(key)); } } return result; }
Example #6
Source File: TypeRegistryTest.java From graphql-spqr with Apache License 2.0 | 6 votes |
private void assertSameType(GraphQLType t1, GraphQLType t2, GraphQLCodeRegistry code1, GraphQLCodeRegistry code2) { assertSame(t1, t2); if (t1 instanceof GraphQLInterfaceType) { GraphQLInterfaceType i = (GraphQLInterfaceType) t1; assertSame(code1.getTypeResolver(i), code2.getTypeResolver(i)); } if (t1 instanceof GraphQLUnionType) { GraphQLUnionType u = (GraphQLUnionType) t1; assertSame(code1.getTypeResolver(u), code2.getTypeResolver(u)); } if (t1 instanceof GraphQLFieldsContainer) { GraphQLFieldsContainer c = (GraphQLFieldsContainer) t1; c.getFieldDefinitions().forEach(fieldDef -> assertSame(code1.getDataFetcher(c, fieldDef), code2.getDataFetcher(c, fieldDef))); } }
Example #7
Source File: SchemaToProto.java From rejoiner with Apache License 2.0 | 6 votes |
@SuppressWarnings("JdkObsolete") static Set<GraphQLType> getAllTypes(GraphQLSchema schema) { LinkedHashSet<GraphQLType> types = new LinkedHashSet<>(); LinkedList<GraphQLObjectType> loop = new LinkedList<>(); loop.add(schema.getQueryType()); types.add(schema.getQueryType()); while (!loop.isEmpty()) { for (GraphQLFieldDefinition field : loop.pop().getFieldDefinitions()) { GraphQLType type = field.getType(); if (type instanceof GraphQLList) { type = ((GraphQLList) type).getWrappedType(); } if (!types.contains(type)) { if (type instanceof GraphQLEnumType) { types.add(field.getType()); } if (type instanceof GraphQLObjectType) { types.add(type); loop.add((GraphQLObjectType) type); } } } } return types; }
Example #8
Source File: ProtoRegistryTest.java From rejoiner with Apache License 2.0 | 6 votes |
@Test public void protoRegistryShouldIncludeAllProtoTypesFromFile() { Set<GraphQLType> graphQLTypes = ProtoRegistry.newBuilder().add(TestProto.getDescriptor()).build().listTypes(); assertThat(FluentIterable.from(graphQLTypes).transform(GET_NAME)) .containsExactly( "javatests_com_google_api_graphql_rejoiner_proto_Proto1_MapFieldEntry", "Input_javatests_com_google_api_graphql_rejoiner_proto_Proto1_MapFieldEntry", "javatests_com_google_api_graphql_rejoiner_proto_Proto1", "javatests_com_google_api_graphql_rejoiner_proto_Proto2", "javatests_com_google_api_graphql_rejoiner_proto_Proto1_InnerProto", "javatests_com_google_api_graphql_rejoiner_proto_Proto2_TestEnum", "javatests_com_google_api_graphql_rejoiner_proto_Proto2_NestedProto", "javatests_com_google_api_graphql_rejoiner_proto_TestEnumWithComments", "Input_javatests_com_google_api_graphql_rejoiner_proto_Proto1", "Input_javatests_com_google_api_graphql_rejoiner_proto_Proto2", "Input_javatests_com_google_api_graphql_rejoiner_proto_Proto1_InnerProto", "Input_javatests_com_google_api_graphql_rejoiner_proto_Proto2_NestedProto"); }
Example #9
Source File: FetchParamsTest.java From hypergraphql with Apache License 2.0 | 6 votes |
@Test @DisplayName("Environment parent type must have a name") void environment_must_have_parent_type_name() { HGQLSchema schema = mock(HGQLSchema.class); DataFetchingEnvironment environment = mock(DataFetchingEnvironment.class); Field field1 = mock(Field.class); List<Field> fields = Collections.singletonList(field1); when(environment.getFields()).thenReturn(fields); when(field1.getName()).thenReturn("field1"); FieldConfig fieldConfig = mock(FieldConfig.class); Map<String, FieldConfig> schemaFields = Collections.singletonMap("field1", fieldConfig); when(schema.getFields()).thenReturn(schemaFields); GraphQLType parentType = mock(GraphQLType.class); when(environment.getParentType()).thenReturn(parentType); Executable executable = () -> new FetchParams(environment, schema); assertThrows(NullPointerException.class, executable); }
Example #10
Source File: TypeRegistryTest.java From graphql-spqr with Apache License 2.0 | 6 votes |
@Test public void additionalTypesFullCopyTest() { GraphQLSchema schema = new TestSchemaGenerator() .withOperationsFromSingleton(new Service()) .generate(); GraphQLSchema schema2 = new TestSchemaGenerator() .withOperationsFromSingleton(new Service()) .withAdditionalTypes(schema.getAllTypesAsList(), schema.getCodeRegistry()) .generate(); schema.getTypeMap().values().stream() .filter(type -> !GraphQLUtils.isIntrospectionType(type) && type != schema.getQueryType()) .forEach(type -> { assertTrue(schema2.getTypeMap().containsKey(type.getName())); GraphQLType type2 = schema2.getTypeMap().get(type.getName()); assertSameType(type, type2, schema.getCodeRegistry(), schema2.getCodeRegistry()); }); GraphQL exe = GraphQL.newGraphQL(schema2).build(); ExecutionResult result = exe.execute("{mix(id: \"1\") {... on Street {name}}}"); assertNoErrors(result); }
Example #11
Source File: TypeWrapper_ReflectionList.java From graphql-java-type-generator with MIT License | 5 votes |
@Override public GraphQLType wrapType(GraphQLType interiorType, TypeSpecContainer originalObject) { if (!canWrapList(originalObject)) { return interiorType; } return new GraphQLList(interiorType); }
Example #12
Source File: FetchParamsTest.java From hypergraphql with Apache License 2.0 | 5 votes |
@Test @DisplayName("Happy path for a Query type") void happy_path_query_config() { String uri = "abc123"; HGQLSchema schema = mock(HGQLSchema.class); DataFetchingEnvironment environment = mock(DataFetchingEnvironment.class); Field field1 = mock(Field.class); List<Field> fields = Collections.singletonList(field1); when(environment.getFields()).thenReturn(fields); when(field1.getName()).thenReturn("field1"); FieldConfig fieldConfig = mock(FieldConfig.class); Map<String, FieldConfig> schemaFields = Collections.singletonMap("field1", fieldConfig); when(schema.getFields()).thenReturn(schemaFields); when(fieldConfig.getId()).thenReturn(uri); GraphQLType parent = mock(GraphQLType.class); when(environment.getParentType()).thenReturn(parent); when(parent.getName()).thenReturn("Query"); FetchParams fetchParams = new FetchParams(environment, schema); assertNotNull(fetchParams); assertEquals(uri, fetchParams.getPredicateURI()); }
Example #13
Source File: StaticTypeRepository.java From graphql-java-type-generator with MIT License | 5 votes |
@Override public GraphQLType registerType(String typeName, GraphQLType graphQlType, TypeKind typeKind) { switch (typeKind) { case OBJECT: case INTERFACE: return registerType(typeName, (GraphQLOutputType) graphQlType); case INPUT_OBJECT: return registerType(typeName, (GraphQLInputType) graphQlType); default: return null; } }
Example #14
Source File: GraphQLTypeAssertions.java From graphql-spqr with Apache License 2.0 | 5 votes |
public static void assertInputMapOf(GraphQLType mapType, Class<? extends GraphQLType> keyType, Class<? extends GraphQLType> valueType) { assertEquals(GraphQLList.class, mapType.getClass()); GraphQLType entry = GraphQLUtils.unwrap(mapType); assertTrue(entry instanceof GraphQLInputObjectType); GraphQLInputType key = ((GraphQLInputObjectType) entry).getFieldDefinition("key").getType(); GraphQLInputType value = ((GraphQLInputObjectType) entry).getFieldDefinition("value").getType(); assertTrue(keyType.isAssignableFrom(key.getClass())); assertTrue(valueType.isAssignableFrom(value.getClass())); }
Example #15
Source File: TypeGeneratorListOfListTest.java From graphql-java-type-generator with MIT License | 5 votes |
@Test public void testGeneratedListOfListOfList() { logger.debug("testGeneratedListOfListOfList"); Object objectType = generator.getOutputType(ClassWithListOfListOfList.class); Assert.assertThat(objectType, instanceOf(GraphQLObjectType.class)); Assert.assertThat(objectType, not(instanceOf(GraphQLList.class))); GraphQLFieldDefinition field = ((GraphQLObjectType) objectType) .getFieldDefinition("listOfListOfListOfInts"); Assert.assertThat(field, notNullValue()); GraphQLOutputType listType = field.getType(); Assert.assertThat(listType, instanceOf(GraphQLList.class)); GraphQLType wrappedType = ((GraphQLList) listType).getWrappedType(); assertListOfListOfInt(wrappedType); }
Example #16
Source File: GlitrTypeMap.java From glitr with MIT License | 5 votes |
@Override public Object putIfAbsent(Object key, Object value) { if (isClass(key)) { nameRegistry.putIfAbsent(((Class) key).getSimpleName(), (GraphQLType) value); return classRegistry.putIfAbsent((Class) key, (GraphQLType) value); } else if (isString(key)) { return nameRegistry.putIfAbsent((String) key, (GraphQLType) value); } throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); }
Example #17
Source File: UniquenessTest.java From graphql-spqr with Apache License 2.0 | 5 votes |
private void testRootQueryArgumentTypeUniqueness(GraphQLSchema schema) { List<GraphQLType> inputTypes = schema.getQueryType().getFieldDefinitions().stream() .flatMap(def -> def.getArguments().stream()) .map(GraphQLArgument::getType) .map(GraphQLUtils::unwrap) .collect(Collectors.toList()); assertEquals(2, inputTypes.size()); assertTrue(inputTypes.stream().allMatch(type -> inputTypes.get(0) == type)); }
Example #18
Source File: FederationSdlPrinter.java From federation-jvm with MIT License | 5 votes |
/** * This can print an in memory GraphQL schema back to a logical schema definition * * @param schema the schema in play * @return the logical schema definition */ public String print(GraphQLSchema schema) { StringWriter sw = new StringWriter(); PrintWriter out = new PrintWriter(sw); GraphqlFieldVisibility visibility = schema.getCodeRegistry().getFieldVisibility(); printer(schema.getClass()).print(out, schema, visibility); List<GraphQLType> typesAsList = schema.getAllTypesAsList() .stream() .sorted(Comparator.comparing(GraphQLNamedType::getName)) .filter(options.getIncludeTypeDefinition()) .collect(toList()); printType(out, typesAsList, GraphQLInterfaceType.class, visibility); printType(out, typesAsList, GraphQLUnionType.class, visibility); printType(out, typesAsList, GraphQLObjectType.class, visibility); printType(out, typesAsList, GraphQLEnumType.class, visibility); printType(out, typesAsList, GraphQLScalarType.class, visibility); printType(out, typesAsList, GraphQLInputObjectType.class, visibility); String result = sw.toString(); if (result.endsWith("\n\n")) { result = result.substring(0, result.length() - 1); } return result; }
Example #19
Source File: TypeGeneratorParameterizedTest.java From graphql-java-type-generator with MIT License | 5 votes |
public void basicsInput() { if (expectedToSucceed == false) { try { generator.getInputType(clazz); fail("Should have caught exception"); } catch (Exception e) { Assert.assertThat(e, instanceOf(RuntimeException.class)); } return; } GraphQLType type = generator.getInputType(clazz); Assert.assertThat(type, instanceOf(GraphQLInputObjectType.class)); GraphQLInputObjectType objectType = (GraphQLInputObjectType) type; Assert.assertThat(objectType.getName(), containsString(expectedName)); Assert.assertThat(objectType.getDescription(), containsString("Autogenerated f")); Assert.assertThat(objectType.getFields(), notNullValue()); for (GraphQLInputObjectField field : objectType.getFields()) { Assert.assertThat(field, notNullValue()); Assert.assertThat(field.getDescription(), containsString("Autogenerated f")); } Assert.assertThat(objectType.getFields().size(), is(expectedNumFields)); }
Example #20
Source File: UniquenessTest.java From graphql-spqr with Apache License 2.0 | 5 votes |
private void testRootQueryTypeUniqueness(GraphQLSchema schema) { List<GraphQLType> fieldTypes = schema.getQueryType().getFieldDefinitions().stream() .map(GraphQLFieldDefinition::getType) .map(GraphQLUtils::unwrap) .collect(Collectors.toList()); assertEquals(2, fieldTypes.size()); assertTrue(fieldTypes.stream().allMatch(type -> fieldTypes.get(0) == type)); }
Example #21
Source File: TypeGenerator.java From graphql-java-type-generator with MIT License | 5 votes |
protected GraphQLType generateType(Object object, TypeKind typeKind) { switch (typeKind) { case OBJECT: return generateOutputType(object); case INTERFACE: return generateInterfaceType(object); case INPUT_OBJECT: return generateInputType(object); case ENUM: return generateEnumType(object); default: return null; } }
Example #22
Source File: TypeGeneratorMapTest.java From graphql-java-type-generator with MIT License | 5 votes |
@Test public void testMapWithTypeVars() throws JsonProcessingException, NoSuchMethodException, SecurityException { logger.debug("testMapWithTypeVars"); Map<Map<? extends String, ? extends String>, Map<? extends String, ? extends String>> map; map = new HashMap<Map<? extends String, ? extends String>, Map<? extends String, ? extends String>>(); Object objectType = DefaultBuildContext.reflectionContext.getOutputType(map); assertThat(objectType, instanceOf(GraphQLType.class)); //uncomment this to print out more data // GraphQLObjectType queryType = newObject() // .name("testQuery") // .field(newFieldDefinition() // .type((GraphQLOutputType) objectType) // .name("testObj") // .staticValue(map) // .build()) // .build(); // GraphQLSchema testSchema = GraphQLSchema.newSchema() // .query(queryType) // .build(); // // ExecutionResult queryResult = new GraphQL(testSchema).execute(TypeGeneratorTest.querySchema); // assertThat(queryResult.getErrors(), is(empty())); // Map<String, Object> resultMap = (Map<String, Object>) queryResult.getData(); // if (logger.isDebugEnabled()) { // logger.debug("testMapWithTypeVars resultMap {}", TypeGeneratorTest.prettyPrint(resultMap)); // } }
Example #23
Source File: GraphQLUtils.java From graphql-spqr with Apache License 2.0 | 5 votes |
public static boolean isRelayConnectionType(GraphQLType type) { if (!(type instanceof GraphQLObjectType)) { return false; } GraphQLObjectType objectType = (GraphQLObjectType) type; return !objectType.getName().equals(CONNECTION) && objectType.getName().endsWith(CONNECTION) && objectType.getFieldDefinition(EDGES) != null && objectType.getFieldDefinition(PAGE_INFO) != null; }
Example #24
Source File: SchemaGeneratorConfigurationTest.java From graphql-spqr with Apache License 2.0 | 5 votes |
@Test(expected = ConfigurationException.class) public void testConflictingKnownTypes() { Set<GraphQLType> additionalTypes = new HashSet<>(); additionalTypes.add(GraphQLObjectType.newObject().name("String").build()); additionalTypes.add(Scalars.GraphQLString); new TestSchemaGenerator() .withOperationsFromSingleton(new Dummy()) .withAdditionalTypes(additionalTypes) .generate(); }
Example #25
Source File: GlitrTypeMap.java From glitr with MIT License | 5 votes |
@Override public Object getOrDefault(Object key, Object defaultValue) { if (isClass(key)) { return classRegistry.getOrDefault(key, (GraphQLType) defaultValue); } else if (isString(key)) { return nameRegistry.getOrDefault(key, (GraphQLType) defaultValue); } throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); }
Example #26
Source File: FieldType_Reflection.java From graphql-java-type-generator with MIT License | 5 votes |
protected GraphQLType getTypeOfFieldFromSignature( Class<?> typeClazz, Type genericType, String name, TypeKind typeKind) { Type gType = null; //attempt GraphQLList from types if (genericType instanceof ParameterizedType || genericType instanceof WildcardType || genericType instanceof TypeVariable) { gType = genericType; } return getContext().getParameterizedType(typeClazz, gType, typeKind); }
Example #27
Source File: FederationSdlPrinter.java From federation-jvm with MIT License | 5 votes |
@SuppressWarnings("unchecked") private void printType(PrintWriter out, List<GraphQLType> typesAsList, Class typeClazz, GraphqlFieldVisibility visibility) { typesAsList.stream() .filter(type -> typeClazz.isAssignableFrom(type.getClass())) .forEach(type -> printType(out, type, visibility)); }
Example #28
Source File: FetchParamsTest.java From hypergraphql with Apache License 2.0 | 5 votes |
@Test @DisplayName("Happy path for a non-Query type with no target") void happy_path_non_query_type_with_no_target() { String uri = "abc123"; HGQLSchema schema = mock(HGQLSchema.class); DataFetchingEnvironment environment = mock(DataFetchingEnvironment.class); Field field1 = mock(Field.class); List<Field> fields = Collections.singletonList(field1); when(environment.getFields()).thenReturn(fields); when(field1.getName()).thenReturn("field1"); FieldConfig fieldConfig = mock(FieldConfig.class); Map<String, FieldConfig> schemaFields = Collections.singletonMap("field1", fieldConfig); when(schema.getFields()).thenReturn(schemaFields); when(fieldConfig.getId()).thenReturn(uri); GraphQLType parent = mock(GraphQLType.class); when(environment.getParentType()).thenReturn(parent); when(parent.getName()).thenReturn("non-Query"); TypeConfig typeConfig = mock(TypeConfig.class); Map<String, TypeConfig> types = Collections.singletonMap("non-Query", typeConfig); when(schema.getTypes()).thenReturn(types); FieldOfTypeConfig fieldOfTypeConfig = mock(FieldOfTypeConfig.class); when(typeConfig.getField("field1")).thenReturn(fieldOfTypeConfig); when(fieldOfTypeConfig.getTargetName()).thenReturn("non-Query"); when(typeConfig.getId()).thenReturn("targetUri"); FetchParams fetchParams = new FetchParams(environment, schema); assertNotNull(fetchParams); assertEquals(uri, fetchParams.getPredicateURI()); assertEquals("targetUri", fetchParams.getTargetURI()); }
Example #29
Source File: NameHelper.java From smallrye-graphql with Apache License 2.0 | 5 votes |
public static String getName(GraphQLType graphQLType) { if (graphQLType instanceof GraphQLNamedType) { return ((GraphQLNamedType) graphQLType).getName(); } else if (graphQLType instanceof GraphQLNonNull) { return getName(((GraphQLNonNull) graphQLType).getWrappedType()); } else if (graphQLType instanceof GraphQLList) { return getName(((GraphQLList) graphQLType).getWrappedType()); } return EMPTY; }
Example #30
Source File: DefaultType_ReflectionScalarsLookup.java From graphql-java-type-generator with MIT License | 5 votes |
@Override public GraphQLType getDefaultType(Object object, TypeKind typeKind) { GraphQLScalarType scalar = getDefaultScalarType(object); if (scalar != null) return scalar; if (TypeKind.OBJECT.equals(typeKind)) { return getDefaultOutputType(object); } return null; }