SomeInterfaceType = GraphQLInterfaceType( name="SomeInterface", fields=lambda: { "name": GraphQLField(GraphQLString), "some": GraphQLField(SomeInterfaceType), }, ) FooType = GraphQLObjectType( name="Foo", interfaces=[SomeInterfaceType], fields=lambda: { "name": GraphQLField(GraphQLString), "some": GraphQLField(SomeInterfaceType), "tree": GraphQLField(GraphQLNonNull(GraphQLList(FooType))), }, ) BarType = GraphQLObjectType( name="Bar", interfaces=[SomeInterfaceType], fields=lambda: { "name": GraphQLField(GraphQLString), "some": GraphQLField(SomeInterfaceType), "foo": GraphQLField(FooType), }, ) BizType = GraphQLObjectType( name="Biz", fields=lambda: {"fizz": GraphQLField(GraphQLString)})
raise Exception("Throws!") def resolver(root, args, *_): return 'Hello ' + args.get('who', 'World') TestSchema = GraphQLSchema(query=GraphQLObjectType( 'Root', fields=lambda: { 'test': GraphQLField(GraphQLString, args={'who': GraphQLArgument(type=GraphQLString)}, resolver=resolver), 'thrower': GraphQLField(GraphQLNonNull(GraphQLString), resolver=raises) })) def test_GET_functionality_allows_GET_with_query_param(): wsgi = graphql_wsgi(TestSchema) c = Client(wsgi) response = c.get('/', {'query': '{test}'}) assert response.json == {'data': {'test': 'Hello World'}} def test_GET_functionality_allows_GET_with_variable_values(): wsgi = graphql_wsgi(TestSchema)
return NullingData() def nonNullNest(self): return NullingData() def promiseNest(self): return resolved(NullingData()) def nonNullPromiseNest(self): return resolved(NullingData()) DataType = GraphQLObjectType( 'DataType', lambda: { 'sync': GraphQLField(GraphQLString), 'nonNullSync': GraphQLField(GraphQLNonNull(GraphQLString)), 'promise': GraphQLField(GraphQLString), 'nonNullPromise': GraphQLField(GraphQLNonNull(GraphQLString)), 'nest': GraphQLField(DataType), 'nonNullNest': GraphQLField(GraphQLNonNull(DataType)), 'promiseNest': GraphQLField(DataType), 'nonNullPromiseNest': GraphQLField(GraphQLNonNull(DataType)) }) schema = GraphQLSchema(DataType) def order_errors(error): locations = error['locations'] return (locations[0]['column'], locations[0]['line'])
def describe_for_graphql_input_object(): TestInputObject = GraphQLInputObjectType( "TestInputObject", { "foo": GraphQLInputField(GraphQLNonNull(GraphQLInt)), "bar": GraphQLInputField(GraphQLInt), }, ) def returns_no_error_for_a_valid_input(): result = _coerce_value({"foo": 123}, TestInputObject) assert expect_value(result) == {"foo": 123} def returns_an_error_for_a_non_dict_value(): result = _coerce_value(123, TestInputObject) assert expect_errors(result) == [ ("Expected type TestInputObject to be a dict.", [], 123) ] def returns_an_error_for_an_invalid_field(): result = _coerce_value({"foo": nan}, TestInputObject) assert expect_errors(result) == [ ( "Expected type Int. Int cannot represent non-integer value: nan", ["foo"], nan, ) ] def returns_multiple_errors_for_multiple_invalid_fields(): result = _coerce_value({"foo": "abc", "bar": "def"}, TestInputObject) assert expect_errors(result) == [ ( "Expected type Int. Int cannot represent non-integer value: 'abc'", ["foo"], "abc", ), ( "Expected type Int. Int cannot represent non-integer value: 'def'", ["bar"], "def", ), ] def returns_error_for_a_missing_required_field(): result = _coerce_value({"bar": 123}, TestInputObject) assert expect_errors(result) == [ ("Field foo of required type Int! was not provided.", [], {"bar": 123}) ] def returns_error_for_an_unknown_field(): result = _coerce_value({"foo": 123, "unknownField": 123}, TestInputObject) assert expect_errors(result) == [ ( "Field 'unknownField' is not defined by type TestInputObject.", [], {"foo": 123, "unknownField": 123}, ) ] def returns_error_for_a_misspelled_field(): result = _coerce_value({"foo": 123, "bart": 123}, TestInputObject) assert expect_errors(result) == [ ( "Field 'bart' is not defined by type TestInputObject." " Did you mean bar?", [], {"foo": 123, "bart": 123}, ) ] def transforms_names_using_out_name(): # This is an extension of GraphQL.js. ComplexInputObject = GraphQLInputObjectType( "Complex", { "realPart": GraphQLInputField(GraphQLFloat, out_name="real_part"), "imagPart": GraphQLInputField( GraphQLFloat, default_value=0, out_name="imag_part" ), }, ) result = _coerce_value({"realPart": 1}, ComplexInputObject) assert expect_value(result) == {"real_part": 1, "imag_part": 0} def transforms_values_with_out_type(): # This is an extension of GraphQL.js. ComplexInputObject = GraphQLInputObjectType( "Complex", { "real": GraphQLInputField(GraphQLFloat), "imag": GraphQLInputField(GraphQLFloat), }, out_type=lambda value: complex(value["real"], value["imag"]), ) result = _coerce_value({"real": 1, "imag": 2}, ComplexInputObject) assert expect_value(result) == 1 + 2j
def describe_for_graphql_input_object(): TestInputObject = GraphQLInputObjectType( "TestInputObject", { "foo": GraphQLInputField(GraphQLNonNull(GraphQLInt)), "bar": GraphQLInputField(GraphQLInt), }, ) def returns_no_error_for_a_valid_input(): result = coerce_value({"foo": 123}, TestInputObject) assert expect_value(result) == {"foo": 123} def returns_error_for_a_non_dict_value(): result = coerce_value(123, TestInputObject) assert expect_error(result) == [ "Expected type TestInputObject to be a dict." ] def returns_error_for_an_invalid_field(): result = coerce_value({"foo": "abc"}, TestInputObject) assert expect_error(result) == [ "Expected type Int at value.foo;" " Int cannot represent non-integer value: 'abc'" ] def returns_multiple_errors_for_multiple_invalid_fields(): result = coerce_value({ "foo": "abc", "bar": "def" }, TestInputObject) assert expect_error(result) == [ "Expected type Int at value.foo;" " Int cannot represent non-integer value: 'abc'", "Expected type Int at value.bar;" " Int cannot represent non-integer value: 'def'", ] def returns_error_for_a_missing_required_field(): result = coerce_value({"bar": 123}, TestInputObject) assert expect_error(result) == [ "Field value.foo" " of required type Int! was not provided." ] def returns_error_for_an_unknown_field(): result = coerce_value({ "foo": 123, "unknownField": 123 }, TestInputObject) assert expect_error(result) == [ "Field 'unknownField' is not defined" " by type TestInputObject." ] def returns_error_for_a_misspelled_field(): result = coerce_value({"foo": 123, "bart": 123}, TestInputObject) assert expect_error(result) == [ "Field 'bart' is not defined" " by type TestInputObject; did you mean bar?" ]
def returns_true_for_wrapped_types(): assert is_type(GraphQLNonNull(GraphQLString)) is True assert_type(GraphQLNonNull(GraphQLString))
def function_1() -> None: pass def function_2() -> None: pass # Create an object directly at the top level of the file so that # 'test_gather_functions_to_model' can verify that we correctly identify the # resolver DirectObjectType = GraphQLObjectType( name="DirectObjectType", description="GraphQLObject directly created at top level", fields={ "no_resolver": GraphQLField(GraphQLNonNull(GraphQLID)), "resolver": GraphQLField(GraphQLBoolean, resolver=function_1), "lambda_resolver": GraphQLField(GraphQLBoolean, resolver=lambda x: x), }, ) BrokenObjectType = GraphQLObjectType( name="BrokenObjectType", description="Look ma, no fields", fields={} ) def add_field(type: GraphQLType, name: str, resolver: Callable) -> None: # pyre-ignore[16]: Undefined attribute type._fields[name] = GraphQLField(GraphQLNonNull(GraphQLID), resolver=resolver)
def returns_true_for_a_non_null_wrapped_type(): assert is_non_null_type(GraphQLNonNull(ObjectType)) is True assert_non_null_type(GraphQLNonNull(ObjectType))
def returns_false_for_a_not_non_null_wrapped_type(): assert is_non_null_type(GraphQLList( GraphQLNonNull(ObjectType))) is False with raises(TypeError): assert_non_null_type(GraphQLList(GraphQLNonNull(ObjectType)))
def returns_true_for_a_wrapped_input_type(): assert is_input_type(GraphQLList(InputObjectType)) is True assert_input_type(GraphQLList(InputObjectType)) assert is_input_type(GraphQLNonNull(InputObjectType)) is True assert_input_type(GraphQLNonNull(InputObjectType))
def returns_true_for_a_wrapped_output_type(): assert is_output_type(GraphQLList(ObjectType)) is True assert_output_type(GraphQLList(ObjectType)) assert is_output_type(GraphQLNonNull(ObjectType)) is True assert_output_type(GraphQLNonNull(ObjectType))
def returns_true_for_a_non_list_wrapped_type(): assert is_list_type(GraphQLNonNull( GraphQLList(ObjectType))) is False with raises(TypeError): assert_list_type(GraphQLNonNull(GraphQLList(ObjectType)))
serialize=lambda value: "SerializedValue" if value == "DeserializedValue" else None, parse_value=lambda value: "DeserializedValue" if value == "SerializedValue" else None, parse_literal=lambda ast, _variables=None: "DeserializedValue" if ast.value == "SerializedValue" else None, ) TestInputObject = GraphQLInputObjectType( "TestInputObject", { "a": GraphQLInputField(GraphQLString), "b": GraphQLInputField(GraphQLList(GraphQLString)), "c": GraphQLInputField(GraphQLNonNull(GraphQLString)), "d": GraphQLInputField(TestComplexScalar), }, ) TestNestedInputObject = GraphQLInputObjectType( "TestNestedInputObject", { "na": GraphQLInputField(GraphQLNonNull(TestInputObject)), "nb": GraphQLInputField(GraphQLNonNull(GraphQLString)), }, ) TestEnum = GraphQLEnumType(
# This implements the following type system shorthand: # interface Character { # id: String! # name: String # friends: [Character] # appearsIn: [Episode] # secretBackstory: String human_type: GraphQLObjectType droid_type: GraphQLObjectType character_interface: GraphQLInterfaceType = GraphQLInterfaceType( "Character", lambda: { "id": GraphQLField(GraphQLNonNull(GraphQLString), description="The id of the character."), "name": GraphQLField(GraphQLString, description="The name of the character."), "friends": GraphQLField( GraphQLList(character_interface), description="The friends of the character," " or an empty list if they have none.", ), "appearsIn": GraphQLField(GraphQLList(episode_enum), description="Which movies they appear in."), "secretBackstory": GraphQLField(GraphQLString, description="All secrets about their past."),
def returns_true_for_required_arguments(): required_arg = GraphQLArgument(GraphQLNonNull(GraphQLString)) assert is_required_argument(required_arg) is True
def returns_false_for_a_wrapped_input_type(): _assert_non_output_type(GraphQLList(InputObjectType)) _assert_non_output_type(GraphQLNonNull(InputObjectType))
def returns_true_for_required_input_field(): required_field = GraphQLInputField(GraphQLNonNull(GraphQLString)) assert is_required_input_field(required_field) is True
def returns_true_for_list_and_non_null_types(): assert is_wrapping_type(GraphQLList(ObjectType)) is True assert_wrapping_type(GraphQLList(ObjectType)) assert is_wrapping_type(GraphQLNonNull(ObjectType)) is True assert_wrapping_type(GraphQLNonNull(ObjectType))
5, description='Released in 1980.', ), 'JEDI': GraphQLEnumValue( 6, description='Released in 1983.', ) } ) characterInterface = GraphQLInterfaceType( 'Character', description='A character in the Star Wars Trilogy', fields=lambda: { 'id': GraphQLField( GraphQLNonNull(GraphQLString), description='The id of the character.' ), 'name': GraphQLField( GraphQLString, description='The name of the character.' ), 'friends': GraphQLField( GraphQLList(characterInterface), description='The friends of the character, or an empty list if they have none.' ), 'appearsIn': GraphQLField( GraphQLList(episodeEnum), description='Which movies they appear in.' ), },
def returns_true_for_list_of_non_null_types(): assert is_nullable_type(GraphQLList( GraphQLNonNull(ObjectType))) is True assert_nullable_type(GraphQLList(GraphQLNonNull(ObjectType)))
def add_field(type: GraphQLType, name: str, resolver: Callable) -> None: # pyre-ignore[16]: Undefined attribute type._fields[name] = GraphQLField(GraphQLNonNull(GraphQLID), resolver=resolver)
def returns_false_for_non_null_types(): assert is_nullable_type(GraphQLNonNull(ObjectType)) is False with raises(TypeError): assert_nullable_type(GraphQLNonNull(ObjectType))
def throw_error_without_path(): with raises(GraphQLError) as exc_info: assert coerce_input_value(None, GraphQLNonNull(GraphQLInt)) assert exc_info.value.message == ( "Invalid value None: Expected non-nullable type Int! not to be None." )
def unwraps_non_null_type(): assert get_nullable_type(GraphQLNonNull(ObjectType)) is ObjectType
"FurColor", { "BROWN": GraphQLEnumValue(0), "BLACK": GraphQLEnumValue(1), "TAN": GraphQLEnumValue(2), "SPOTTED": GraphQLEnumValue(3), "NO_FUR": GraphQLEnumValue(), "UNKNOWN": None, }, ) ComplexInput = GraphQLInputObjectType( "ComplexInput", { "requiredField": GraphQLInputField(GraphQLNonNull(GraphQLBoolean)), "nonNullField": GraphQLInputField(GraphQLNonNull(GraphQLBoolean), default_value=False), "intField": GraphQLInputField(GraphQLInt), "stringField": GraphQLInputField(GraphQLString), "booleanField": GraphQLInputField(GraphQLBoolean), "stringListField": GraphQLInputField(GraphQLList(GraphQLString)), }, ) ComplicatedArgs = GraphQLObjectType( "ComplicatedArgs",
def unwraps_wrapper_types(): assert get_named_type(GraphQLNonNull(ObjectType)) is ObjectType assert get_named_type(GraphQLList(ObjectType)) is ObjectType
def with_modifiers(types): return (types + [GraphQLList(t) for t in types] + [GraphQLNonNull(t) for t in types] + [GraphQLNonNull(GraphQLList(t)) for t in types])
def unwraps_deeply_wrapper_types(): assert (get_named_type( GraphQLNonNull(GraphQLList(GraphQLNonNull(ObjectType)))) is ObjectType)
def test_prohibits_nesting_nonnull_inside_nonnull(): with raises(Exception) as excinfo: GraphQLNonNull(GraphQLNonNull(GraphQLInt)) assert 'Can only create NonNull of a Nullable GraphQLType but got: Int!.' in str( excinfo.value)
def does_not_convert_non_null_values_to_null_value(): non_null_boolean = GraphQLNonNull(GraphQLBoolean) assert ast_from_value(None, non_null_boolean) is None