def prints_custom_directives(): simple_directive = GraphQLDirective("simpleDirective", [DirectiveLocation.FIELD]) complex_directive = GraphQLDirective( "complexDirective", [DirectiveLocation.FIELD, DirectiveLocation.QUERY], description="Complex Directive", args={ "stringArg": GraphQLArgument(GraphQLString), "intArg": GraphQLArgument(GraphQLInt, default_value=-1), }, is_repeatable=True, ) schema = GraphQLSchema( directives=[simple_directive, complex_directive]) output = print_for_test(schema) assert output == dedent(''' directive @simpleDirective on FIELD """Complex Directive""" directive @complexDirective(stringArg: String, intArg: Int = -1) repeatable on FIELD | QUERY ''' # noqa: E501 )
def rejects_a_directive_with_invalid_name(): with raises(GraphQLError) as exc_info: GraphQLDirective("", []) assert str(exc_info.value) == "Expected name to be a non-empty string." with raises(GraphQLError) as exc_info: GraphQLDirective("bad-name", []) assert str(exc_info.value) == ( "Names must only contain [_a-zA-Z0-9] but 'bad-name' does not.")
def does_not_accept_a_bad_name(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective(None, locations=[]) # type: ignore assert str(exc_info.value) == 'Directive must be named.' with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective({'bad': True}, locations=[]) # type: ignore assert str(exc_info.value) == 'The directive name must be a string.'
def does_not_accept_bad_locations(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective("test", locations="bad") # type: ignore assert str(exc_info.value) == "test locations must be a list/tuple." with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective("test", locations=["bad"]) # type: ignore assert str(exc_info.value) == ( "test locations must be DirectiveLocation objects.")
def recects_a_directive_with_incorrectly_typed_locations(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective("Foo", locations="bad") # type: ignore assert str(exc_info.value) == "Foo locations must be a list/tuple." with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective("Foo", locations=["bad"]) # type: ignore assert str(exc_info.value) == ( "Foo locations must be DirectiveLocation objects.")
def does_not_accept_bad_locations(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective('test', locations='bad') # type: ignore assert str(exc_info.value) == 'test locations must be a list/tuple.' with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective('test', locations=['bad']) # type: ignore assert str(exc_info.value) == ( 'test locations must be DirectiveLocation objects.')
def without_extensions(): some_directive = GraphQLDirective( "SomeDirective", [], {"someArg": GraphQLArgument(dummy_type)}) assert some_directive.extensions is None some_arg = some_directive.args["someArg"] assert some_arg.extensions is None assert some_directive.to_kwargs()["extensions"] is None assert some_arg.to_kwargs()["extensions"] is None
def should_detect_locations_removed_from_a_directive(): d1 = GraphQLDirective( "Directive Name", locations=[DirectiveLocation.FIELD_DEFINITION, DirectiveLocation.QUERY], ) d2 = GraphQLDirective( "Directive Name", locations=[DirectiveLocation.FIELD_DEFINITION] ) assert find_removed_locations_for_directive(d1, d2) == [DirectiveLocation.QUERY]
def rejects_a_directive_with_incorrectly_typed_locations(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective("Foo", locations="bad") # type: ignore assert (str(exc_info.value) == "Foo locations must be specified" " as a sequence of DirectiveLocation enum values.") with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective("Foo", locations=["bad"]) # type: ignore assert str(exc_info.value) == ( "Foo locations must be specified" " as a sequence of DirectiveLocation enum values.")
def rejects_a_directive_with_incorrectly_typed_name(): with raises(TypeError, match="missing .* required .* 'name'"): # noinspection PyArgumentList GraphQLDirective() # type: ignore with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective(None, []) # type: ignore assert str(exc_info.value) == "Must provide name." with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective(42, {}) # type: ignore assert str(exc_info.value) == "Expected name to be a string."
def graphql_directive(): directive = GraphQLDirective("Foo", [], description="not lazy") assert directive.name == "Foo" assert directive.description == "not lazy" with raises(TypeError, match="The directive name must be a string\\."): GraphQLDirective(lazy_string, []) with raises(TypeError, match="Foo description must be a string\\."): GraphQLDirective("Foo", [], description=lazy_string) with registered(LazyString): directive = GraphQLDirective("Foo", [], description=lazy_string) assert directive.description is lazy_string assert str(directive.description).endswith("lazy?") with raises(TypeError, match="The directive name must be a string\\."): GraphQLDirective(lazy_string, [])
def directive_accepts_strings_as_locations(): # noinspection PyTypeChecker directive = GraphQLDirective(name="Foo", locations=["SCHEMA", "OBJECT"]) assert directive.locations == [ DirectiveLocation.SCHEMA, DirectiveLocation.OBJECT, ]
def defines_a_directive_with_no_args(): locations = [DirectiveLocation.QUERY] directive = GraphQLDirective("Foo", locations=locations) assert directive.name == "Foo" assert directive.args == {} assert directive.locations == locations
def rejects_a_directive_with_incorrectly_typed_repeatable_flag(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective("Foo", locations=[], is_repeatable=None) # type: ignore assert str( exc_info.value) == "Foo is_repeatable flag must be True or False."
def allows_overriding_the_type_map_reducers(): foo_type = GraphQLObjectType("Foo", {"bar": GraphQLField(GraphQLString)}) query_type = GraphQLObjectType("Query", {"foo": GraphQLField(foo_type)}) baz_directive = GraphQLDirective("Baz", []) log_types = [] log_directives = [] class CustomGraphQLSchema(GraphQLSchema): def type_map_reducer(self, map_, type_): log_types.append(type_) return super().type_map_reducer(map_, type_) def type_map_directive_reducer(self, map_, directive): log_directives.append(directive) return super().type_map_directive_reducer(map_, directive) schema = CustomGraphQLSchema(query_type, directives=[baz_directive]) assert schema.type_map["Query"] == query_type assert schema.type_map["Foo"] == foo_type assert schema.directives == [baz_directive] assert query_type in log_types assert foo_type in log_types assert GraphQLString in log_types assert log_directives == [baz_directive]
def does_not_accept_a_bad_ast_node(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective('test', locations=[], ast_node=Node()) # type: ignore assert str(exc_info.value) == ( 'test AST node must be a DirectiveDefinitionNode.')
def rejects_a_directive_with_incorrectly_typed_ast_node(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective("Foo", locations=[], ast_node=Node()) # type: ignore assert str(exc_info.value) == ( "Foo AST node must be a DirectiveDefinitionNode.")
def rejects_a_directive_with_undefined_locations(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective("Foo", locations=None) # type: ignore assert str(exc_info.value) == ( "Foo locations must be specified" " as a collection of DirectiveLocation enum values.")
def freezes_the_specified_directives(): directives = [GraphQLDirective("SomeDirective", [])] schema = GraphQLSchema(directives=directives) assert isinstance(schema.directives, FrozenList) assert schema.directives == directives directives = schema.directives schema = GraphQLSchema(directives=directives) assert schema.directives is directives
def accepts_strings_as_locations(): # noinspection PyTypeChecker directive = GraphQLDirective(name='test', locations=['SCHEMA', 'OBJECT']) # type: ignore assert directive.locations == [ DirectiveLocation.SCHEMA, DirectiveLocation.OBJECT ]
def with_extensions(): directive_extensions = {"SomeDirectiveExt": "directive"} arg_extensions = {"SomeArgExt": "arg"} some_directive = GraphQLDirective( "SomeDirective", [], {"someArg": GraphQLArgument(dummy_type, extensions=arg_extensions)}, extensions=directive_extensions, ) assert some_directive.extensions is directive_extensions some_arg = some_directive.args["someArg"] assert some_arg.extensions is arg_extensions assert some_directive.to_kwargs()["extensions"] is directive_extensions assert some_arg.to_kwargs()["extensions"] is arg_extensions
def defines_a_repeatable_directive(): locations = [DirectiveLocation.QUERY] directive = GraphQLDirective("Foo", is_repeatable=True, locations=locations) assert directive.name == "Foo" assert directive.args == {} assert directive.is_repeatable is True assert directive.locations == locations
def directive_accepts_input_types_as_arguments(): # noinspection PyTypeChecker directive = GraphQLDirective( name="Foo", locations=[], args={"arg": GraphQLString} ) arg = directive.args["arg"] assert isinstance(arg, GraphQLArgument) assert arg.type is GraphQLString
def freezes_the_specified_directives(): directives_list = [GraphQLDirective("SomeDirective", [])] schema = GraphQLSchema(directives=directives_list) assert isinstance(schema.directives, tuple) assert schema.directives == tuple(directives_list) directives_tuple = schema.directives schema = GraphQLSchema(directives=directives_tuple) assert schema.directives is directives_tuple
def rejects_a_directive_with_incorrectly_typed_description(): with raises(TypeError) as exc_info: # noinspection PyTypeChecker GraphQLDirective( # type: ignore "Foo", locations=[], description={"bad": True}) assert str(exc_info.value) == "Foo description must be a string."
def rejects_a_directive_with_incorrectly_named_args(): with raises(GraphQLError) as exc_info: GraphQLDirective( "Foo", locations=[DirectiveLocation.QUERY], args={"bad-name": GraphQLArgument(GraphQLString)}, ) assert str(exc_info.value) == ( "Names must only contain [_a-zA-Z0-9] but 'bad-name' does not.")
def defines_a_directive_with_no_args(): locations = [DirectiveLocation.QUERY] directive = GraphQLDirective("Foo", locations=locations) assert directive.name == "Foo" assert directive.args == {} assert directive.is_repeatable is False assert directive.extensions is None assert directive.locations == locations
def accepts_input_types_as_arguments(): # noinspection PyTypeChecker directive = GraphQLDirective(name='test', locations=[], args={'arg': GraphQLString}) # type: ignore arg = directive.args['arg'] assert isinstance(arg, GraphQLArgument) assert arg.type is GraphQLString
def accepts_strings_as_locations(): # noinspection PyTypeChecker directive = GraphQLDirective(name="test", locations=["SCHEMA", "OBJECT"]) # type: ignore assert directive.locations == [ DirectiveLocation.SCHEMA, DirectiveLocation.OBJECT, ]
def prints_custom_directives(): custom_directive = GraphQLDirective( name="customDirective", locations=[DirectiveLocation.FIELD]) schema = GraphQLSchema(directives=[custom_directive]) output = print_for_test(schema) assert output == dedent(""" directive @customDirective on FIELD """)